Boost logo

Boost Users :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2007-01-24 15:12:32


Pavel Antokolsky aka Zigmar wrote:
> Hello!
> I'm trying to use function_traits but it fails to compile. It seems
> like the add_pointer is somehow related to the error, as using the
> helper struct directly (and thus avoiding add_pointer on supplied
> type) compiles ok. I'm using boost 1.30.2 with g++ 3.2.2 on linux.
>
> The minimal example is:
> #include <boost/type_traits/function_traits.hpp>
>
> int foo() {}
>
> template<typename T>
> void bar(T f) {
> typedef typename boost::function_traits<T>::result_type X; //fails to compile
> typedef typename boost::detail::function_traits_helper<T>::result_type Y; //OK
>
> }
>
> int main(int argc, char** argv) {
> bar(foo);
> }
>

First of all, you really shouldn't be using anything in 'detail'
namespaces -- what's in there are implementation details which a) might
be removed at any time b) might be changed to behave differently at any
time c) might even be defined differently or not at all for some compilers.

'T' is deduced to a function /pointer/. You have to use the plain
function type (without the pointer on top) for 'function_traits' to work:

     typedef typename boost::function_traits<
         typename boost::remove_pointer<T>::type >::result_type X;

Note that 'function_traits' won't work with function objects (that is
clases with an overloaded operator()). Guessing from the code you posted
you might be better off using Boost.ResultOf:

     http://www.boost.org/libs/utility/utility.htm#result_of

Further, a library that provides a more complete set of traits classes
entirely dedicated to built-in functional types (and pointers/references
thereof) has been accepted into Boost, recently. It's not in the CVS yet
and can be downloaded from the Boost file vault:

     http://tinyurl.com/qaf5f - zip archive

Regards,
Tobias


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net