libc ++中的std :: function回调

时间:2012-07-14 02:54:22

标签: c++ c++11 clang libc++ std-function

  

可能重复:
  Does std::function's copy-constructor require the template type's argument types to be complete types?

我有一个带有基于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>

时,此代码编译时没有问题

0 个答案:

没有答案