Issue |
142144
|
Summary |
`constexpr string` from `constexpr string_view` is not always a constant _expression_
|
Labels |
new issue
|
Assignees |
|
Reporter |
fekir
|
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
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs