468,456 Members | 1,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

confused with SFINAE


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
1 1602
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.

Similar topics

8 posts views Thread by Peter Collingbourne | last post: by
3 posts views Thread by justin.adam.miller | last post: by
2 posts views Thread by Clark S. Cox III | last post: by
6 posts views Thread by edd | last post: by
3 posts views Thread by none | last post: by
5 posts views Thread by Fei Liu | last post: by
35 posts views Thread by James Kanze | last post: by
reply views Thread by greek_bill | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.