How to define string literal with character type that depends on template parameter?

前端 未结 5 1222
不知归路
不知归路 2021-02-06 07:47
template
class StringTraits {
public:
    static const CharType NULL_CHAR = \'\\0\';
    static constexpr CharType* WHITESPACE_STR = \" \";
};

         


        
5条回答
  •  日久生厌
    2021-02-06 08:20

    Here's an alternative implementation based on @zett42 's answer. Please advise me.

    #include 
    #include 
    
    #define TOWSTRING_(x) L##x
    #define TOWSTRING(x) TOWSTRING_(x)  
    #define MAKE_LPCTSTR(C, STR) (std::get(std::tuple(STR, TOWSTRING(STR))))
    
    template
    class StringTraits {
    public:
        static constexpr const CharType* WHITESPACE_STR = MAKE_LPCTSTR(CharType, "abc");
    };
    
    typedef StringTraits AStringTraits;
    typedef StringTraits WStringTraits;
    
    int main(int argc, char** argv) {
        std::cout << "Narrow string literal: " << AStringTraits::WHITESPACE_STR << std::endl;
        std::wcout << "Wide string literal  : " << WStringTraits::WHITESPACE_STR << std::endl;
        return 0;
    }
    

提交回复
热议问题