How to deduce the size of a compile time a const char string with C++ templates?
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_