Reputation: 155
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>
Upvotes: 2
Views: 249
Reputation: 172924
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));
};
Upvotes: 4
Reputation: 217283
()
has issue with regular declaration with most-vexing parse.
To avoid this possible issue, in class member initialization disallows that syntax.
Upvotes: 4