2

According to the GCC 8 Release Note, it is now possible to use std::char_traits<char> and std::char_traits<wchar_t> in constant expressions:

  • Improved experimental support for C++17, including the following features:
    • std::char_traits and std::char_traits<wchar_t> are usable in constant expressions.

What I understand here is you can't use std::char_traits<char> in constant expressions before GCC 8.

However, the following code compiles well with GCC 7.5:

#include <string_view>

int main() {
    constexpr std::string_view sv{""};
    constexpr std::size_t size = std::char_traits<char>::length(sv.data());

    return 0;
}

So, is it possible to use std::char_traits in constant expressions before GCC 8? Or am I missing something?

Pierre
  • 1,942
  • 3
  • 23
  • 43
  • `Is it possible to use std::char_traits in constant expressions before GCC 8?` Sure. It's just a class that is never going to be instantiated. The change log is simplifying a bit, and it is actually about the member functions of `std::char_traits`. – eerorika Jul 23 '20 at 12:50

1 Answers1

2

This improvement was backported from mainline. It is available in the gcc 7 line from 7.3 onwards.

ecatmur
  • 152,476
  • 27
  • 293
  • 366