Access to static functions or member variables of a class can be performed in two ways: through an object ( obj.member_fun() or obj.member_var ) or through a class ( Class::member_fun() or Class::member_var ). However, in constexpr functions constexpr Clang gives an error when accessing an object and requires the use of class access:
struct S { constexpr static auto s_v = 42; constexpr static auto v() { return s_v; } }; #define TEST 1 constexpr auto foo(S const& s [[maybe_unused]]) { #if TEST constexpr auto v = sv();
Live Example compiled with -std=c++1z and #define TEST 1 for Clang 5.0 SVN with error message:
Start prog.cc:12:24: error: constexpr variable 'v' must be initialized by a constant expression constexpr auto v = sv(); // ERROR for clang, OK for gcc ^~~~~ prog.cc:22:24: error: constexpr variable 'v' must be initialized by a constant expression constexpr auto v = s.s_v; // ERROR for clang, OK for gcc ^~~~~ 2 errors generated. 1 Finish
Question : is this a Clang error, or is gcc too liberal in accepting both forms of syntax to access the static member in the constexpr function?
c ++ clang static-members constexpr c ++ 17
TemplateRex
source share