https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93311
Bug ID: 93311
Summary: Missing "warning" when instantiating a constexpr
function
Product: gcc
Version: 9.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: kuzniar95 at o2 dot pl
Target Milestone: ---
The following piece of code produces a binary but the compiler output doesn't
inform if it's a warning or an error.
Compilation command: g++ -std=c++17 main.cpp
//----------------------
#include <array>
#include <cstddef>
template <std::size_t Size>
constexpr std::array<int, Size> foo() noexcept
{
std::array<int, Size> ret;
return ret;
}
int main()
{
foo<2>();
}
//----------------------
The above results in:
//----------------------
In file included from main.cpp:1:
main.cpp: In instantiation of 'constexpr std::array<int, Size> foo() [with long
unsigned int Size = 2]':
main.cpp:13:13: required from here
/usr/local/include/c++/9.2.0/array:94:12: note: 'struct std::array<int, 2>' has
no user-provided default constructor
94 | struct array
| ^~~~~
/usr/local/include/c++/9.2.0/array:110:56: note: and the implicitly-defined
constructor does not initialize 'int std::array<int, 2>::_M_elems [2]'
110 | typename _AT_Type::_Type _M_elems;
| ^~~~~~~~
//----------------------
By the way
- adding a pair of empty braces there (i.e. ret{}) solves the issue, which is
OK
- "untemplatizing" the function leads to "error: uninitialized variable 'ret'
in 'constexpr' function", which also is OK (see:
https://stackoverflow.com/questions/26568820/why-do-templates-allow-constexpr-function-members-with-non-constexpr-constructor)