I looked at a few similar questions, like this one and this other one, for example, and I understand how to work with enable_if for member functions.
Here is a working example:
#include <iostream>
template <int size>
class Test
{
private:
constexpr static bool ENABLE = (size < 10);
public:
template <bool E = ENABLE, typename std::enable_if<E, int>::type = 0>
static int foo();
template <bool E = ENABLE, typename std::enable_if<!E, int>::type = 0>
constexpr static int foo();
};
template <int size>
template <bool E, typename std::enable_if<E, int>::type>
int Test<size>::foo()
{
return 7;
}
template <int size>
template <bool E, typename std::enable_if<!E, int>::type>
constexpr int Test<size>::foo()
{
return 12;
}
int main()
{
Test<5> v1;
Test<15> v2;
std::cout << v1.foo() << "\n";
std::cout << v2.foo() << "\n";
}
However, when I try to slightly modify the code to work for member variables, I get nasty redeclaration errors. Is this even possible to do with variables, am I just missing something simple?
Here is my problematic example code:
#include <iostream>
template <int size>
class Test
{
private:
constexpr static bool ENABLE = (size < 10);
public:
template <bool E = ENABLE, typename std::enable_if<E, int>::type = 0>
static int foo;
template <bool E = ENABLE, typename std::enable_if<!E, int>::type = 0>
constexpr static int foo = 12;
};
template <int size>
template <bool E, typename std::enable_if<E, int>::type>
int Test<size>::foo = 7;
template <int size>
template <bool E, typename std::enable_if<!E, int>::type>
constexpr int Test<size>::foo;
int main()
{
Test<5> v1;
Test<15> v2;
std::cout << v1.foo<> << "\n";
std::cout << v2.foo<> << "\n";
}
Thanks in advance, any help/guidance is appreciated!