【问题标题】:invalid use of incomplete type boost function_traits [duplicate]无效使用不完整类型 boost function_traits [重复]
【发布时间】:2015-11-24 11:30:29
【问题描述】:

我尝试使以下内容正常工作,但编译失败。

// 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");
});

错误是:

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

这是因为编译器无法扣除类型吗?如果我想保持这样的用法,如何解决?

编辑

我在没有 boost::function_traits 的情况下实现了类似的东西,但仍然有编译错误。

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;
    });

错误依旧

error: invalid use of incomplete type

知道为什么这仍然是错误的吗?

【问题讨论】:

    标签: c++ c++11 boost metaprogramming template-meta-programming


    【解决方案1】:

    来自文档:http://www.boost.org/doc/libs/1_59_0/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html

    提示:

    function_traits 旨在内省 R ()R( A1 )R ( A1, ... etc. ) 形式的 C++ 函数,而不是函数指针或类成员函数。要将函数指针类型转换为合适的类型,请使用 remove_pointer。

    因此,它没有为其他类型实现(例如像std::function&lt;&gt; 这样的复合函数对象)很有意义

    【讨论】:

    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 2017-10-24
    • 2010-10-13
    • 2018-12-17
    • 2013-05-19
    • 2016-04-06
    • 2017-07-28
    相关资源
    最近更新 更多