Concat two `const char` string literals

后端 未结 5 2145
北荒
北荒 2020-11-28 12:26

Is it possible to concat two string literals using a constexpr? Or put differently, can one eliminate macros in code like:

#define nl(str) str \         


        
5条回答
  •  悲&欢浪女
    2020-11-28 12:36

    A little bit of constexpr, sprinkled with some TMP and a topping of indices gives me this:

    #include 
    
    template struct seq{};
    template
    struct gen_seq : gen_seq{};
    template
    struct gen_seq<0, Is...> : seq{};
    
    template
    constexpr std::array concat(char const (&a1)[N1], char const (&a2)[N2], seq, seq){
      return {{ a1[I1]..., a2[I2]... }};
    }
    
    template
    constexpr std::array concat(char const (&a1)[N1], char const (&a2)[N2]){
      return concat(a1, a2, gen_seq{}, gen_seq{});
    }
    

    Live example.

    I'd flesh this out some more, but I have to get going and wanted to drop it off before that. You should be able to work from that.

提交回复
热议问题