variable-templates

Recursive computation using variable templates - gcc vs clang

非 Y 不嫁゛ 提交于 2020-06-11 16:53:09
问题 Consider the following example: #include <cstdio> template <int N> int fib = fib<N - 1> + fib<N - 2>; template <> int fib<2> = 1; template <> int fib<1> = 1; int main() { std::printf("%d %d %d", fib<4>, fib<5>, fib<6>); } GCC 7.x, 8.x, 9.x, and 10.x all print out the expected result of 3 5 8 . Clang 5.x, 6.x, 7.x, 8.x, 9.x, and 10.x all print out 1 3 4 as a result. live example on godbolt.org Clang's behavior is surprising. Is there any subtle interaction between variable template

Can a variable template be passed as a template template argument?

☆樱花仙子☆ 提交于 2020-05-23 04:20:42
问题 The following nonsensical example does not compile, but is there some other way to pass a variable template as a template template argument? template<typename T> constexpr auto zero = T{0}; template<typename T, template<typename> auto VariableTemplate> constexpr auto add_one() { return VariableTemplate<T> + T{1}; } int main() { return add_one<int, zero>(); } Try on Compiler Explorer 回答1: Short answer: No. Long answer: Yes you can using some indirection through a class template: template

How do I expand integer_sequence?

落花浮王杯 提交于 2020-02-24 05:33:04
问题 I have a function which looks like this: template <typename T, std::size_t... I> std::ostream& vector_insert(std::ostream& lhs, const char* delim, const T& rhs, std::index_sequence<I...>) { std::ostream_iterator<float> it(lhs, delim); ((*it++ = at(rhs, I)), ...); return lhs; } This is my final attempt and I'm still failing on my expansion of the integer_sequence I'm hoping someone can tell me how to write a line that will effectively expand to: *it++ = at(rhs, 0U), *it++ = at(rhs, 1U), *it++

Variable template at class scope

余生颓废 提交于 2019-12-28 07:04:26
问题 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

Variable template at class scope

霸气de小男生 提交于 2019-12-28 07:04:18
问题 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

Forward declare a constexpr variable template

微笑、不失礼 提交于 2019-12-23 07:21:41
问题 I tried to forward-declare a constexpr variable template like this: template<typename> constexpr std::size_t iterator_category_value; The goal was to document that every specialization should be constexpr but I have to admit that I never checked whether it was legal or not and g++ was happy with it. However, when I tried to compile this spinnet with clang++ instead, I got the following error: error: default initialization of an object of const type 'const std::size_t' (aka 'const unsigned

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

与世无争的帅哥 提交于 2019-12-18 14:12:56
问题 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

Division in Variable Template Returns Zero in Visual Studio 2017

断了今生、忘了曾经 提交于 2019-12-14 01:29:01
问题 This is probably a visual-studio-2017 bug related to this question: Templated Variables Bug With Lambdas in Visual Studio? And as mentioned in the comments there seems to be optimizer related. Division in the definition of a variable template seems to have a bug in Visual Studio 2017. So this code for example: template <typename T> const T PI = std::acos(static_cast<T>(-1)); template <typename T> const T ONE_EIGHTY = 180; template <typename T> const T DEG_TO_RAD = PI<T> / ONE_EIGHTY<T>; int

Templated Variables Bug With Lambdas in Visual Studio?

天涯浪子 提交于 2019-12-10 12:48:21
问题 c++14 provides variable templates Which work fine in visual-studio-2017, but within lambdas they seem to fall apart. For example: template <typename T> const auto PI = std::acos(static_cast<T>(-1)); int main() { auto func = []() { cout << PI<float> << endl; }; func(); } On gcc 6.3 this outputs: 3.14159 On Visual Studio 2017 this outputs: 0.0 回答1: Wierd bug, but it seems to have a reliable workaround, which works for both, this case and the related case. In both cases forcefully activating

Type_traits *_v variable template utility order fails to compile

老子叫甜甜 提交于 2019-12-10 02:58:57
问题 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...>,