Is there interest in a library for testing existence of a function member of class?

Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :) Synopsis class Bar { public: Bar(int); }; class Foo { public: Foo(int); int foo(Bar); }; // create struct 'has_foo' which is used to test function member 'foo'.HAS_FUNCTION_MEMBER(foo) int main() { assert((!has_foo<void(Bar::*)(Bar)>::value)); assert((has_foo<int(Foo::*)(Bar)>::value)); return 0; } The code is in my gisthttps://gist.github.com/wehu/1987f716074904d26cf0 The code only checks if the function is compatible with expected, but not require to be exact same. Thanks,Wei

Hi Wei, Have you checked Boost Concept Check library, it looks like it could fit your use case. Regards, Sylvain On 22 Nov 2014 22:33, "胡维" <huwei04@hotmail.com> wrote:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :) Synopsis class Bar { public: Bar(int); };
class Foo { public: Foo(int); int foo(Bar); };
// create struct 'has_foo' which is used to test function member 'foo'.HAS_FUNCTION_MEMBER(foo)
int main() { assert((!has_foo<void(Bar::*)(Bar)>::value)); assert((has_foo<int(Foo::*)(Bar)>::value)); return 0; } The code is in my gisthttps://gist.github.com/wehu/1987f716074904d26cf0 The code only checks if the function is compatible with expected, but not require to be exact same. Thanks,Wei
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Thanks, Sylvain to point to that. :)The use case is similar and a little different.Boost Concept Check will fail if not meet requirements.I would like a way to test it and return boolean. For example, I would like do something like below: struct default_foo{ void foo();}; struct user_foo{ void foo();}; template <typename T>void call_foo(typename if_<has_foo<void(T::*)()>::value, T, default_foo>::type u){ u.foo();} Thanks,Wei
Date: Sat, 22 Nov 2014 22:44:49 +0800 From: sylvain.bougerel.devel@gmail.com To: boost@lists.boost.org Subject: Re: [boost] Is there interest in a library for testing existence of a function member of class?
Hi Wei,
Have you checked Boost Concept Check library, it looks like it could fit your use case.
Regards, Sylvain On 22 Nov 2014 22:33, "胡维" <huwei04@hotmail.com> wrote:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :) Synopsis class Bar { public: Bar(int); };
class Foo { public: Foo(int); int foo(Bar); };
// create struct 'has_foo' which is used to test function member 'foo'.HAS_FUNCTION_MEMBER(foo)
int main() { assert((!has_foo<void(Bar::*)(Bar)>::value)); assert((has_foo<int(Foo::*)(Bar)>::value)); return 0; } The code is in my gisthttps://gist.github.com/wehu/1987f716074904d26cf0 The code only checks if the function is compatible with expected, but not require to be exact same. Thanks,Wei
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

2014-11-22 17:36 GMT+08:00 胡维 <huwei04@hotmail.com>:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :)
Yes, it's in Boost.TTI: http://tinyurl.com/k3y5x8l

oh, thanks! That's almost same as mine. :) Wei
Date: Sat, 22 Nov 2014 23:36:48 +0800 From: tongari95@gmail.com To: boost@lists.boost.org Subject: Re: [boost] Is there interest in a library for testing existence of a function member of class?
2014-11-22 17:36 GMT+08:00 胡维 <huwei04@hotmail.com>:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :)
Yes, it's in Boost.TTI: http://tinyurl.com/k3y5x8l
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

TONGARI J wrote:
2014-11-22 17:36 GMT+08:00 ?? <huwei04@hotmail.com>:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :)
Yes, it's in Boost.TTI: http://tinyurl.com/k3y5x8l
Has anyone ever worked out how to do this without having to specify the type? E.g. with BOOST_TTI_HAS_MEMBER_DATA I need to write something like has_member_data_fieldname<int ClasdName::*>::value; The most recent time I needed something like this, I wanted to test whether a struct contained a field with a particular name that was convertible to a float (I think float or int16_t in that case). I would have been happy with a test for a field with that name of any type. But I couldn't work out how to do it. Any ideas? Regards, Phil.

Phil Endecott <spam_from_boost_dev <at> chezphil.org> writes:
Has anyone ever worked out how to do this without having to specify the type? E.g. with BOOST_TTI_HAS_MEMBER_DATA I need to write something like
has_member_data_fieldname<int ClasdName::*>::value;
The most recent time I needed something like this, I wanted to test whether a struct contained a field with a particular name that was convertible to a float (I think float or int16_t in that case). I would have been happy with a test for a field with that name of any type. But I couldn't work out how to do it. Any ideas?
In C++11, decltype and expression SFINAE make this trait very trivial. (decltype is enough to not need the signature.) Regards

TONGARI J wrote:
2014-11-22 17:36 GMT+08:00 ?? <huwei04@hotmail.com>:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :)
Yes, it's in Boost.TTI: http://tinyurl.com/k3y5x8l
Has anyone ever worked out how to do this without having to specify the type? E.g. with BOOST_TTI_HAS_MEMBER_DATA I need to write something like has_member_data_fieldname<int ClasdName::*>::value; The most recent time I needed something like this, I wanted to test whether a struct contained a field with a particular name that was convertible to a float (I think float or int16_t in that case). I would have been happy with a test for a field with that name of any type. But I couldn't work out how to do it. Any ideas? Regards, Phil.

The most recent time I needed something like this, I wanted to test whether a struct contained a field with a particular name that was convertible to a float (I think float or int16_t in that case). I would have been happy with a test for a field with that name of any type. But I couldn't work out how to do it. Any ideas?
I think below code(incomplete) can test a field with a name of any type. template <int c>struct enable{ typedef void type;}; template <typename T>struct wrapper{ static T value;}; template <typename T, typename E=void>struct has_foo_impl{ enum { value = false };}; template <typename T>struct has_foo_impl<const T, typename enable<sizeof(wrapper<T>::value.foo, 1)>::type>{ enum { value = true };}; template <typename T>struct has_foo : has_foo_impl<const T> {}; struct a{};struct b{ int foo; }; #include <iostream> int main (){ std::cout << has_foo<a>::value << std::endl; std::cout << has_foo<b>::value << std::endl; return 0;}

I
To: boost@lists.boost.org Date: Sat, 22 Nov 2014 17:58:09 +0000 From: spam_from_boost_dev@chezphil.org Subject: Re: [boost] Is there interest in a library for testing existence of a function member of class?
TONGARI J wrote:
2014-11-22 17:36 GMT+08:00 ?? <huwei04@hotmail.com>:
Hi, I am looking for how to test if a function member of class exist or not in compile time.I did not find a solution by google so I wrote a piece of code for it.Please let me know if it's already in boost. :)
Yes, it's in Boost.TTI: http://tinyurl.com/k3y5x8l
Has anyone ever worked out how to do this without having to specify the type? E.g. with BOOST_TTI_HAS_MEMBER_DATA I need to write something like
has_member_data_fieldname<int ClasdName::*>::value;
The most recent time I needed something like this, I wanted to test whether a struct contained a field with a particular name that was convertible to a float (I think float or int16_t in that case). I would have been happy with a test for a field with that name of any type. But I couldn't work out how to do it. Any ideas?
Regards, Phil.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Yes, it's in Boost.TTI: http://tinyurl.com/k3y5x8l
Has anyone ever worked out how to do this without having to specify the type? E.g. with BOOST_TTI_HAS_MEMBER_DATA I need to write something like
has_member_data_fieldname<int ClasdName::*>::value;
The most recent time I needed something like this, I wanted to test whether a struct contained a field with a particular name that was convertible to a float (I think float or int16_t in that case). I would have been happy with a test for a field with that name of any type. But I couldn't work out how to do it. Any ideas?
I just realized that BOOST_TTI_HAS_MEMBER_FUNCTION is not exactly same as mine.BOOST_TTI_HAS_MEMBER_FUNCTION will check the type signature and only return true if it's exactly match. I would like to test if a member function of class is 'applicable'. For example, below code will print 0, but I expect to get 1.BOOST_TTI_HAS_MEMBER_FUNCTION(foo) class a{public: int foo(int);}; int main(){ std::cout << has_member_function_foo<int (a::*)(double)>::value << std::endl; return 0;}

胡维 wrote
Has anyone ever worked out how to do this without having to specify the type? E.g. with BOOST_TTI_HAS_MEMBER_DATA I need to write something like
has_member_data_fieldname
<int ClasdName::*> ::value;
I had that same need and could not find anything suitable... had to come up with my own (sort of). You might be interested in "is_callable" trait and BOOST_HAS_MEMBER macro deployment in my boost/convert. Right now it's in the development branch only. https://github.com/boostorg/convert/tree/develop -- View this message in context: http://boost.2283326.n4.nabble.com/Is-there-interest-in-a-library-for-testin... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (6)
-
Adam Merz
-
Phil Endecott
-
Sylvain Bougerel
-
TONGARI J
-
Vladimir Batov
-
胡维