C++ template string concatenation

后端 未结 5 898
天涯浪人
天涯浪人 2020-12-16 02:50

I\'m trying to define some variadic template like that:

typedef const char CCTYPE[];
template struct StringConcat { ... };
         


        
5条回答
  •  再見小時候
    2020-12-16 03:06

    You can solve the problem of making your std::cout << StringConcat work.

    template struct StrBag {};
    template void StringConcat(StrBag) {}
    
    std::ostream &print(std::ostream &os) { 
      return os; 
    }
    
    template 
    std::ostream &print(std::ostream &os, CCTYPE t1, T ...t) { 
      os << t1; 
      return print(os, t...);
    }
    
    template
    std::ostream &operator<<(std::ostream &os, void(StrBag)) {
      return print(os, Str...) << std::endl;
    }
    

    Now you can say

    char foo[] = "foo"; char bar[] = "bar";
    int main() {
      std::cout << StringConcat << std::endl;
    }
    

    Hope it helps.

提交回复
热议问题