Sunday 15 February 2015

c++11 - Length of user-defined string literal as a template argument? -


Is there any way to behave this way?

  // some definition) Operator "" _my_str // Some definition of function or macro using T1 = MY_STR_LEN ("app" _my_str) MY_STR_LEN; // T1 is std :: integral_constant & lt; Std :: size_t, 3U & gt; T2 = MY_STR_LEN ("Aardvark" _my_str); // T2 std :: integral_constant & lt; Std :: size_t, 8u & gt;   

It does not seem that string strings are sent immediately to some_return_type operator "_my_str (const char *, std :: size_t); and sometimes Not even a literal operator template (2.14.8 / 5). This size function parameter can not be used as a template logic, even if it is almost continuous expression.

But it seems that There should be a way to do it. Reading C ++ 11 2.14.8 carefully shows that "literal PATTERTER TEMPLATE "is only considered for numerical literals, but not for string and character literals.

However, following approaches give you access to string length (but not indicator) Seems to:

  straight mist {four consists * str; unsigned int lane; Constexpr MyStr (thief const * p, unsigned int n): str (p), lan (n) { }}; Constexpr MyStr operator "" _xyz (four const * s, unsigned int len) {return MyStr (S, LE );} Constexpr auto s = "hello" _xyz;   

Test:

  #include & lt; Array & gt; Atype = std :: array & lt; Int, s.len & gt; // OK    

No comments:

Post a Comment