c++ - template which takes a pack of type T -


i have following minimal code fragment compiles , executes expected:

template < class t, t... chrs> struct mystring {     static auto c_str()     {         static char str[] = {chrs..., 0};         return str;     }  };  template <class t, t... chrs> auto operator""_t() {     return mystring<t, chrs...>(); }  int main() {     auto x = "hallo"_t ;     std::cout << x.c_str() << std::endl; } 

question:

is possible write template mystring in way excepts:

 auto x = mystring<'a','b','c'>(); 

but also

 auto x = mystring< 1,2,3> (); 

or other type.

i have no idea how write ( pseudo code ):

template < t ... chrs> // how define t here? struct mystring { } 

also following not allowed:

template <typename t, t ...chrs > struct mystring<t...chrs> {}; 

the same way you're doing right now. except instead of using t, t template parameter, use char directly:

template <char... chrs> struct mystring {     /* rest before */ }; 

of course works char , not wchar_t (but again, original)


you can generalize writing like:

template <class t, t... vals> struct array { ... };  template <char... chrs> using mystring  = array<char, chrs...>; 

in c++17, we'll have template auto let write:

template <auto... vals> struct array { /* .. */ }; 

and it's verify vals same type. perhaps via:

template <auto val, decltype(val)... vals> struct array { /* .. */ }; 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -