c++ - Default constructor with empty brackets -
is there reason empty set of round brackets (parentheses) isn't valid calling default constructor in c++?
myobject object; // ok - default ctor myobject object(blah); // ok myobject object(); // error
i seem type "()" automatically everytime. there reason isn't allowed?
most vexing parse
this related known "c++'s vexing parse". basically, can interpreted compiler declaration interpreted declaration.
another instance of same problem:
std::ifstream ifs("file.txt"); std::vector<t> v(std::istream_iterator<t>(ifs), std::istream_iterator<t>());
v
interpreted declaration of function 2 parameters.
the workaround add pair of parentheses:
std::vector<t> v((std::istream_iterator<t>(ifs)), std::istream_iterator<t>());
or, if have c++11 , list-initialization (also known uniform initialization) available:
std::vector<t> v{std::istream_iterator<t>{ifs}, std::istream_iterator<t>{}};
with this, there no way interpreted function declaration.
Comments
Post a Comment