为什么这个程序使用std :: pair与std :: chrono :: time_point不能编译? (转换错误)

时间:2015-11-06 02:58:43

标签: c++ c++11 chrono

以下无法使用gcc 5.2.0(Arch Linux x86_64)进行编译:

#include <chrono>
#include <utility>

using std::chrono::seconds; // std::chrono::duration<long> on this system
using std::chrono::system_clock;
using std::chrono::time_point;
using std::make_pair;
using std::pair;

typedef time_point<system_clock, seconds> timept;

int main() {
    char c = 'c';
    long i = 10;
    seconds d(i);
    timept t1(d);
    timept t2(seconds(10));
    timept t3(seconds(i));
    pair<timept,char> p1 = make_pair(t1, c);
    pair<timept,char> p2 = make_pair(t2, c);
    pair<timept,char> p3 = make_pair(t3, c); // conversion error
    auto p3a = make_pair(t3, c); // works, but what is the type of p3a?

    return 0;
}

由于t1,t2和t3具有相同的类型,因此对p1,p2和p3应该是等效的。然而,make_pair(t3,c)似乎创建了一对不同的类型 - 某种功能:

g++ -std=gnu++14 -Wall -Wextra -O0 -o test test.cpp

test.cpp: In function ‘int main()’:
test.cpp:21:37: error: conversion from ‘std::pair<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int> > (*)(std::chrono::duration<long int>), char>’ to non-scalar type ‘std::pair<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int> >, char>’ requested
     pair<timept,char> p3 = make_pair(t3, c);

任何人都能解释一下吗?谢谢:))

0 个答案:

没有答案