Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template argument for std::vector initialization

There is a struct called Matrix with a template argument N and data_ field:

#include <cstddef>
#include <vector>

template <std::size_t N>
struct Matrix {
    std::vector<std::vector<int>> data_{N, std::vector<int>(N)};
};

Why is it not possible to initialize data_ using round brackets?

std::vector<std::vector<int>> data_(N, std::vector<int>(N));

Here's the error:

<source>:6:41: error: unknown type name 'N'
    std::vector<std::vector<int>> data_(N, std::vector<int>(N));
                                        ^
<source>:6:61: error: declaration of 'N' shadows template parameter
    std::vector<std::vector<int>> data_(N, std::vector<int>(N));
                                                            ^
<source>:4:23: note: template parameter is declared here
template <std::size_t N>
like image 639
Lev Avatar asked Mar 26 '26 21:03

Lev


2 Answers

Default member initializer (since C++11) doesn't support parentheses initializer, but only braced initializer and equal-sign initializer.

Through a default member initializer, which is a brace or equals initializer included in the member declaration and is used if the member is omitted from the member initializer list of a constructor.

Beside the braced one you've showed, you can also

template <std::size_t N>
struct Matrix {
    std::vector<std::vector<int>> data_ = std::vector<std::vector<int>>(N, std::vector<int>(N));
};
like image 115
songyuanyao Avatar answered Mar 31 '26 17:03

songyuanyao


() has issue with regular declaration with most-vexing parse.

To avoid this possible issue, in class member initialization disallows that syntax.

like image 26
Jarod42 Avatar answered Mar 31 '26 17:03

Jarod42