首页 文章

是否可以将std :: chrono :: duration与Rep类型一起使用为double?我尝试时在vs2012中遇到编译器错误

提问于
浏览
1

我得到以下编译器(vs2012)错误:

错误3错误C2679:二进制'=':找不到运算符,它接受类型'const std :: chrono :: duration <_Rep,_Period>'的右手操作数(或者没有可接受的转换)c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \ include \ chrono 749

我对持续时间的定义是:

// Tick interval type in nanoseconds
typedef std::chrono::duration<double, std::ratio<1, 100000000>> tick_interval_type;

当我使用float时出现相同的错误...它仅在Rep类型的持续时间为整数时编译 .

有人可以帮忙吗?

编辑(从输出更完整的日志):

c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \ include \ chrono(749):error C2679:binary'=':找不到运算符,它采用类型为'const std :: chrono的右手操作数:: duration <_Rep,_Period>'(或没有可接受的转换)[_ Red = double,_Period = std :: nano] c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \ include \ chrono( 166):可以是'std :: chrono :: duration <_Rep,_Period>&std :: chrono :: duration <_Rep,_Period> :: operator =(const std :: chrono :: duration <_Rep,_Period>&) '[Rep = __ int64, Period = std :: nano]尝试匹配参数列表'(std :: chrono :: nanoseconds,const std :: chrono :: duration <_Rep,_Period>)'与[_Rep = double,_Period = std :: nano] c:\ program files(x86)\ microsoft visual studio 11.0 \ _vc \ include \ thread(164):参见函数模板实例化'xtime std :: _ To_xtime(const std :: chrono) :: duration <_Rep,_Period>&)'正在使用[_Rep = double,_Period = std :: nano] c:\ dev \ projects \ revolverx \ classes \ ticker.h(78)编译:请参阅引用函数模板实例化'void std :: this_thread :: sleep_for(const std :: chrono :: duration <_Rep,_Period>&)'用[_Rep = double,_Period = std :: nano]编译

2 回答

  • 6

    Visual Studio中的 <chrono> 已损坏 . 它不适用于混合型算术,可以说是one of the main features<chrono> . 你得到这个错误,因为其中一方使用 __int64 nanos而另一方使用 double nanos .

    我建议将其删除以支持真正的C实现,或者使用Boost.Chrono .

  • 0

    更新:我在最初发布这个问题四年后就遇到了这个问题,我在Visual Studio 2015上对此进行了测试 . 它现在可以编译 . 例如:

    #include <iostream>
    #include <iomanip>
    #include <chrono>
    
    int main()
    {
        typedef std::chrono::duration<double, std::ratio<1, 100000000>> tick_interval_type;  // Originally posted line
        tick_interval_type tick {0};
        tick += std::chrono::microseconds(3);
        std::cout << std::setprecision(6) << std::fixed << tick.count();
        return 0;
    }
    

    输出:

    300.000000
    

相关问题