user2969540
user2969540

Reputation: 1

What is the diff between following declarations?

vector<double> salaries();
vector<double> salaries;

I wanted to declare a empty vectors and above two declarations are possible as compiler returned no error. What is the difference between two above declarations?

Upvotes: 0

Views: 58

Answers (1)

twalberg
twalberg

Reputation: 62379

With the update, the two declarations you show are:

  1. vector<double> salaries(); // function named salaries that takes no parameters and returns a vector<double>
  2. vector<double> salaries; // a variable with type vector<double> that is default-constructed

Note that (1) is sometimes written with the intention of doing the same thing as (2) (i.e. using the default constructor). But that's not how the compiler sees it - this is often referred to as a "most vexing parse", which you should be able to find much more information on with a little searching. Basically, though, it's an (intentional) ambiguity in the language syntax, that is resolved by the language specs by requiring that particular syntax to be treated as a function, but still surprises a lot of people.

Upvotes: 1

Related Questions