我有一个简单的类,它在 std::function 上构建了一个回调,但它不是用 C++11/libc++ 构建的。我想我看到了错误,但我不知道解决方案。
#include <functional>
struct Base {
typedef std::function<void( Base baseInstance )> Callback;
Base() {}
virtual ~Base() {}
void setCallBack( Callback callback ) { mCallback = callback; }
private:
Callback mCallback; // <--- Error, "Ivalid application of 'sizeof' to an incomplete type of 'Base'"
};
完整的错误日志:
In file included from /Volumes/ssd/code/sandbox/test_touchtracker_libcpp/test_touchtracker_libcpp/main.cpp:9:
In file included from /Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:38:
In file included from /Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ios:216:
In file included from /Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__locale:15:
In file included from /Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:434:
In file included from /Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:591:
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:2825:19: error: invalid application of 'sizeof' to an incomplete type 'Base'
static_assert(sizeof(_Tp) > 0, "Type must be complete.");
^~~~~~~~~~~
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:2812:15: note: in instantiation of template class 'std::__1::__check_complete<Base>' requested here
private __check_complete<_T0, _Tp...>
^
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:2978:15: note: in instantiation of template class 'std::__1::__check_complete<std::__1::function<void (Base)> &, Base>' requested here
: private __check_complete<_Fp, _Args...>
^
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:2989:11: note: in instantiation of template class 'std::__1::__invokable_imp<std::__1::function<void (Base)> &, Base>' requested here
__invokable_imp<_Fp, _Args...>::value>
^
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1115:33: note: in instantiation of template class 'std::__1::__invokable<std::__1::function<void (Base)> &, Base>' requested here
template <class _Fp, bool = __invokable<_Fp&, _ArgTypes...>::value>
^
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1163:9: note: in instantiation of default argument for '__callable<std::__1::function<void (Base)> >' required here
__callable<typename decay<_Fp>::type>::value,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode45-DP2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1166:7: note: while substituting deduced template arguments into function template 'operator=' [with _Fp = const std::__1::function<void (Base)> &]
operator=(_Fp&&);
^
/Volumes/ssd/code/sandbox/test_touchtracker_libcpp/TouchTracker.h:5:8: note: definition of 'Base' is not complete until the closing '}'
struct Base {
^
1 error generated.
因此, std::function 并不完整。但我希望这个类能够引用一个回调,这个类。所以我有点苦恼。有人知道如何用 C++11 处理这个问题吗?
注意:此代码在使用时编译没有问题<tr1/functional>