c++templates

Template function, VS 2022 compiler seems to only implement argument version that was called first


I'm trying to create a templated function which can take either a Boost ptime, or time_duration - in order to extract the hours and format them as "00:00".

It is seeming that the compiler will only implement the template function under the type that was called first (in main()). Any further calls with another type will fail in compilation, saying xyz function cannot be used with type x.

Is there a way to make this template function work, or am I asking too much of the C++ compiler here?

Here's the code:

template <class T>
std::string GetHHMM(T TS) {
    int HH, MM;
    time_duration td(0, 0, 0);  // Our working variable that will contain either the time_duration argument, or a converted ptime argument.
    // If it's a ptime, we need to get the time out and put it in the time_duration variable.
    if (typeid(TS) == typeid(ptime)) {      // Just a check...picking up ptime argument OK.
        std::cout << "GetHHMM got called with a ptime." << std::endl;
        td = TS.time_of_day();  // Error C2039 - if main calls this function with a time_duration first
        HH = td.hours();
        MM = td.minutes();
    }
    if (typeid(TS) == typeid(time_duration)) {  // Just a check...picking up time_duration argument OK
        std::cout << "GetHHMM got called with a time_duration." << std::endl;   // Picks up time_duration argument OK
        HH = TS.hours();    // Error C2039 - hours is not member of ptime (thinks TS is a ptime when it's not, due ptime called first in main())
        MM = TS.minutes();
    }
    std::string s = str(boost::format("%02d:%02d") % HH % MM);
    return s;
}

In main() I've tried 'forcing' (not sure if that's the right term) the compiler to make both editions (one for time_duration, and one for ptime) with a call like:

GetHHMM<ptime>(p1) and GetHHMM(time_duration)

but no go. The compiler is only to happy to implement the function for whatever argument I called first in main(). I was hoping shifting compilation to C++17 would help (I believe it does has improved template interpretation), but no go there either. I'm using VS 2022 C++.


Solution

  • The entire function needs to typecheck regardless of the type T.
    You could solve that with a compile-time if constexpr, but then you would also need to handle unexpected types (e.g. GetHHMM(123) and GetHHMM("hello") would compile with your code).

    There is no point in reaching for templates here - overloading makes the code much simpler.
    Example:

    std::string GetHHMM(time_duration td) {
        return str(boost::format("%02d:%02d") % td.hours() % td.minutes());
    }
    
    std::string GetHHMM(ptime ts) {
        return GetHHMM(ts.time_of_day());
    }