Reputation: 2326
Given:
class Hokey
{
public:
explicit C(int i): i_(i) { }
template<typename T>
T& render(T& t) { t = static_cast<T>(i_); return t; }
private:
unsigned i_;
};
If I try:
Hokey h(1);
string s;
h.render(s);
Codepad gives me an error on the static cast:
t.cpp: In member function 'T& Hokey::render(T&) [with T = std::string]':
t.cpp:21: instantiated from here
Line 11: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(unsigned int&)'
It seems like it should say that there is no Hokey::render
to match.
Of course, if I supply a valid overload, everything works. But given the code below, you uncomment the line, codepad chokes again:
string& render(string& s) const {
ostringstream out;
out << i_;
// s = out.str();
return s;
}
Doesn't SFINAE say that - in the first case - the problem within render should not be the error, rather the absence of a render that works should be the error?
Upvotes: 2
Views: 1421
Reputation: 504073
It's only not an error during overload resolution. In other words, it puts off giving you an error until it's sure the call definitely isn't going to work. After that, it's an error.
struct example
{
template <typename T>
static void pass_test(typename T::inner_type); // A
template <typename T>
static void pass_test(T); // B
template <typename T>
static void fail_test(typename T::inner_type); // C
};
int main()
{
// enumerates all the possible functions to call: A and B
// tries A, fails with error; error withheld to try others
// tries B, works without error; previous error ignored
example::pass_test(5);
// enumerates all the possible functions to call: C
// tries C, fails with error; error withheld to try others
// no other functions to try, call failed: emit error
example::fail_test(5);
}
It should also be noted that overload resolution (and therefore SFINAE) only looks at the function signature, not the definition. So this will always fail:
struct example_two
{
template <typename T>
static int fail_test(T x)
{
return static_cast<int>(x);
}
template <typename T>
static int fail_test(T x)
{
return boost::lexical_cast<int>(x);
}
};
int main()
{
example_two::fail_test("string");
}
There are no errors for either template substitution — for the function signatures — so both functions are okay to call, even though we know the first one will fail and the second won't. So this gives you an ambiguous function call error.
You can explicitly enable or disable functions with boost::enable_if
(or std::enable_if
in C++0x, equivalent to boost::enable_if_c
). For example, you might fix the previous example with:
struct example_two_fixed
{
template <typename T>
static boost::enable_if<boost::is_convertible<T, int>, int>
pass_test(T x) // AA
{
return static_cast<int>(x);
}
template <typename T>
static boost::disable_if<boost::is_convertible<T, int>, int>
pass_test(T x) // BB
{
return boost::lexical_cast<float>(x);
}
};
struct empty {} no_conversion;
int main()
{
// okay, BB fails with SFINAE error because of disable_if, does AA
example_two::pass_test(5);
// okay, AA fails with SFINAE error because of enable_if, does BB
example_two::pass_test("string");
// error, AA fails with SFINAE, does BB, fails because cannot lexical_cast
example_two::pass_test(no_conversion);
}
Upvotes: 7