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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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;
}
</code>
<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; } </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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GetHHMM<ptime>(p1) and GetHHMM(time_duration)
</code>
<code>GetHHMM<ptime>(p1) and GetHHMM(time_duration) </code>
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++.

6

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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());
}
</code>
<code>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()); } </code>
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());
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật