Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the brace-or-equal initializer brace-or-equal? [duplicate]

Tags:

c++

#include <iostream>
#include <vector>

struct S {
    //std::vector<int> ns(1); //ERROR!
    std::vector<int> ns = std::vector<int>(1);
};

int main() {
    S s;
    std::cout << (s.ns[0] = 123) << std::endl;
    return 0;
}

Using the parentheses initializer seems to be an error. What is the purpose behind this.


1 Answers

The idea is to flat out reject any syntax that could be interpreted as a function declaration. For example,

std::vector<int> ns();

is a function declaration. These aren't:

std::vector<int> ns{};
std::vector<int> ns = std::vector<int>();

For consistency, any member declaration with this form

T t(args...);

is disallowed, which avoids a repeat of the most vexing parse fiasco.

like image 103
juanchopanza Avatar answered Dec 06 '25 13:12

juanchopanza