3
#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 Answers1

10

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    Why would you write `T t();` instead of `T t;`? –  May 28 '15 at 20:47
  • 3
    @xiver77 This is value initialization vs. default initialization. If `T` is an built-in type, or an aggregate with built-in members, it makes a difference: value initialization leads to zero initialization, and default initialization doesn't. – juanchopanza May 28 '15 at 20:48