【问题标题】:tr1::functional error with g++ 4.8.1g++ 4.8.1 的 tr1::functional 错误
【发布时间】:2013-09-23 12:00:26
【问题描述】:

代码:

    #include <tr1/functional>

    class Test
    {
    public:
        Test() { ; }
        virtual void foo() = 0;
    };

    void someFunc(Test& j)
    {
        j.foo();
    }

    void func(Test& j)
    {
       std::tr1::bind(someFunc, std::tr1::ref(j));
    }

在Linux上使用g++ 4.8.1,用--std=c++11编译我得到:

    In file included from foo.cpp:1:0:
    /usr/include/c++/4.8.1/tr1/functional: In instantiation of ‘class std::tr1::reference_wrapper<Test>’:
    foo.cpp:17:44:   required from here
    /usr/include/c++/4.8.1/tr1/functional:495:9: error: cannot allocate an object of abstract type ‘Test’
             operator()(_Args&... __args) const
             ^
    foo.cpp:3:7: note:   because the following virtual functions are pure within ‘Test’:
     class Test
           ^
    foo.cpp:7:18: note:     virtual void Test::foo()
         virtual void foo() = 0;
                      ^

这似乎没有任何意义。使用相应的 boost 类可以正常工作。有人可以确认这是 G++ 4.8.1 中的 TR1 错误吗?

【问题讨论】:

  • 适用于 GCC 4.8 上的非 TR1 std:: C++11 等效项。
  • @stefan, main() 不需要编译它,只有当你尝试链接它时。
  • 如果您可以访问 GCC4.8(即支持 C++11 的编译器),您为什么要使用技术报告?
  • @stefan,如果你只是添加main(),运行代码什么都不会告诉你,你仍然不执行func。问题是关于编译错误,而不是运行时行为。

标签: c++ c++11 tr1 g++4.8


【解决方案1】:

libstdc++ tr1::reference_wrapper 实现有这个:

  template<typename... _Args>
    typename result_of<_M_func_type(_Args...)>::type
    operator()(_Args&... __args) const
    {
      return __invoke(get(), __args...);
    }

result_of 表达式使用了一个按值的_M_func_type 参数(这是reference_wrapperTest 的模板参数),所以它尝试形成函数类型Test(),它使用了一个by -value Test 返回类型,对于不完整或抽象类型无效。我想我很久以前就为std::reference_wrapper 修复了这个问题,它需要使用result_of&lt;_M_func_type&amp;(Args...)&gt;

libstdc++ 中的 TR1 实现不再真正维护 - TR1 达到了它的目的,但它的时代已经过去了。

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多