A very strange problem that I have been struggling with for the past few hours (after solving 5-6 other problems with SFINAE, since I am new to it). Basically in the following code I want to have f()for all possible instances of the templates, but g()is only available when N == 2:
#include <type_traits>
#include <iostream>
template<typename T, int N>
class A
{
public:
void f(void);
void g(void);
};
template<typename T, int N>
inline void A<T, N>::f()
{
std::cout << "f()\n";
}
template<typename T, int N, typename std::enable_if<N == 2, void>::type* = nullptr>
inline void A<T, N>::g()
{
std::cout << "g()\n";
}
int main(int argc, char *argv[])
{
A<float, 2> obj;
obj.f();
obj.g();
return 0;
}
When I try to compile it, I get an error message with the presence of three template parameters instead of two. Then, after some testing, I decided to move the definition g()inside the definition itself A, for example:
#include <type_traits>
#include <iostream>
template<typename T, int N>
class A
{
public:
void f(void);
template<typename t = T, int n = N, typename std::enable_if<N == 2, void>::type* = nullptr>
void g()
{
std::cout << "g()\n";
}
};
template<typename T, int N>
inline void A<T, N>::f()
{
std::cout << "f()\n";
}
int main(int argc, char *argv[])
{
A<float, 2> obj;
obj.f();
obj.g();
return 0;
}
, . : ? , -, ? : A, ? ? 3 , +1 , A ?
, , , ? , , enable_if, nullptr, , , SO, .
, !!!