inline-variable

Are all constexpr variable implicitly inline?

痴心易碎 提交于 2020-07-17 05:51:53
问题 I was playing around with auto template parameters and I was surprised that this code didn't compiled: constexpr auto bar = 2; template<auto& T> struct Foo { auto operator()() const { return T; } }; int main() { Foo<bar> b; b(); } Visual Studio 15.7 (preview 4) spit out these errors: error C2970: 'Foo': template parameter 'T': 'bar': an expression involving objects with internal linkage cannot be used as a non-type argument note: see declaration of 'Foo' note: see declaration of 'bar' error

Are all constexpr variable implicitly inline?

 ̄綄美尐妖づ 提交于 2020-07-17 05:51:51
问题 I was playing around with auto template parameters and I was surprised that this code didn't compiled: constexpr auto bar = 2; template<auto& T> struct Foo { auto operator()() const { return T; } }; int main() { Foo<bar> b; b(); } Visual Studio 15.7 (preview 4) spit out these errors: error C2970: 'Foo': template parameter 'T': 'bar': an expression involving objects with internal linkage cannot be used as a non-type argument note: see declaration of 'Foo' note: see declaration of 'bar' error

Are inline variables unique across boundaries?

孤街浪徒 提交于 2019-12-18 04:31:55
问题 This is a follow up of this question. As mentioned in the comments to the answer: An inline variable has the property that - It has the same address in every translation unit . [...] Usually you achieved that by defining the variable in a cpp file, but with the inline specifier you can just declare/define your variables in a header file and every translation unit using this inline variable uses exactly the same object. Moreover, from the answer itself: While the language does not guarantee

Initializer “sizeof(T)” of inline static auto… Does it need instantiation?

被刻印的时光 ゝ 提交于 2019-12-07 05:19:28
问题 What should happen if an expression's type is not dependent, but we use it to initialize a static auto variable? GCC and Clang differ in their behavior template<typename T> struct A { static inline auto x = sizeof(T{}.f); }; A<int> a; GCC doesn't raise an error. But Clang thinks that this is invalid because it instantiates the operand of "sizeof". GCC appears to skip that step because sizeof(T{}.f) always has type size_t (not type dependent), so it already knows type of x without

Initializer “sizeof(T)” of inline static auto… Does it need instantiation?

為{幸葍}努か 提交于 2019-12-05 08:37:17
What should happen if an expression's type is not dependent, but we use it to initialize a static auto variable? GCC and Clang differ in their behavior template<typename T> struct A { static inline auto x = sizeof(T{}.f); }; A<int> a; GCC doesn't raise an error. But Clang thinks that this is invalid because it instantiates the operand of "sizeof". GCC appears to skip that step because sizeof(T{}.f) always has type size_t (not type dependent), so it already knows type of x without instantiation. Both compilers conformly reject the program if we refer to x , for example by (void) a.x; . Does it

Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr?

霸气de小男生 提交于 2019-11-30 07:56:31
问题 Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr ? For example, see is_integral_v: template< class T > inline constexpr bool is_integral_v = is_integral<T>::value; Is this just a matter of style or is there some difference in behavior? As far as I know constexpr variables are implicitly inline . Edit: Looking at the draft of the latest standard, it also uses inline constexpr . The question actually applies to the standard, then. 回答1: [basic

Are inline variables unique across boundaries?

ぐ巨炮叔叔 提交于 2019-11-29 05:34:39
This is a follow up of this question . As mentioned in the comments to the answer: An inline variable has the property that - It has the same address in every translation unit . [...] Usually you achieved that by defining the variable in a cpp file, but with the inline specifier you can just declare/define your variables in a header file and every translation unit using this inline variable uses exactly the same object. Moreover, from the answer itself: While the language does not guarantee (or even mention) what happens when you use this new feature across shared libraries boundaries, it does