473,411 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 1800
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter Collingbourne | last post by:
Hello I am trying to do some template metaprogramming involving enabling or disabling a method in a parameterised class based on some condition. I am trying to use the Boost enable_if mechanism...
3
by: justin.adam.miller | last post by:
I've been trying to use the sfinae principle in some code and have been getting many compiler errors. So I decided to try a very simplified version to see if I had the idea correct. Here's the...
2
by: Clark S. Cox III | last post by:
I'm writing a class that, depending on a template parameter, has constructors that take differing numbers of arguments. I initially thought that I could use SFINAE (via boost::enable_if_c) to...
6
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the...
3
by: none | last post by:
I am trying to understand SFINAE based on this wiki page: http://en.wikipedia.org/wiki/SFINAE // Defines return type to be void for all types // besides int. template<typename T> struct...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
2
by: Barry | last post by:
The problem brought by one earlier post from comp.lang.c++ http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/ I take part of the question and reproduce the code to...
35
by: James Kanze | last post by:
Just ran into an interesting question concerning SFINAE. Given the following code: #include <iostream> #include <typeinfo> template< typename T > class P { public:
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.