如何根据定义的字符串类型在`std :: cout`和`std :: wcout`之间进行选择?

时间:2018-01-31 14:29:06

标签: c++ string compile-time c++17

我在代码中定义了自己的字符串类型。

typedef wchar_t CharType;
typedef std::basic_string<CharType> StringType;

我有一个静态类(它没有实例),它会在屏幕上打印字符串消息。我决定根据我定义的字符串类型放置一个COUT静态成员,该成员将引用std::coutstd::wcout

部首:

#include <ostream>

class MyTestClass
{
    public:
        // ...
        static std::basic_ostream<CharType> & COUT;
        // ...
}

CPP:

std::basic_ostream<CharType> & MyTestClass::COUT = /* How do I initialize this? */;

有没有办法初始化这个静态成员COUT

3 个答案:

答案 0 :(得分:5)

这是C ++ 17中的一个选项:

#include <iostream>
#include <type_traits>

template <class T>
auto &get_cout() {
    if constexpr(std::is_same_v<T, char>){
        return std::cout;
    }else{
        return std::wcout;
    }
}

int main() {
    {
        using CharType = char;
        std::basic_ostream<CharType> & MyTestClass_COUT = get_cout<CharType>();
        MyTestClass_COUT << "Hello";
    }
    {
        using CharType = wchar_t;
        std::basic_ostream<CharType> & MyTestClass_COUT = get_cout<CharType>();
        MyTestClass_COUT << L"World";
    }
}

如果您没有C ++ 17,可以使用基于特征的解决方案替换if constexpr

Demo

答案 1 :(得分:4)

老式特质风格解决方案:

template<class T>
struct cout_trait {};

template<>
struct cout_trait<char> {
    using type = decltype(std::cout);
    static constexpr type& cout = std::cout;
    static constexpr type& cerr = std::cerr;
    static constexpr type& clog = std::clog;
};
template<>
struct cout_trait<wchar_t> {
    using type = decltype(std::wcout);
    static constexpr type& cout = std::wcout;
    static constexpr type& cerr = std::wcerr;
    static constexpr type& clog = std::wclog
};

用法:

auto& cout = cout_trait<char>::cout;

答案 2 :(得分:2)

您还可以使用专门的课程为您做出选择:

template <typename T_Char>
struct StreamSelector;

template <>
struct StreamSelector<char> {
    static constexpr std::ostream &stream = std::cout;
};

template <>
struct StreamSelector<wchar_t> {
    static constexpr std::wostream &stream = std::wcout;
};

std::basic_ostream<CharType> &COUT = StreamSelector<CharType>::stream;

这可以从C ++ 11开始,虽然它可以在此之前稍作修改。