I made a template function for initializing a chrono::time_point
from a number. I have succeeded so far but encountered an issue which I do not fully understand. Two minimal examples of my code are given below.
Below code fails to compile with the following error:
/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;
}
However, when I add a template specialization with empty definition it does compile.
#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;
}
The specialization has a definition but it does not even return a value. What am I missing here?
EDIT: Thanks for the quick replies. Below is a more extensive example using Howard Hinnant's 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;
}
Warning in case 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]
}
^
It is clearly undefined behavior to not return a value from a non-void function. However, the thing I do not understand is how it can be possible that I am getting the correct output with an empty specialization? The way I see it is that both case 2 and case 3 are incorrect and should not give me a correct result on stdout.