Restrict passed parameter to a string literal

前端 未结 6 1183
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 23:20

I have a class to wrap string literals and calculate the size at compile time.

The constructor looks like this:

template< std::size_t N >
Liter         


        
6条回答
  •  春和景丽
    2020-12-03 23:37

    With a C++11 compiler with full support for constexpr we can use a constexpr constructor using a constexpr function, which compiles to a non-const expression body in case the trailing zero character precondition is not fulfilled, causing the compilation to fail with an error. The following code expands the code of UncleBens and is inspired by an article of Andrzej's C++ blog:

    #include 
    
    class Literal
    {
      public:
    
        template  constexpr
        Literal(const char (&str)[N])
        : mStr(str),
          mLength(checkForTrailingZeroAndGetLength(str[N - 1], N))
        {
        }
    
        template  Literal(char (&str)[N]) = delete;
    
      private:
        const char* mStr;
        std::size_t mLength;
    
        struct Not_a_CString_Exception{};
    
        constexpr static
        std::size_t checkForTrailingZeroAndGetLength(char ch, std::size_t sz)
        {
          return (ch) ? throw Not_a_CString_Exception() : (sz - 1);
        }
    };
    
    constexpr char broke[] = { 'a', 'b', 'c' };
    
    //constexpr Literal lit = (broke); // causes compile time error
    constexpr Literal bla = "bla"; // constructed at compile time
    

    I tested this code with gcc 4.8.2. Compilation with MS Visual C++ 2013 CTP failed, as it still does not fully support constexpr (constexpr member functions still not supported).

    Probably I should mention, that my first (and preferred) approach was to simply insert

    static_assert(str[N - 1] == '\0', "Not a C string.")
    

    in the constructor body. It failed with a compilation error and it seems, that constexpr constructors must have an empty body. I don't know, if this is a C++11 restriction and if it might be relaxed by future standards.

提交回复
热议问题