重载采用 chrono::d uration 的函数

Overloading a function that takes a chrono::duration

本文关键字:uration 函数 重载 chrono      更新时间:2023-10-16
typedef std::chrono::duration<int, std::ratio_multiply<std::ratio<12, 34>, std::ratio<9>>> irrelevant;
void func(irrelevant){} //  comment this out to make it work
void func(std::chrono::seconds){}
void func(std::chrono::minutes){}
int main()
{
    func(std::chrono::seconds(43));
}

上面,包含需要irrelevant的重载会导致一堆错误,例如:

'std::ratio_divide<_R1,_R2>::type' 不是 'std::ratio_divide<_R1,_R2>' 基类的成员

是真的出了什么问题,还是VS2012应该受到指责?我该如何解决它?

我无法访问VS2012,但看看这是否有效:

typedef std::chrono::duration<int, std::ratio_multiply<std::ratio<12, 34>, std::ratio<9> >::type> irrelevant;

您的代码在 C++11 模式下为我编译,但不在 C++03 模式下编译(clang/libc++)。 我的理论是VS2012尚未实现模板别名,或者即使有,其<chrono>尚未对模板别名的可用性做出反应。 作为回退,ratio_multiply有一个嵌套类型type应该可以完成这项工作。