如何在编译时连接const char * [英] How to concatenate a const char* in compile time

查看:139
本文介绍了如何在编译时连接const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mpl :: string的向量。 mpl :: string大小限制为32个元素。有一种方法可以在编译时创建const char *数组

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

但是我需要在编译时得到一个const char *字符串。怎么做?

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

更新
我在编译时创建了一个mpl :: string数组。他们压缩(每个字符串的大小大约为25-31,限制为32)。我可能会从中获取数组,例如

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

如何从 array_mpl_strings中获取 string?

How do "string" from "array_mpl_strings"?

推荐答案

我不知道有关 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;
}

这篇关于如何在编译时连接const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆