无效使用不完整的类型boost function_traits
invalid use of incomplete type boost function_traits
我试图使以下事情工作,但它无法编译。
// 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
知道为什么这仍然是错误的吗?
From docs: 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<>
等复合函数对象)没有实现是很有意义的
相关文章:
- 如何将类成员函数与param绑定为boost::function的右值
- 自 1.55 以来,boost::bind / boost::function 中的奇怪行为变化
- 将 boost::function 与指向派生类的共享指针的参数一起使用
- 将 boost::function 和 boost::bind 替换为模板
- 如何从“boost::function”中获取 C 指针(如 'var (*)(vo
- 如何使用 boost::bind 将静态成员函数绑定到 boost::function
- 以 C++11 方式重写 boost 风味代码(Boost.Bind、Boost.Function)
- Boost:bind and Boost::function
- 将模板函数分配给 boost::function
- boost::function 和 std::tr1::function 之间是否有重要的区别需要了解
- 将 boost::function 与实例方法一起使用
- 将 Python 函数作为 Boost.Function 参数发送
- 将 C++11 std::function 传递给采用 boost::function 的遗留函数是否安全?
- 将 boost::function 转换为 std::function
- 使用 boost::bind 和 boost::function 将成员函数分配给回调
- 将赋值运算符绑定到 boost::function 对象
- 如何在"boost::function"中访问类实例(对象)指针?
- llvm g++ and boost function
- Using boost::function and boost::bind
- 为什么使用boost::function调用函数对象时不需要类实例