moneypunct 是 Object International 吗?

Is moneypunct Object International?

假设我有一个模板化函数,它接受 moneypunct:

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

我可以使用 typename T:char_type 来确定第一个 moneypunct 模板参数(无论我是在处理 moneypunct<char> 还是 moneypunct<wchar_t>。)但是我怎么才能确定第二个模板参数是 true 还是 false (moneypunct<char, true> or moneypunct<char, false>?)

唯一的方法是将我的功能重新设计为:

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

如果你只想拍一张moneypunct,这绝对是最好最清晰的解决方案:

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

但是,您仍然可以从具有类型特征的原始模板参数中确定两个模板参数:

template <typename> struct isInternational;

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

你可以使用:

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;
}