简体   繁体   中英

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?

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) (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM