This program demonstrate two tries to use SFINAE. One works as expected and the other doesn't:
#include <iostream>
using namespace std;
struct Example {
typedef int type;
};
template <typename T>
void foo(typename T::type t) {
cout << "Function 1 is called" << endl;
}
template <typename T>
void foo(T t) {
cout << "Function 2 is called" << endl;
}
class Stream {
public:
template <typename T>
Stream& operator <<(typename T::type t) {
cout << "Function 3 is called" << endl;
return *this;
}
template <typename T>
Stream& operator <<(T t) {
cout << "Function 4 is called" << endl;
return *this;
}
};
int main()
{
// SFINAE works as expected
foo<Example>(10); // Prints "Function 1 is called"
foo<int>(10); // Prints "Function 2 is called"
// SFINAE doesn't work as expected
Stream strm;
strm << Example(); // Prints "Function 4 is called"
strm << 10; // Prints "Function 4 is called"
return 0;
}
The output is, as noted:
Function 1 is called
Function 2 is called
Function 4 is called
Function 4 is called
Can someone please explain why the line
strm << Example();
doesn't print "Function 3 is called"? and what should I do to make it happen?