https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
Bug ID: 103668
Summary: constexpr std::vector not working as expected
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: andrei.popa105 at yahoo dot com
Target Milestone: ---
Hi,
Currently gcc supports constexpr std::vector and std::string. Suppose that you
want to write code like this:
#include <vector>
constexpr auto get_vector() {
std::vector<int> vec{ 1, 2, 3, 4, 5, 6 };
return vec;
}
constexpr auto get_vector_size() {
constexpr auto vec = get_vector();
return vec.size();
}
int main() {
constexpr auto vec_size = get_vector_size();
return 0;
}
Unfortunately, this code does not compile and the error is the following:
error: ‘get_vector()()’ is not a constant expression because it refers to a
result of ‘operator new’
But std::vector is not used in runtime world, the function get_vector is called
in a constexpr context and then returns the size of the vector. Am I missing
something or it is a bug?
I compiled this code with g++ file.cpp -std=c++23