Why does C++ treat Window w(WindowConfig()); as a function declaration, and how do you write it so it creates an object?
answer
- the parser prefers declarations
- a parameter type that looks like a call
- function returning WindowConfig, adjusted to a pointer
- braces cannot be a declarator
- extra parentheses or = also work
basics
~20 sC++ resolves the ambiguity in favour of a declaration: WindowConfig() can be read as an unnamed parameter of type function returning WindowConfig, so w becomes a function. Window w{WindowConfig{}}; or Window w((WindowConfig())); declares an object instead.
solid answer
~40 sThe grammar allows `Window w(WindowConfig());` to be read two ways: an object `w` initialized from a value-initialized temporary, or a function `w` returning `Window` that takes an unnamed parameter of type "function returning `WindowConfig`", which is adjusted to a pointer to function. The standard's ambiguity rule says anything that **can** be a declaration **is** one, so `w` is a function and `w.width` fails to compile. The simpler cousin is `Timer t();`, which declares a function, not a value-initialized object. Fixes: braces, which cannot form a parameter list — `Window w{WindowConfig{}};`; an extra pair of parentheses around the argument — `Window w((WindowConfig()));`; or copy-initialization — `auto w = Window(WindowConfig());`, which since C++17 constructs `w` directly. Clang warns with `-Wvexing-parse` by default.
code
cpp · 15 lines#include <iostream>
struct WindowConfig { int width = 1280; int height = 720; };
struct Window {
explicit Window(WindowConfig cfg) : width(cfg.width) {}
int width;
};
int main() {
Window w1{WindowConfig{}}; // braces: always an object
Window w2((WindowConfig())); // extra parentheses: an expression, not a parameter
auto w3 = Window(WindowConfig());
std::cout << w1.width << ' ' << w2.width << ' ' << w3.width << '\n';
}go deeper
Recognise that Timer t(); and Window w(WindowConfig()); both declare functions rather than objects, and know that braces fix it.
State the declaration-wins rule, name the deduced function type, and give two spellings that declare an object.
Choose a fix that does not change meaning on types with initializer-list constructors, and spot the parse behind a confusing error far from its cause.
Decide whether brace initialization becomes the house style partly to rule this out, given the initializer-list trade-off it brings.
## The ambiguity C++ inherited from C a declaration syntax in which parentheses can appear around declarators and function parameter lists look like call arguments. That creates statements with two valid parses: - `Window w(WindowConfig());` can be an **object declaration**: `w` is a `Window` direct-initialized from `WindowConfig()`, a value-initialized temporary. - It can equally be a **function declaration**: `w` is a function returning `Window` whose single unnamed parameter has type `WindowConfig()` — "function taking nothing and returning `WindowConfig`" — which, like an array parameter, is adjusted to a pointer: `WindowConfig (*)()`. The standard resolves this in the section on ambiguity resolution: any construct that could possibly be a declaration **is** a declaration. So `w` is a function, the line compiles, and the error appears later, at the first use: ```plaintext warning: parentheses were disambiguated as a function declaration [-Wvexing-parse] note: add a pair of parentheses to declare a variable error: member reference base type 'Window (WindowConfig (*)())' is not a structure or union ``` That is Clang's output for `w.width`; the type in the error message, `Window (WindowConfig (*)())`, is the giveaway. ## The everyday cousin `Timer t();` is the same rule with no argument at all: empty parentheses after a name form a function declarator, so `t` is a function returning `Timer`. Clang reports "empty parentheses interpreted as a function declaration [-Wvexing-parse]". The intended value-initialized object is `Timer t{};`, and the intended default-initialized one is `Timer t;`. ## Ways to say "object" | Spelling | Why it is an object | |---|---| | `Window w{WindowConfig{}};` | a braced list cannot be a parameter list | | `Window w((WindowConfig()));` | a parenthesized expression cannot be a parameter declaration | | `Window w = Window(WindowConfig());` | `=` begins an initializer | | `auto w = Window(WindowConfig());` | `=` begins an initializer; since C++17 the temporary is not materialized, `w` is constructed directly | Each has a cost to weigh: - Braces switch to **list-initialization**, which forbids narrowing and prefers initializer-list constructors. For a type with such a constructor, `T x{arg}` can mean something different from `T x(arg)`. - The extra-parentheses form is correct but easy to "clean up" by a well-meaning reviewer. - The `=` forms work even with an `explicit` constructor, because the right-hand side is already a `Window` prvalue; no implicit conversion to `Window` is needed. ## Where it hides in real code - Constructing from iterators: `std::vector<char> data(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());` declares a function named `data`; this is the textbook "most vexing parse". - Lock guards and scope guards: `std::lock_guard<std::mutex> lock(std::mutex());` is a declaration, not a lock — although the more common bug is forgetting the name entirely, which creates and destroys a temporary. - Test fixtures: `Fixture f();` in a test body compiles, and the test then fails to compile at the first member access, far from the cause. ## How compilers help Both major compilers now flag the pattern by default. GCC documents `-Wvexing-parse` as enabled by default and shows the declaration it chose (`extern struct S y (int (*) (void));` in its own example), suggesting braces or removing the parentheses. Clang's `-Wvexing-parse` is also on by default and adds a note telling you to add a pair of parentheses or remove the empty ones. The warning fires at the declaration, which is far more useful than the error that otherwise appears at the first member access. Do not silence it with `-Wno-vexing-parse`; fix the declaration instead. The rule cannot simply be reversed. C++ must keep accepting function declarations at block scope (`int helper(int);` inside a function is legal and occasionally used), and C code relies on the same grammar, so the language chose a fixed tie-breaker rather than guessing intent. ## Interview takeaway 1. State the rule: if it can be a declaration, it is. 2. Show the type the compiler deduced for the name. 3. Offer a fix and its trade-off: braces are the common answer, but they bring list-initialization semantics with them.
- What exactly is the parameter type in Window w(WindowConfig());?`WindowConfig()` in a parameter position is the type "function with no parameters returning `WindowConfig`". Parameters of function type are adjusted to pointer to function, so the declared function `w` has type `Window(WindowConfig (*)())`: it takes a pointer to a function returning `WindowConfig` and returns a `Window`.
- Why is fixing the parse with braces not always a drop-in change?Braces make the declaration list-initialization. If the type has an initializer-list constructor, `T x{arg}` may call it instead of the constructor the parentheses called, and any narrowing conversion of `arg` becomes ill-formed. For containers in particular, `std::vector<int> v{n}` creates one element rather than `n`.
- Timer t(); creates a value-initialized Timer
- Window w(WindowConfig()); is a compile error on its own line
- it only happens with built-in types
- braces fix it with no other change in meaning
- the parser picks whichever reading the programmer intended