How to determine if a function with a given signature is declared?

Hi, Newbie question follows... I've used BOOST_MPL_HAS_XXX_TRAIT_DEF in the past but recently needed to determine if a function was defined with a given signature. Searching boost::mpl, stackoverflow etc, I couldn't find anything to do what I needed. I've attached my solution below. Basically, I'm wondering if there is a boost::mpl solution or some other more elegant solution. thanks, andrew #include <boost/mpl/bool.hpp> #include <boost/mpl/assert.hpp> #define XXX_DEFINED(symbol) \ template <typename T, T* = symbol> \ struct check_ ## symbol \ { \ typedef void type; \ }; \ \ template <typename T, typename enable = void> \ struct symbol ## _defined : \ boost::mpl::false_ \ { }; \ \ template <typename T> \ struct symbol ## _defined<T, typename check_ ## symbol<T>::type > : \ boost::mpl::true_ \ { }; // declare a some_func(int) so we can see if its declared... void some_func(int) {} // defines some_func_defined template XXX_DEFINED(some_func); BOOST_MPL_ASSERT(( some_func_defined<void(int)> )); // some_func(double) hasn't been declared BOOST_MPL_ASSERT_NOT(( some_func_defined<void(double)> ));

andrew.beck wrote:
Hi, Newbie question follows...
I've used BOOST_MPL_HAS_XXX_TRAIT_DEF in the past but recently needed to determine if a function was defined with a given signature. Searching boost::mpl, stackoverflow etc, I couldn't find anything to do what I needed. I've attached my solution below. Basically, I'm wondering if there is a boost::mpl solution or some other more elegant solution.
Maybe Type Traits Introspection on the review schedule could help yuu (http://www.boost.org/community/review_schedule.html) Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/How-to-determine-if-a-function-with-a-giv... Sent from the Boost - Dev mailing list archive at Nabble.com.

On 5/17/2011 6:06 PM, Vicente Botet wrote:
andrew.beck wrote:
Hi, Newbie question follows...
I've used BOOST_MPL_HAS_XXX_TRAIT_DEF in the past but recently needed to determine if a function was defined with a given signature. Searching boost::mpl, stackoverflow etc, I couldn't find anything to do what I needed. I've attached my solution below. Basically, I'm wondering if there is a boost::mpl solution or some other more elegant solution.
Maybe Type Traits Introspection on the review schedule could help yuu (http://www.boost.org/community/review_schedule.html)
Only if the function is within a type as a member function or static member function.

Edward Diener-3 wrote:
On 5/17/2011 6:06 PM, Vicente Botet wrote:
andrew.beck wrote:
Hi, Newbie question follows...
I've used BOOST_MPL_HAS_XXX_TRAIT_DEF in the past but recently needed to determine if a function was defined with a given signature. Searching boost::mpl, stackoverflow etc, I couldn't find anything to do what I needed. I've attached my solution below. Basically, I'm wondering if there is a boost::mpl solution or some other more elegant solution.
Maybe Type Traits Introspection on the review schedule could help yuu (http://www.boost.org/community/review_schedule.html)
Only if the function is within a type as a member function or static member function.
Wow, I missed this point. For example, Boost.Thread make use of some traits of this kind. I guess that it should be no too much work to provide the equivalent for free functions; isn't it? Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/How-to-determine-if-a-function-with-a-giv... Sent from the Boost - Dev mailing list archive at Nabble.com.

On 5/18/2011 1:36 AM, Vicente Botet wrote:
Edward Diener-3 wrote:
On 5/17/2011 6:06 PM, Vicente Botet wrote:
andrew.beck wrote:
Hi, Newbie question follows...
I've used BOOST_MPL_HAS_XXX_TRAIT_DEF in the past but recently needed to determine if a function was defined with a given signature. Searching boost::mpl, stackoverflow etc, I couldn't find anything to do what I needed. I've attached my solution below. Basically, I'm wondering if there is a boost::mpl solution or some other more elegant solution.
Maybe Type Traits Introspection on the review schedule could help yuu (http://www.boost.org/community/review_schedule.html)
Only if the function is within a type as a member function or static member function.
Wow, I missed this point. For example, Boost.Thread make use of some traits of this kind. I guess that it should be no too much work to provide the equivalent for free functions; isn't it?
If it can be done it should be a separate library. My library is Type Traits Introspection, meaning it introspects a type.

On Sun, May 22, 2011 at 2:34 PM, Edward Diener <eldiener@tropicsoft.com> wrote:
On 5/18/2011 1:36 AM, Vicente Botet wrote:
Edward Diener-3 wrote:
Wow, I missed this point. For example, Boost.Thread make use of some traits of this kind. I guess that it should be no too much work to provide the equivalent for free functions; isn't it?
If it can be done it should be a separate library.
My library is Type Traits Introspection, meaning it introspects a type.
I would say a function signature is a type. At least it is once it exists. You can argue about whether a type_exists<Type>::value trait makes sense in general. Tony

On 5/22/2011 10:15 PM, Gottlob Frege wrote:
On Sun, May 22, 2011 at 2:34 PM, Edward Diener<eldiener@tropicsoft.com> wrote:
On 5/18/2011 1:36 AM, Vicente Botet wrote:
Edward Diener-3 wrote:
Wow, I missed this point. For example, Boost.Thread make use of some traits of this kind. I guess that it should be no too much work to provide the equivalent for free functions; isn't it?
If it can be done it should be a separate library.
My library is Type Traits Introspection, meaning it introspects a type.
I would say a function signature is a type. At least it is once it exists. You can argue about whether a type_exists<Type>::value trait makes sense in general.
There is already a library, called function_types, for introspecting a function signature.

On 23/05/11 12:41, Edward Diener wrote:
On 5/22/2011 10:15 PM, Gottlob Frege wrote:
On Sun, May 22, 2011 at 2:34 PM, Edward Diener<eldiener@tropicsoft.com> wrote:
On 5/18/2011 1:36 AM, Vicente Botet wrote:
Edward Diener-3 wrote:
Wow, I missed this point. For example, Boost.Thread make use of some traits of this kind. I guess that it should be no too much work to provide the equivalent for free functions; isn't it?
If it can be done it should be a separate library.
My library is Type Traits Introspection, meaning it introspects a type.
I would say a function signature is a type. At least it is once it exists. You can argue about whether a type_exists<Type>::value trait makes sense in general.
There is already a library, called function_types, for introspecting a function signature.
Well this is different. But basically, you can use the old SFINAE trick by trying to detect the function f you want and have a f function living in the global namespace with a (...) prototype. This alas require a macro taking function name and prototype.
participants (5)
-
andrew.beck@astc-design.com
-
Edward Diener
-
Gottlob Frege
-
Joel falcou
-
Vicente Botet