Re: SFINAE
On Mar 28, 1:14 am, none <""johs\"@(none)"> wrote:
I am trying to understand SFINAE based on this wiki page:
You'd probably be better off using an orignal source. Say the
Jusuttis and Vandevoorde. The Wikipedia is NOT the place to
learn how to use technical concepts.
http://en.wikipedia.org/wiki/SFINAE
// Defines return type to be void for all types
// besides int.
template<typename T>
struct can_use_f {
typedef void type;
};
template<>
struct can_use_f<int> {
typedef int type;
};
// function with return type ::type.
template<typename T>
typename can_use_f<T>::type f(T const &);
int main() {
f(1);
f(1.);
}
There's no SFINAE in your example. SFINAE occurs during
template argument type deduction, and is used to eliminate
certain functions from the overload set (so they won't be
considered and chosen later). Since there's no failure to
deduce template arguments in your code, there's no SFINAE.
Note that your code doesn't correspond to that in the Wikipedia
article, since you've ensured that template argument deduction
will always work. (I'm not sure that the Wikipedia example is
well chosen, however. The usual use of SFINAE is exactly what
the article says in its first sentence: "used when resolving
overloaded functions or templates". Providing an example which
doesn't involve overloading, but uses SFINAE to provoke a later
error, seems rather contradictory. At the very least, some
explination as to how this is used for concept checking would
have been in order.)
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<double>::type
f<double>(double const&)'
collect2: ld returned 1 exit status
That's because you haven't provided an implementation for the
function.
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 i=
nt
(this does not work).
Of course it does. Just provide an implementation of the
function, and there's no problem. (Note that your error
messages come from the linker.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34