谁是失败,助推,ang或gcc?问题与std :: chrono用于boost :: asio [英] Who is failing, boost, clang, or gcc? Issue with std::chrono used with boost::asio

查看:783
本文介绍了谁是失败,助推,ang或gcc?问题与std :: chrono用于boost :: asio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题所示,boost :: asio现在可以使用如果它们可用,C ++ 11计时对象。但是,以下代码编译但不包含clang 3.6.0-svn223366-1〜exp1

As noted by this question, boost::asio now can use the C++11 chrono objects if they are available. However, the following code compiles with but not with clang 3.6.0-svn223366-1~exp1

#include <iostream>
#include <boost/chrono.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <thread>
#include <functional>

boost::asio::io_service ioservice;
boost::asio::steady_timer timer(ioservice);

void function()
{
    std::cout << "Success" << std::endl;
}

int main()
{
    std::thread t([&]{ ioservice.run(); });
    t.detach();

    timer.expires_from_now(std::chrono::milliseconds(10));
    timer.async_wait(boost::bind(&function));
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

gcc:

~/scratch$ g++ -std=c++11 test_asio_chrono_broken.cpp -lpthread -lboost_system -lboost_chrono
~/scratch$ ./a.out 
Success

gcc和boost都很乐意使用std :: chrono

gcc and boost are happy to use std::chrono

但是,clang:

~/scratch$ clang++-3.6 -std=c++11 test_asio_chrono_broken.cpp -lpthread -lboost_system -lboost_chrono
test_asio_chrono_broken.cpp:23:8: error: no matching member function for call to 'expires_from_now'
    timer.expires_from_now(std::chrono::milliseconds(10));
    ~~~~~~^~~~~~~~~~~~~~~~
/usr/include/boost/asio/basic_waitable_timer.hpp:414:15: note: candidate function not viable: no known conversion from 'std::chrono::milliseconds' (aka 'duration<int64_t, milli>') to 'const duration' (aka 'const duration<boost::int_least64_t, nano>') for 1st argument
  std::size_t expires_from_now(const duration& expiry_time)
              ^
/usr/include/boost/asio/basic_waitable_timer.hpp:387:12: note: candidate function not viable: requires 0 arguments, but 1 was provided
  duration expires_from_now() const
           ^
/usr/include/boost/asio/basic_waitable_timer.hpp:445:15: note: candidate function not viable: requires 2 arguments, but 1 was provided
  std::size_t expires_from_now(const duration& expiry_time,
              ^
1 error generated.

boost :: asio正在尝试使用boost :: chrono,而不是std :: chrono。

boost::asio is trying to use boost::chrono instead of std::chrono.

在这里的故障?我猜想boost是怪的,因为它有特殊的GCC #ifdef标志用于捕获c ++ 11和std :: chrono,但那些#ifdefs没有捕获std :: chrono在使用clang时可用。

Who is at fault here? I would guess that boost is to blame because it has special GCC #ifdef flags for catching c++11 and std::chrono, but that those #ifdefs aren't catching that std::chrono is available when using clang. To whom do I report this issue?

我可以通过明确定义定时器类型来修复代码(在自动的std :: chrono检测之前, 。以下更改将在gcc和clang上进行编译

I can fix the code by explicitly defining the timer type (as required by older versions of boost before the automagic std::chrono detection was added). The following change will compile on both gcc and clang

typedef boost::asio::basic_waitable_timer<std::chrono::steady_clock> steady_timer;
steady_timer timer(ioservice);


推荐答案

chrono文档描述此


对于
g ++ 4.6和更高版本,当-std = c ++ 0x或-std = gnu ++ 0x编译器
选项时,将自动启用对std :: chrono功能的支持使用。 (注意,对于g ++,使用草稿标准
monotonic_clock代替steady_clock。)支持可以通过定义BOOST_ASIO_DISABLE_STD_CHRONO禁用
,或者为其他编译器显式地启用
,通过定义BOOST_ASIO_HAS_STD_CHRONO。

Support for the std::chrono facilities is automatically enabled for g++ 4.6 and later, when the -std=c++0x or -std=gnu++0x compiler options are used. (Note that, for g++, the draft-standard monotonic_clock is used in place of steady_clock.) Support may be disabled by defining BOOST_ASIO_DISABLE_STD_CHRONO, or explicitly enabled for other compilers by defining BOOST_ASIO_HAS_STD_CHRONO.

也就是说,你可以使用clang通过显式定义 BOOST_ASIO_HAS_STD_CHRONO

Added emphasis is mine. That said, you can get this working using clang by explicitly defining BOOST_ASIO_HAS_STD_CHRONO

clang++ -D BOOST_ASIO_HAS_STD_CHRONO -std=c++11 test_asio_chrono_broken.cpp -lpthread -lboost_system -lboost_chrono

我不知道是否有优先支持ang这样的东西在其他boost库,请提交错误

I don't know if there is precedence supporting clang for stuff like this in other boost libraries, please file a bug.

这篇关于谁是失败,助推,ang或gcc?问题与std :: chrono用于boost :: asio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆