Search code examples
c++templatestypedefcurrencytypename

Is moneypunct Object International?


Say I have a templatized function that takes a moneypunct:

template <typename T>
void foo(const T& bar);

I can use typename T:char_type to determine the first moneypunct template argument (whether I'm dealing with a moneypunct<char> or a moneypunct<wchar_t>.) But how can I determine whether the second template argument is true or false (moneypunct<char, true> or moneypunct<char, false>?)

Is the only way to accomplish this to redesign my function to:

template <typename CharT, typename International = false>
void foo(const moneypunct<CharT, International>& bar);

Solution

  • If you only want to take a moneypunct, this would definitely be the best, clearest solution:

    template <typename CharT, typename International = false>
    void foo(const moneypunct<CharT, International>& bar);
    

    However, you can still determine both template arguments from the original with a type trait:

    template <typename> struct isInternational;
    
    template <typename CharT, bool International>
    struct isInternational<std::moneypunct<CharT, International>>
    : std::integral_constant<bool, International>
    { }
    

    Which you can use:

    template <typename T>
    void foo(const T& bar) {
        // this won't compile if T is not a std::moneypunct
        std::cout << isInternational<T>::value << std::endl;
    }