Why can’t the default constructor be called with empty brackets?

Most vexing parse This is related to what is known as “C++’s most vexing parse”. Basically, anything that can be interpreted by the compiler as a function declaration will be interpreted as a function declaration. Another instance of the same problem: std::ifstream ifs(“file.txt”); std::vector<T> v(std::istream_iterator<T>(ifs), std::istream_iterator<T>()); v is interpreted as a declaration of function with … Read more

Error calling user-defined operator+ on temporary object when there are extra brackets

It’s a cast syntax. The reason for that is that casting and the unary addition, subtraction and multiplication (the dereference operator) have higher precedence than their binary counterparts. Since the white spaces here don’t matter this can also be read as: A a = (A()) +A(); The cast and unary+ have higher precedence than the … Read more

Visual Studio C++ compiler weird behaviour

T(i_do_not_exist); is an object declaration with the same meaning as T i_do_not_exist;. N4567 § 6.8[stmt.ambig]p1 There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion (5.2.3) as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those … Read more

Why won’t this compile without a default constructor?

Clang gives this warning message: <source>:12:16: warning: parentheses were disambiguated as redundant parentheses around declaration of variable named ‘num’ [-Wvexing-parse] Boo(num); // No default constructor ^~~~~ This is a most-vexing parse issue. Because Boo is the name of a class type and num is not a type name, Boo(num); could be either the construction of … Read more

Why does C++ allow us to surround the variable name in parentheses when declaring a variable?

Grouping. As a particular example, consider that you can declare a variable of function type such as int f(int); Now, how would you declare a pointer to such a thing? int *f(int); Nope, doesn’t work! This is interpreted as a function returning int*. You need to add in the parentheses to make it parse the … Read more

Default constructor with empty brackets

Most vexing parse This is related to what is known as “C++’s most vexing parse”. Basically, anything that can be interpreted by the compiler as a function declaration will be interpreted as a function declaration. Another instance of the same problem: std::ifstream ifs(“file.txt”); std::vector<T> v(std::istream_iterator<T>(ifs), std::istream_iterator<T>()); v is interpreted as a declaration of function with … Read more