By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

confused with SFINAE

P: n/a

I am a little confused about the difference between SFINAE and
unambiguous overload resolution set. For ex:

template<typename Tvoid func(T);
template<typename Tvoid func(T*);

now, func<intis going to be ambiguous. Ok so far so good.

Now:

template<int Nint g() { }
template<int* Pint g() { }

g<1>();

why does SFINAE apply in this case? The literal 1 is not convertible
to int* anyway, right?
I mean, shouldn't normal overload resolution rules automatically apply
in this case?

I am certain I am not understanding the correct purpose of invoking
SFINAE.

Jul 6 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Dilip wrote:
I am a little confused about the difference between SFINAE and
unambiguous overload resolution set. For ex:

template<typename Tvoid func(T);
template<typename Tvoid func(T*);

now, func<intis going to be ambiguous. Ok so far so good.

Now:

template<int Nint g() { }
template<int* Pint g() { }

g<1>();
You're walking the edge here since neither function returns a value.
>
why does SFINAE apply in this case?
Because if it didn't, an attempt to instantiate the latter function
with '1' and figure out what 'P' is would be a compilation failure
since, as you say, '1' is not convertible to a pointer. If SFINAE is
used, the compiler can proceed normally generating only the former
function, and safely skipping the latter.
The literal 1 is not convertible
to int* anyway, right?
Right.
I mean, shouldn't normal overload resolution rules automatically apply
in this case?
What overload resolution? The set of overloaded functions is formed
based on the compiler's ability to instantiate the functions from the
templates. Since it cannot instantiate the second template, the set
is only one function long. IOW, instantiation first, resolution later.
I am certain I am not understanding the correct purpose of invoking
SFINAE.
The purpose is to allow the compiler not choke if in some situations it
cannot determine what the template argument should be (either by deducing
the argument or when you supply it).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.