Foo() vs. Foo{}

Hello,

I have two questions:
1. What is the difference between Foo() and Foo{} here:

1
2
3
4
5
6
7
8
9
10
struct Foo
{
    // ... Nothing
};

int main()
{
    throw Foo();
    throw Foo{};
}


2. What does Foo() in isolation do? What does it return?

I'd usually do something like this:
 
Foo bar();

To make an instance of Foo. So seeing Foo() in isolation has me confused.

Thanks!
Foo bar();
That is a function declaration.
The 'bar' is a function that takes no parameters and returns a value that has type 'Foo'.

This is often referred to as "C++'s most vexing parse".

C++11 did introduce brace initialization syntax. That is more uniform and easily distinguishable from function declaration.
Got it, I just read up about it and it seems like there are several methods to make it explicit that I'm defining a variable as opposed to a function.

1. Extra parentheses
2. Brace initialization
3. Copy initialization

Is any of them preferred? I hear the word "Copy" and I get scared.

Also, for my second question, is Foo() in isolation simply returning an instance of Foo default constructed?
Last edited on
Topic archived. No new replies allowed.