C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SFINAE basic question.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Motti Lanzkron
Guest





PostPosted: Fri Aug 20, 2004 1:28 am    Post subject: SFINAE basic question. Reply with quote



Hi, I've been trying to understand the rules of SFINAE (Substitution
Failure Is Not An Error).

What's bothering me is that I can't figure out what constitutes an
"acceptable" failure and what doesn't.

Here's my first attempt at is_pod:

template <class T>
class is_pod {
// If T is a POD we can build a pod.
union pod {
T t;
char c[2]; // sizeof(pod) > 1
};

static T make();

static pod check(T);
static char check(...);
public:
enum { value = sizeof(check(make())) != 1 };
};


#include <string>
int main()
{
const bool True = is_pod<int>::value; // OK
const bool False = is_pod<std::string>::value; // Ooops
}

I think this failed because pod was instantiated in any case, which
led to my second try.

template <class T>
class is_pod {
template <class U>
union pod {
U u;
char c[2];
};
static T make();

template <class U>
static pod<U> check(U);
static char check(...);
public:
enum { value = sizeof(check(make())) != 1 };
};

I thought that if the template check method isn't used then no-one
will instantiate pod<std::string>, needless to say I was wrong.

I would appreciate an explanation of the scope of SFINAE or a pointer
to such an explanation (google, for once, wasn't very helpful).

Thanks, Motti.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Aug 20, 2004 10:17 am    Post subject: Re: SFINAE basic question. Reply with quote



* Motti Lanzkron:
Quote:
Hi, I've been trying to understand the rules of SFINAE (Substitution
Failure Is Not An Error).

Basing non-library code on SFINAE or any other generally less well-known
feature may cause maintainance problems.



Quote:
What's bothering me is that I can't figure out what constitutes an
"acceptable" failure and what doesn't.

I may be mistaken, but after browsing the Holy Standard I think the relevant
list is the one in §14.8.2/2.

This list has a number of invalid type constructs that makes template
function argument type deduction fail (hence this function disregarded?).

A union with non-POD member is unfortunately not in the list.



Quote:
Here's my first attempt at is_pod:

template <class T
class is_pod {
// If T is a POD we can build a pod.
union pod {
T t;
char c[2]; // sizeof(pod) > 1
};

static T make();

static pod check(T);
static char check(...);
public:
enum { value = sizeof(check(make())) != 1 };
};


For what it's worth, the following (which occurred during
"mix-and-match-perhaps-something-is-there-to-learned" testing, it's not
something that should work!) compiles cleanly, which it should not, and
produces "incorrect" result 11 with Visual C++ 7.1.

(There's finally a place to report bugs in Visual C++ 8.0, which is still in
beta (only a single ICE in the list, I don't believe it), but not so for 7.1
or earlier versions, which is all I and most others have: possibly most of
the good old been-there-always core bugs will continue in 8.0.)

That this code compiling cleanly means that even if I'm wrong about
§14.8.2/2 listing all cases, using the union-technique with MSVC may fail
because the compiler's very happy with a union of non-POD type.


template< class T >
static T make();

template <class U>
static int check(U const& v)
{
union pod {
U u;
char c[2];
} x;
return 0;
}

template <class U>
static char check(...);

template <class T>
class is_pod {
public:
enum { value = sizeof(check<T>(make<T>())) != 1 };
};

#include <string>
#include <iostream>
int main()
{
const bool True = is_pod<int>::value; // OK
const bool False = is_pod<std::string>::value; // Ooops
std::cout << True << False << std::endl;
}



Quote:
I thought that if the template check method isn't used then no-one
will instantiate pod

I think it has nothing to do with "isn't used" and much more to do with the
list in §14.8.2/2 being mechanically applied and resulting in "being used",
but I may be wrong.


Quote:
I would appreciate an explanation of the scope of SFINAE or a pointer
to such an explanation (google, for once, wasn't very helpful).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Motti Lanzkron
Guest





PostPosted: Sun Aug 22, 2004 10:47 am    Post subject: Re: SFINAE basic question. Reply with quote



Alf P. Steinbach wrote:
Quote:
* Motti Lanzkron:
Hi, I've been trying to understand the rules of SFINAE (Substitution
Failure Is Not An Error).

Basing non-library code on SFINAE or any other generally less well-known
feature may cause maintainance problems.



What's bothering me is that I can't figure out what constitutes an
"acceptable" failure and what doesn't.

I may be mistaken, but after browsing the Holy Standard I think the relevant
list is the one in §14.8.2/2.

This list has a number of invalid type constructs that makes template
function argument type deduction fail (hence this function disregarded?).

A union with non-POD member is unfortunately not in the list.

My copy of the standard seems to be out of date, could you please
quote the relevant section?

Thanks, Motti.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Aug 22, 2004 11:02 pm    Post subject: Re: SFINAE basic question. Reply with quote

* Motti Lanzkron:
Quote:
Alf P. Steinbach wrote:
* Motti Lanzkron:
Hi, I've been trying to understand the rules of SFINAE (Substitution
Failure Is Not An Error).

Basing non-library code on SFINAE or any other generally less well-known
feature may cause maintainance problems.



What's bothering me is that I can't figure out what constitutes an
"acceptable" failure and what doesn't.

I may be mistaken, but after browsing the Holy Standard I think the relevant
list is the one in §14.8.2/2.

This list has a number of invalid type constructs that makes template
function argument type deduction fail (hence this function disregarded?).

A union with non-POD member is unfortunately not in the list.

My copy of the standard seems to be out of date, could you please
quote the relevant section?

It can't be that out of date.

Anyway it's a bit much to quote, but summing up the list of invalid
constructs:


- array with zero or negative size.

- array with element type that is either 'void', function type,
reference,

- non-class type in qualified name,

- qualified name where specified member doesn't exist,

- qualified name where specified member isn't a type when it should be,

- pointer to reference.0

- reference to reference,

- reference to void,

- pointer to member of non-class T,

- invalid conversion (example is of converting int-value 1 to int*),

- attempt to create function type with parameter type void
(not sure I understand this one!),

- attempting to create a const/volatile-qualified function type.


And that's it -- no union of non-POD in there.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Tue Aug 24, 2004 10:42 pm    Post subject: Re: SFINAE basic question. Reply with quote

[email]alfps (AT) start (DOT) no[/email] (Alf P. Steinbach) wrote
Quote:
...summing up the list of invalid constructs:
....
- attempt to create function type with parameter type void
(not sure I understand this one!),
....


I think this means that a function type would look like this:

int (*foo)(int, void, double);
^^^^

I may be wrong.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.