Search code examples
c++c++11boost-mpl

How to concatenate a const char* in compile time


I have a vector of mpl::string. mpl::string size limit is 32 elements. Have a way to create const char* array in compile time

 MACRO(z,i,data) data
 .............
 const char* array[] = { BOOST_PP_ENUM(SIZE,MACRO,mpl_vector) };

But i need get a one const char* string in compile time. How to make it?

UPDATE I create an array of mpl::string in compile time. They compressed (size of every string about 25-31 with limit of 32). I may get array from it strings like

  //first,second string etc is mpl::c_str<mpl_string>::value
  const char* array_mpl_strings[] = {first_string,second_string .....};

But i need a complete string (not array):

  const char* string = first_stringsecond_string....;

How do "string" from "array_mpl_strings"?


Solution

  • I don't know the reason about BOOST_MPL_STRING_MAX_LENGTH

    but following may help if you could convert your type:

    #include <cassert>
    #include <cstring>
    #include <tuple>
    
    
    template<char...Cs> struct seq
    {
        typedef const char (&arr_type)[sizeof...(Cs) + 1];
        static constexpr arr_type c_str() { return str; }
        static constexpr char str[sizeof...(Cs) + 1] = {Cs..., '\0'};
    };
    
    template<char...Cs>
    constexpr char seq<Cs...>::str[sizeof...(Cs) + 1];
    
    
    template<typename T, typename ... Ts>
    struct concat_seq;
    
    template<char...Cs>
    struct concat_seq<seq<Cs...>> : seq<Cs...> {};
    
    template<char...Cs1, char...Cs2>
    struct concat_seq<seq<Cs1...>, seq<Cs2...>> : seq<Cs1..., Cs2...> {};
    
    template<char...Cs1, char...Cs2, typename ... Ts>
    struct concat_seq<seq<Cs1...>, seq<Cs2...>, Ts...> :
        concat_seq<seq<Cs1..., Cs2...>, Ts...> {};
    
    template<typename ... Ts>
    struct concat_seq<std::tuple<Ts...>> : concat_seq<Ts...> {};
    
    int main()
    {
        const char* s = concat_seq<std::tuple<seq<'h', 'e'>, seq<'l', 'l', 'o'>>>::c_str();
    
        assert(strcmp(s, "hello") == 0);
    
        return 0;
    }