I am trying to understand SFINAE based on this wiki page:
// Defines return type to be void for all types
// besides int.
template<typena me T>
struct can_use_f {
typedef void type;
};
template<>
struct can_use_f<int{
typedef int type;
};
// function with return type ::type.
template<typena me T>
typename can_use_f<T>::t ype f(T const &);
int main() {
f(1);
f(1.);
}
I have made a slight modification so if f(1) is called f should have
return type int. But it gives the error:
/tmp/cclJK0dJ.o: In function `main':
bob.cpp:(.text+ 0x95): undefined reference to `can_use_f<int> ::type
f<int>(int const&)'
bob.cpp:(.text+ 0xa5): undefined reference to `can_use_f<doub le>::type
f<double>(doubl e const&)'
collect2: ld returned 1 exit status
Is this the correct execution steps:
1) f(T const &) is called.
2) the return type is decided based on the template parameter.
3) if T != int return type is void. If T = int return type should be int
(this does not work).
// Defines return type to be void for all types
// besides int.
template<typena me T>
struct can_use_f {
typedef void type;
};
template<>
struct can_use_f<int{
typedef int type;
};
// function with return type ::type.
template<typena me T>
typename can_use_f<T>::t ype f(T const &);
int main() {
f(1);
f(1.);
}
I have made a slight modification so if f(1) is called f should have
return type int. But it gives the error:
/tmp/cclJK0dJ.o: In function `main':
bob.cpp:(.text+ 0x95): undefined reference to `can_use_f<int> ::type
f<int>(int const&)'
bob.cpp:(.text+ 0xa5): undefined reference to `can_use_f<doub le>::type
f<double>(doubl e const&)'
collect2: ld returned 1 exit status
Is this the correct execution steps:
1) f(T const &) is called.
2) the return type is decided based on the template parameter.
3) if T != int return type is void. If T = int return type should be int
(this does not work).
Comment