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 

detecting existence of a member function (SFINAE ?)

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





PostPosted: Mon Mar 14, 2005 12:04 am    Post subject: detecting existence of a member function (SFINAE ?) Reply with quote



I thought I remembered seeing some application of SFINAE that could detect
whether a class had a given member function.
but I can't find any evidence of it now.. and my short experiments didnt
bring much of an answer.
Isn't it feasible ? anyone has pointers about that ?

--
Samuel.Krempp
cout << "@" << "crans." << "ens-cachan.fr" << endl;


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

Back to top
David Abrahams
Guest





PostPosted: Mon Mar 14, 2005 8:58 am    Post subject: Re: detecting existence of a member function (SFINAE ?) Reply with quote



Samuel Krempp <krempp (AT) crans (DOT) ens-cachan.fr> writes:

Quote:
I thought I remembered seeing some application of SFINAE that could detect
whether a class had a given member function.
but I can't find any evidence of it now.. and my short experiments didnt
bring much of an answer.
Isn't it feasible ? anyone has pointers about that ?

#include <boost/mpl/has_xxx.hpp>
#include <boost/static_assert.hpp>
#include <utility>

BOOST_MPL_HAS_XXX_TRAIT_DEF(first_type)
BOOST_STATIC_ASSERT((has_first_type<std::pair::value));

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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

Back to top
Jonathan Turkanis
Guest





PostPosted: Mon Mar 14, 2005 9:00 am    Post subject: Re: detecting existence of a member function (SFINAE ?) Reply with quote



Samuel Krempp wrote:
Quote:
I thought I remembered seeing some application of SFINAE that could
detect whether a class had a given member function.
but I can't find any evidence of it now.. and my short experiments
didnt bring much of an answer.
Isn't it feasible ? anyone has pointers about that ?

See these threads from the Boost developers list:

http://lists.boost.org/MailArchives/boost/msg76679.php
http://lists.boost.org/MailArchives/boost/msg55484.php

And this thread from comp.lang.c++:

http://groups-beta.google.com/group/comp.lang.c++/msg/ec08415ef5b4b0d6

Jonathan



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


Back to top
Maxim Yegorushkin
Guest





PostPosted: Mon Mar 14, 2005 9:05 am    Post subject: Re: detecting existence of a member function (SFINAE ?) Reply with quote

Samuel Krempp <krempp (AT) crans (DOT) ens-cachan.fr> wrote:

Quote:
I thought I remembered seeing some application of SFINAE that could
detect
whether a class had a given member function.
but I can't find any evidence of it now.. and my short experiments didnt
bring much of an answer.
Isn't it feasible ? anyone has pointers about that ?

Something along these lines:

#include <iostream>

template <class T, T val>
struct member_wrapper{};

template <class T>
char test_for_swap(member_wrapper<void (T::*)(T&), &T::swap>* p);

template <class T>
double test_for_swap(...);

template <class T>
struct has_member_swap
{
static const bool value = (1 == sizeof(test_for_swap<T>(0)));
};

struct A {};
struct B { void swap(B&); };

int main()
{
using namespace std;

cout << has_member_swap cout << has_member_swap }

--
Maxim Yegorushkin

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

Back to top
Vladimir Marko
Guest





PostPosted: Mon Mar 14, 2005 1:31 pm    Post subject: Re: detecting existence of a member function (SFINAE ?) Reply with quote

Samuel Krempp wrote:
Quote:
I thought I remembered seeing some application of SFINAE that could
detect
whether a class had a given member function.
but I can't find any evidence of it now.. and my short experiments
didnt
bring much of an answer.
Isn't it feasible ? anyone has pointers about that ?

If you know the name and the _exact signature_ of the function
it is possible. If you are just trying to find out if there
is an unambigous member function with a given name that can
be called with a given set of arguments you will soon run into
DR339:

http://www2.open-std.org/jtc1/sc22/WG21/docs/cwg_active.html#339

I didn't find any workaroud, but it may still exist.

So, for the easy case:

struct no { };
struct yes { no no_[2]; };

template <typename T,void (T::*)()> // exact signature
struct yes_holder { typedef yes type; };

no tester(...);

template <typename T>
typename yes_holder<T,&T::foo>::type tester(const T&);

// test:
struct s1 { };
struct s2 { void foo() { } };

static const bool s1_has_foo= sizeof(yes)==sizeof(tester(s1()));
static const bool s2_has_foo= sizeof(yes)==sizeof(tester(s2()));

#include <iostream>
#include <ostream>

int main(){
std::cout << s1_has_foo << " " << s2_has_foo << std::endl;
}

Note, however, that this is not very useful. A template like

template struct has_foo{
static const bool value= sizeof(yes)==sizeof(tester(*(T*)0));
};

would be much better, but according to April 2003 Note from
DR339 even this should be invalid. On the other hand, N1705
(quoted in March 2005 Note in DR339) suggests a different
resolution.

For now, we have to work with existing compilers, so let's
also talk about what the compilers say if we add the template
has_foo to the program and
std::cout << has_foo std::endl;
to the main(). gcc 3.3 fails to apply SFINAE in instantiation
of has_foo accept the code and give correct results. Comeau C/C++ Online
issues an "NULL reference is not allowed" in instantiation
of has_foo<s2> which is really funny since the NULL reference
is in a sizeof expression.

Regards,
Vladimir Marko


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

Back to top
Samuel Krempp
Guest





PostPosted: Mon Mar 14, 2005 9:26 pm    Post subject: Re: detecting existence of a member function (SFINAE ?) Reply with quote

Vladimir Marko wrote:
Quote:
If you know the name and the _exact signature_ of the function
it is possible. If you are just trying to find out if there
is an unambigous member function with a given name that can
be called with a given set of arguments you will soon run into
DR339:

http://www2.open-std.org/jtc1/sc22/WG21/docs/cwg_active.html#339

I didn't find any workaroud, but it may still exist.

So, for the easy case:

struct no { };
struct yes { no no_[2]; };

template <typename T,void (T::*)()> // exact signature
struct yes_holder { typedef yes type; };

no tester(...);

template <typename T
typename yes_holder

thanks for the code, and the precisions on the situation with overload
resolution. Even if it requires the exact prototype, it can make things a
little more transparent in my situation.

this kind of techniques really takes some getting used to, tricking the
compiler to work for us this way isn't very intuitive IMO :)

--
Sam

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

Back to top
David Abrahams
Guest





PostPosted: Mon Mar 14, 2005 9:33 pm    Post subject: Re: detecting existence of a member function (SFINAE ?) Reply with quote

"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> writes:

Quote:
Samuel Krempp wrote:
I thought I remembered seeing some application of SFINAE that could
detect whether a class had a given member function.
but I can't find any evidence of it now.. and my short experiments
didnt bring much of an answer.
Isn't it feasible ? anyone has pointers about that ?

See these threads from the Boost developers list:

http://lists.boost.org/MailArchives/boost/msg76679.php
http://lists.boost.org/MailArchives/boost/msg55484.php

And this thread from comp.lang.c++:

http://groups-beta.google.com/group/comp.lang.c++/msg/ec08415ef5b4b0d6

Whoops; I didn't read your question carefully enough. What I posted
only finds member types. Jonathan gave the right references.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ 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.