Closed as duplicate of#73232
Closed as duplicate of#73232
Description
Consider following program, which compiles with gcc and msvc
#include <string_view>
#include <string>
template <typename T>
constexpr int foo() {
constexpr auto test = std::string_view( "test" );
static_assert(test.size() >1);
static_assert(std::string(test).size() >1); // should not fail
return 0;
}
int bar(){
return foo<int>();
}
With clang
it fails to compile with "error: static assertion expression is not an integral constant expression".
Strangely, adding a local char
array and a static_assert
, changes the behavior
This code still fails to compile
#include <string_view>
#include <string>
template <typename T>
constexpr int foo() {
constexpr auto test = std::string_view( "test" );
constexpr char test2[] = "test";
static_assert(test.size() >1);
static_assert(std::string(test).size() >1); // should still not fail
static_assert(std::string(test2).size() >1);
return 0;
}
int bar(){
return foo<int>();
}
This one compiles:
#include <string_view>
#include <string>
template <typename T>
constexpr int foo() {
constexpr auto test = std::string_view( "test" );
constexpr char test2[] = "test";
static_assert(test.size() >1);
static_assert(std::string(test2).size() >1);
static_assert(std::string(test).size() >1); // success
return 0;
}
int bar(){
return foo<int>();
}
live demo: https://godbolt.org/z/KK1WjajPq