gpt4 book ai didi

c++ - 具有空定义的模板特化

转载 作者:行者123 更新时间:2023-11-30 00:43:27 24 4
gpt4 key购买 nike

我制作了一个模板函数,用于从数字初始化 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 函数返回值显然是未定义的行为。但是,我不明白的是,我怎么可能通过空特化获得正确的输出?我的看法是,案例 2 和案例 3 都不正确,不应在标准输出上给我正确的结果。

最佳答案

未定义的行为。它会编译,但你会收到一个巨大的警告,也许还会发生崩溃。或者什么都没有。

你知道的是你需要这个定义,正如定义所说,它应该返回一个 std::chrono::time_point<clock_t, duration_t>。 .如果你不这样做,那么你就是在违约。编译器是这样说的:

warning: no return statement in function returning non-void [-Wreturn-type]

关于c++ - 具有空定义的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com