Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::array::size constexpr with simple types (int, double, ...) but not std::vector (GCC)?

The following code:

std::array<int, 4> arr1;
std::array<float, arr1.size()> arr2;

...compiles with both gcc and clang because std::array::size is considered constexpr.

But the following does not compile with gcc (version 5.3.0 20151204):

std::array<std::vector<int>, 4> arr1;
std::array<std::vector<double>, arr1.size()> arr2;

For me, there is no reason such code should fail to compile if the first one works but since I did not find a lot of post on this I don't know if it is a gcc bug or a clang extension?

The error from gcc (that I don't really understand... ):

main.cpp: In function 'int main()':
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
     std::array<std::vector<double>, arr1.size()> arr2;
                                              ^
In file included from main.cpp:1:0:
/usr/local/include/c++/5.3.0/array:170:7: note: 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]' is not usable as a constexpr function because:
       size() const noexcept { return _Nm; }
       ^
/usr/local/include/c++/5.3.0/array:170:7: error: enclosing class of constexpr non-static member function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]' is not a literal type
/usr/local/include/c++/5.3.0/array:89:12: note: 'std::array<std::vector<int>, 4ul>' is not literal because:
     struct array
            ^
/usr/local/include/c++/5.3.0/array:89:12: note:   'std::array<std::vector<int>, 4ul>' has a non-trivial destructor
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
     std::array<std::vector<double>, arr1.size()> arr2;
                                              ^
main.cpp:6:48: note: in template argument for type 'long unsigned int' 
     std::array<std::vector<double>, arr1.size()> arr2;
                                                ^
like image 639
Holt Avatar asked Apr 14 '16 12:04

Holt


1 Answers

I think this is a related to CWG issue 1684. Previously, the constexpr requirements included:

The class of which a constexpr function is a member shall be a literal type

And std::array<std::vector<int>, 4> is not a literal type, and hence its size() member function would not be constexpr. However, the new wording allows for a constexpr non-static member functions for non-literal types, assuming those functions meet all the other requirements of constexpr (which array<T,N>::size() clearly does).

Per the new wording, this is a gcc bug. Previously filed as 66297.

like image 183
Barry Avatar answered Nov 11 '22 18:11

Barry