variable-templates

Partial specilization of static variable template in class template

你说的曾经没有我的故事 提交于 2019-12-06 05:27:28
问题 If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: template <typename T> static X<T> x_in_y; }; template< typename T2> template< typename T> X<T> Y<T2>::x_in_y{200}; template<> template<> X<float> Y<int>::x_in_y<float>{100}; template<> template<> X<int> Y<int>::x_in_y<int>{101}; template< > template< typename T > X<T> Y<bool>::x_in_y{77

C++14 warning: too many template headers for variable (should be 0)

狂风中的少年 提交于 2019-12-05 10:15:12
问题 While experimenting with the recent g++-5 compiler, I wrote below statement in a file: template<T> T a; template<> int a = 1; Which results in: warning: too many template headers for a (should be 0) Also effectively, it doesn't really specialize a<int> . e.g. template<typename T> T a; template<> int a = 1; int main () { std::cout << a<double> << "\n"; // prints 0; OK std::cout << a<int> << "\n"; // prints 0! why not 1? } What is the mystery about this syntax? 回答1: Template arguments can only

Type_traits *_v variable template utility order fails to compile

帅比萌擦擦* 提交于 2019-12-05 03:26:34
Having seen this answer , I tried to come up with a variable template utility to the code from it: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template, class... Args> struct is_specialization<Template<Args...>, Template> : std::true_type {}; And implement it like so: template <template <class...> class Template, class... Args> constexpr bool is_specialization_v = is_specialization<Template<Args...>, Template>::value; because that's how I saw similar utilities to be implemented in the header file of

Partial specilization of static variable template in class template

我的梦境 提交于 2019-12-04 09:10:26
If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: template <typename T> static X<T> x_in_y; }; template< typename T2> template< typename T> X<T> Y<T2>::x_in_y{200}; template<> template<> X<float> Y<int>::x_in_y<float>{100}; template<> template<> X<int> Y<int>::x_in_y<int>{101}; template< > template< typename T > X<T> Y<bool>::x_in_y{77}; int main() { std::cout << Y<int>::x_in_y<int> << std::endl; std::cout << Y<int>::x_in_y<float> <<

Variable template in template class - unexpected error (possible bug?)

和自甴很熟 提交于 2019-11-30 11:22:19
Having: struct Value { template<class T> static constexpr T value{0}; }; (0) ideone template<typename TValue> struct Something { void x() { static_assert(TValue::template value<int> == 0, ""); } }; int main() { Something<Value>{}.x(); return 0; } Does not compile with clang++ 3.6. error: cannot refer to variable template 'value' without a template argument list Does not compile with g++ 5.2. error: ‘template constexpr const T Value::value’ is not a function template (1) ideone Compiles with both clang++ and g++. struct Something { void x() { static_assert(Value::template value<int> == 0, "");

Variable template at class scope

左心房为你撑大大i 提交于 2019-11-28 02:12:50
Using N3651 as a basis, A variable template at class scope is a static data member template . The example given is: struct matrix_constants { template <typename T> using pauli = hermitian_matrix<T, 2>; Yet all of the following definitions give an error: struct foo { template <typename T> T pi = T{3.14}; }; template <typename T> struct foo2 { template <typename U = T> U pi = U{3.14}; }; template <typename T> struct foo3 { template <T> T pi = 42; }; error: member 'pi' declared as a template What gives? Casey EDIT: The committee has spoken , Clang is correct to require the static keyword for