mardi 21 février 2017

Lexicographically compare two strings at compile time in C++11

I'm trying to use the str_const class inspired from http://ift.tt/2lEtvK8

class str_const
{
public:
    template<std::size_t N>
    constexpr str_const(const char(&arr)[N]) noexcept : str{arr}, len{N - 1} 
    {
    }

    constexpr char operator[](std::size_t i) const 
    {
        return i < len ? str[i] : throw std::out_of_range{""}; 
    }

    constexpr std::size_t size() const noexcept { return len; }

    constexpr operator const char*() const noexcept { return str; } 

    constexpr const char* c_str() const noexcept { return str; }

private:
    const char* const str;
    const std::size_t len;
};

Considering the restrictions on constexpr functions in C++11, how to implement the following lexicographical comparison:

constexpr bool operator<(str_const lhs, str_const rhs)
{
}

Aucun commentaire:

Enregistrer un commentaire