Concatenate compile-time strings in a template at compile time?

前端 未结 3 1727
猫巷女王i
猫巷女王i 2020-12-08 12:47

Currently I have:

template  struct typename_struct {
    static char const* name() { 
        return (std::string(typename_struc         


        
3条回答
  •  没有蜡笔的小新
    2020-12-08 13:03

    You could use something like this. Everything happens at compile time. Specialize base_typename_struct to define your primitive types.

    template 
    struct append {
      static constexpr const char* value() {
        return append::value();
      }
    };
    
    template 
    struct append {
      static const char value_str[];
      static constexpr const char* value() {
        return value_str;
      }
    };
    
    template 
    const char append::value_str[] = { suffix..., 0 };
    
    
    template 
    struct base_typename_struct;
    
    template <>
    struct base_typename_struct {
      static constexpr const char name[] = "int";    
    };
    
    
    template 
    struct typename_struct {
      typedef base_typename_struct base;
      static const char* name() {
        return append::value();
      }
    };
    
    template 
    struct typename_struct:
      public typename_struct {
    };
    
    
    int main() {
      cout << typename_struct::name() << endl;
    }
    

提交回复
热议问题