【发布时间】:2019-01-28 12:14:40
【问题描述】:
我制作了一个模板函数,用于从数字初始化chrono::time_point。到目前为止我已经成功了,但遇到了一个我不完全理解的问题。下面给出了我的代码的两个最小示例。
以下代码无法编译并出现以下错误:
/usr/include/c++/7/chrono:616:14: note: no known conversion for argument 1 from ‘const double’ to ‘const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<double> >&’
/usr/include/c++/7/chrono:616:14: note: candidate: constexpr std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<double> >::time_point(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<double> >&&)
/usr/include/c++/7/chrono:616:14: note: no known conversion for argument 1 from ‘const double’ to ‘std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<double> >&&’
#include <iostream>
#include <chrono>
namespace yv {
using clock_t = std::chrono::system_clock;
using duration_t = std::chrono::duration<double>;
using time_t = std::chrono::time_point<clock_t, duration_t>;
namespace fromnumber {
template<class T, class T_time> T_time time(T const& timestamp) {
return T_time(timestamp);
}
// No specialization
}; // end namespace fromnumber
}; // end namespace yv
int main()
{
using namespace yv;
using namespace std;
yv::time_t t0 = yv::fromnumber::time<double, yv::time_t>(0.0);
yv::time_t t1 = yv::fromnumber::time<double, yv::time_t>(1548675254.0);
return 0;
}
但是,当我添加具有空定义的模板特化时,它会编译。
#include <iostream>
#include <chrono>
namespace yv {
using clock_t = std::chrono::system_clock;
using duration_t = std::chrono::duration<double>;
using time_t = std::chrono::time_point<clock_t, duration_t>;
namespace fromnumber {
template<class T, class T_time> T_time time(T const& timestamp) {
return T_time(timestamp);
}
template<> std::chrono::time_point<clock_t, duration_t> time(double const&) {
// EMPTY
}
}; // end namespace fromnumber
}; // end namespace yv
int main()
{
using namespace yv;
using namespace std;
yv::time_t t0 = yv::fromnumber::time<double, yv::time_t>(0.0);
yv::time_t t1 = yv::fromnumber::time<double, yv::time_t>(1548675254.0);
return 0;
}
特化有一个定义,但它甚至不返回一个值。我在这里错过了什么?
编辑: 感谢您的快速回复。下面是一个使用 Howard Hinnant 的 date.h 的更广泛的示例。
#include <iostream>
#include "date/date.h"
//#include <chrono>
//using namespace date;
namespace yv {
using clock_t = std::chrono::system_clock;
using duration_t = std::chrono::duration<double>;
using time_t = std::chrono::time_point<clock_t, duration_t>;
namespace fromnumber {
template<class T, class T_time> T_time time(T const& timestamp) {
return T_time(timestamp);
}
// Case 1. Correct specialization, not getting any warnings.
template<> std::chrono::time_point<clock_t, duration_t> time(double const& t)
{
return std::chrono::time_point<clock_t, duration_t>(duration_t(t));
}
// Case 2. Incorrect specialization, compiles and prints the correct datetime but getting a warning
template<> std::chrono::time_point<clock_t, duration_t> time(double const& t)
{
}
// Case 3. Without the specialization it will not compile, error given above
}; // end namespace fromnumber
}; // end namespace yv
std::ostream& operator<< (std::ostream& outStream, const yv::time_t& t) {
using namespace date;
auto t2 = date::floor<std::chrono::milliseconds>(t);
outStream << date::format("%c", t2);
return outStream;
}
int main()
{
using namespace yv;
using namespace std;
yv::time_t t0 = yv::fromnumber::time<double, yv::time_t>(0.0);
yv::time_t t1 = yv::fromnumber::time<double, yv::time_t>(1548675254.0);
cout << t1 << endl;
// expecting: Mon Jan 28 11:34:14 2019
return 0;
}
情况2的警告:
../try_chrono/main.cpp: In function ‘T_time yv::fromnumber::time(const T&) [with T = double; T_time = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<double> >]’:
../try_chrono/main.cpp:21:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
不从非 void 函数返回值显然是未定义的行为。但是,我不明白的是,我怎么可能通过空的专业化获得正确的输出? 我看到它的方式是 case 2 和 case 3 都不正确,不应该在 stdout 上给我正确的结果。
【问题讨论】:
-
未定义行为并不意味着“肯定会给出错误的结果”。它只是意味着“任何事情(包括‘正确’行为)都可能发生”。
标签: c++ chrono function-templates