Take the following piece of code:
#include <type_traits>
#include <iostream>
template <class T>
void print(T &&t){
std::cout << t << std::endl;
}
template<class T, T val>
struct Foo{
static constexpr T value = val;
};
int main(){
print(Foo<int, 123>::value);
}
It refuses to compile under Clang 3.3 and GCC 4.8.1 ("undefined reference to Foo<int, 123>::value")
, which puzzles me because Foo
does exactly the same as std::integral_constant
, and the same code runs fine with integral_constant
. It also fails with plain lvalue reference in the print function. Any explanation regarding this behaviour?
The issue is also present with this quite minimal example:
template<class T>
struct Bar{
static const bool value = true;
};
As the compiler says, there is no reference to the static variable, you must add
template<class T, T val>
constexpr T Foo<T, val>::value;
after the definition of the class Foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With