Macro for static std::string object from literal

前端 未结 5 2173
萌比男神i
萌比男神i 2021-02-12 17:56

Suppose I need to call a function foo that takes a const std::string reference from a great number of places in my code:

int foo(const          


        
5条回答
  •  天涯浪人
    2021-02-12 18:22

    You may use something like to create your static std::string "in place":

    #include 
    #include 
    
    // Sequence of char
    template  struct char_sequence
    {
        template  using push_back = char_sequence;
    };
    
    // Remove all chars from char_sequence from '\0'
    template  struct strip_sequence;
    
    template 
    struct strip_sequence, Cs...>
    {
        using type = char_sequence;
    };
    
    template 
    struct strip_sequence, Cs2...>
    {
        using type = char_sequence;
    };
    
    template 
    struct strip_sequence, Cs2...>
    {
        using type = typename strip_sequence, Cs2..., C>::type;
    };
    
    // struct to create a std::string
    template  struct static_string;
    
    template 
    struct static_string>
    {
        static const std::string str;
    };
    
    template 
    const
    std::string static_string>::str = {Cs...};
    
    // helper to get the i_th character (`\0` for out of bound)
    template 
    constexpr char at(const char (&a)[N]) { return I < N ? a[I] : '\0'; }
    
    // helper to check if the c-string will not be truncated
    template 
    constexpr bool check_size(const char (&)[N])
    {
        static_assert(N <= max_size, "string too long");
        return N <= max_size;
    }
    
    // Helper macros to build char_sequence from c-string
    #define PUSH_BACK_8(S, I) \
        ::push_back(S)>::push_back(S)> \
        ::push_back(S)>::push_back(S)> \
        ::push_back(S)>::push_back(S)> \
        ::push_back(S)>::push_back(S)>
    
    #define PUSH_BACK_32(S, I) \
            PUSH_BACK_8(S, (I) + 0) PUSH_BACK_8(S, (I) + 8) \
            PUSH_BACK_8(S, (I) + 16) PUSH_BACK_8(S, (I) + 24)
    
    #define PUSH_BACK_128(S, I) \
        PUSH_BACK_32(S, (I) + 0) PUSH_BACK_32(S, (I) + 32) \
        PUSH_BACK_32(S, (I) + 64) PUSH_BACK_32(S, (I) + 96)
    
    // Macro to create char_sequence from c-string (limited to 128 chars) without leading '\0'
    #define MAKE_CHAR_SEQUENCE(S) \
        strip_sequence \
        PUSH_BACK_128(S, 0) \
        ::push_back(S) ? '\0' : '\0'> \
        >::type
    
    // Macro to return an static std::string
    #define STATIC_STRING(S) static_string::str
    

    Live example

提交回复
热议问题