0

I tried to make following things work but it failed to compile.

// T is a function with a callback, std::function<void(std::function<void (DataType)> >
struct doFunc {
    template<typename T>
    void operator()(T && func) {
        auto callback = [](typename function_traits<typename function_traits<T>::arg1_type >::arg1_type r) {
            std::cout << r;
        };
        func(callback);
    }
};

// usage
doFunc()([](std::function<void(string)> cb){
    cb("hello");
});

the error is:

invalid use of incomplete type 'struct boost::detail::function_traits_helper<SomeClass::TestBody()::<lambda(std::function<void(std::__cxx11::basic_string<char>)>)>*>'

Is this because the compiler cannot deduct the type out? If I want to keep the usage like that, How to fix it?

EDIT

I implemented similar thing without boost::function_traits but still has compile errors.

template <typename T>
struct SimpleTraits;

template <typename R>
struct SimpleTraits<std::function<void(R)>> {
    typedef R ARG_TYPE;

};

static_assert(std::is_same<SimpleTraits<std::function<void(int)>>::ARG_TYPE, int>::value, "int and int is same type");
static_assert(!std::is_same<SimpleTraits<std::function<void(int)>>::ARG_TYPE, std::string>::value, "int and string is not");

struct doFunc {
    template<typename T>
    void operator()(T && func) {
        typename SimpleTraits<T>::ARG_TYPE empty();
        func(empty);
    }
};

   doFunc()([](int a){
        cout << a;
    });

the error is still

error: invalid use of incomplete type

any idea why this is still wrong?

qqibrow
  • 2,942
  • 1
  • 24
  • 40

1 Answers1

1

From the docs: http://www.boost.org/doc/libs/1_59_0/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html

Tip:

function_traits is intended to introspect only C++ functions of the form R (), R( A1 ), R ( A1, ... etc. ) and not function pointers or class member functions. To convert a function pointer type to a suitable type use remove_pointer.

So, it makes a lot of sense it's not implemented for other types (such as composite function objects like std::function<>)

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • you are absolutely correct! I tried sth else but still failed. could you check the followup in my question? Thank you! – qqibrow Aug 30 '15 at 22:08
  • http://stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda – sehe Aug 30 '15 at 22:14