473,909 Members | 5,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

confused with SFINAE


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

template<typena me Tvoid func(T);
template<typena me 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 1834
Dilip wrote:
I am a little confused about the difference between SFINAE and
unambiguous overload resolution set. For ex:

template<typena me Tvoid func(T);
template<typena me 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
2705
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 to do this, using the SFINAE principle. Here is a simple example showing what I am trying to do: 1 #include <iostream.h> 2 #include <boost/utility/enable_if.hpp> 3
3
1891
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 example: #include <iostream> template <typename T> class IsClassT { private:
2
3469
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 achieve my ends, but I have hit a snag. If I've messed up somewhere, or there's another way to get what I'm after, I'd be greaful if someone could help me out. Here is a condensed version of my code, showing the problem. Note that I get the same...
6
2950
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 raw pointer at "the end of the arrow". For types that support the operator, I have a reasonable solution, but I'd like to have a null pointer returned if the operator isn't supported by a particular object.
3
424
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 can_use_f {
5
2085
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 issue any error. If I replace U with U(*), then the code compiles again. I couldn't make the sense out of it. What's the magic with (*)? Note that function<int>::yes returns correct result 0 even with U. Please help me out.
2
2480
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 represent (partly) the question. #include <iostream> template <class> void f(...)
35
2026
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
2934
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 instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing this that I'm using partial _function_ specialization. I'm sure I remember reading somewhere that
0
10037
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11348
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11052
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10540
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9727
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6140
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.