Restrict passed parameter to a string literal

前端 未结 6 1190
没有蜡笔的小新
没有蜡笔的小新 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:47

    There is a way to force a string literal argument: make a user defined literal operator. You can make the operator constexpr to get the size at compile time:

    constexpr Literal operator "" _suffix(char const* str, size_t len) {
        return Literal(chars, len);
    }
    

    I don't know of any compiler that implements this feature at this time.

提交回复
热议问题