【问题标题】:-std=c++0x causing returning reference to temporary with boost 1.64-std=c++0x 导致使用 boost 1.64 返回对临时的引用
【发布时间】:2017-10-27 21:08:18
【问题描述】:

以下示例结果:

boost/bind/bind.hpp:882: 错误:返回对临时的引用

如果使用 -std=c++0x 编译。

如果在没有 -std=c++0x 标志的情况下构建,则不会出现警告。尽管 gcc 4.4 不完全支持它,但似乎 boost 正在尝试在这里使用 c++11 右值。提前致谢!

gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)

完整的构建线: g++ -std=c++0x -Wall -Werror -I/pathtoboost/include main.cpp -o bind

#include <boost/function.hpp>
#include <boost/bind.hpp>

#include <iostream>

int foo( int a )
{
    std::cout << "a: " << a << std::endl;
    return a;
}

int main( int argc, char** argv )
{
    foo( 5 );
    boost::function< int( int ) > bfoo = boost::bind( foo, _1 );
    bfoo( 10 );
    return 0;
}

完整构建错误(警告):

cc1plus: warnings being treated as errors
In file included from /boost/include/boost/bind.hpp:22,
                 from main.cpp:2:
/boost/include/boost/bind/bind.hpp: In member function ‘A1&& boost::_bi::rrlist1<A1>::operator[](boost::arg<1> (*)()) const [with A1 = int]’:
/boost/include/boost/bind/bind.hpp:249:   instantiated from ‘R boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = int, F = int (*)(int), A = boost::_bi::rrlist1<int>, A1 = boost::arg<1>]’
/boost/include/boost/bind/bind.hpp:1306:   instantiated from ‘typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&&) [with A1 = int, R = int, F = int (*)(int), L = boost::_bi::list1<boost::arg<1> >]’
/boost/include/boost/function/function_template.hpp:138:   instantiated from ‘static R boost::detail::function::function_obj_invoker1<FunctionObj, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::_bi::bind_t<int, int (*)(int), boost::_bi::list1<boost::arg<1> > >, R = int, T0 = int]’
/boost/include/boost/function/function_template.hpp:925:   instantiated from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = boost::_bi::bind_t<int, int (*)(int), boost::_bi::list1<boost::arg<1> > >, R = int, T0 = int]’
/boost/include/boost/function/function_template.hpp:716:   instantiated from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<(! boost::is_integral::value), int>::type) [with Functor = boost::_bi::bind_t<int, int (*)(int), boost::_bi::list1<boost::arg<1> > >, R = int, T0 = int]’
/boost/include/boost/function/function_template.hpp:1061:   instantiated from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<(! boost::is_integral::value), int>::type) [with Functor = boost::_bi::bind_t<int, int (*)(int), boost::_bi::list1<boost::arg<1> > >, R = int, T0 = int]’
main.cpp:15:   instantiated from here
/boost/include/boost/bind/bind.hpp:882: error: returning reference to temporary
make: *** [all] Error 1

【问题讨论】:

  • 请记住,仅仅因为没有发出警告并不意味着警告不应该发出。
  • @JesperJuhl 非常好的评论。更糟糕的是,编译器不需要对大多数事情进行任何诊断。严格来说,这是一件尽力而为的事情
  • @sehe 实际上,由于优化交互的方式和信息丢失,编译器可能非常难以发出许多表面上显而易见的警告在前端解析你的代码和优化器看到的中间表示之间。
  • 是时候升级到 6 年前的标准 (2011)。我不希望 boost 支持 2003 或 1997 的标准,但希望他们能很快支持 2014 和 2017 的标准......

标签: c++ c++11 boost


【解决方案1】:

我已经使用 GCC 4.4 (CentOS 6) 和 Boost 1.41(捆绑在 CentOS 中)和 Boost 1.64(最新)运行您的代码。您的代码可以正常运行。

也许你混合了 boost 和 std 的东西?

boost::function<int(int)> bfoo = boost::bind(foo, boost::placeholders::_1); // #1 works
boost::function<int(int)> bfoo = boost::bind(foo, _1); // #2 _1 is from boost -> works
std::function<int(int)> bfoo = std::bind(foo, std::placeholders::_1); // #3 works

std::function<int(int)> bfoo = std::bind(foo, _1); // #4 not work
boost::function<int(int)> bfoo = boost::bind(foo, std::placeholders::_1); #5 not work

【讨论】:

  • 感谢@Byoungchan 的回复。我无法使用 -std=c++0x 标志(警告)获得上面的 #1 或 #2。但是,它确实可以在没有该标志的情况下编译并运行良好。我确信我没有混合 std 和 boost,因为我已经完全确定了所有内容。 #3 和 #4 对我有用 -std=c++0x -Werror -Wall。
猜你喜欢
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多