I have a template function as following:
using namespace std::chrono;
using namespace std::chrono_literals;
template <typename D>
time_point<system_clock, D> makeTime(
int year, int month, int day, int hour = 0, int minute = 0,
int second = 0, int ms = 0, int us = 0, int ns = 0 );
Normally, I call it like this: auto us_tp1 = makeTime<microseconds>( 2020, 5, 26, 21, 21, 21, 999, 123 );
But now I need to call it at somewhere through an alias "makeTimeUS" like this:
auto us_tp1 = makeTimeUS( 2020, 5, 26, 21, 21, 21, 999, 123 );
just like that makeTimeUS is an instance of makeTime.
I tried this:
using makeTimeUS = template time_point<system_clock, microseconds> makeTime;
and this:
using makeTimeUS = template time_point<system_clock, microseconds> makeTime(
int, int, int, int, int, int, int, int, int );
but neither can pass the compilation.
How to instantiate a template function and give an alias on it at same time? The reason I need to do so, is that there are too many old codes calling makeTimeUS as if it be a normal function instead of a template. Thanks!
You can get a function pointer to the function you want and then use that as your "alias". That would look like:
auto makeTimeUS = makeTime<microseconds>;
And can be used like:
auto us_tp1 = makeTimeUS( 2020, 5, 26, 21, 21, 21, 999, 123 );
But this just lets you change the name. Since it is a function pointer the default arguments no longer work and you still have to specify all of the parameters.
To get around that you can make a wrapper instead of an alias using a lambda and that would look like
auto makeTimeUS = [](int year, int month, int day, int hour = 0,
int minute = 0, int second = 0, int ms = 0)
{
return makeTime<microseconds>(year, month, day, hour, minute, second, ms);
};