I\'m a C++ beginner and would like to understand why
return std::list();
needs parentheses, but
std::list
look at it this way:
1) you need to create an object
2) you need to return it.
let's say the compiler looks at the expression return Foo; , the compiler thinks "hey! he wants me to return a type! a type is not a thing that I can return! I need a true variable here!"
so you can write something like
Foo temp;
return temp;
or make it shorter - call the default constructor of Foo , then return the Anonymous object I just created. you treat the constructor as a function that produces an object.
does the code return createDefaultFoo(); looks much more reasonable? well, this is what Foo() does, it creates and returns anonymous Foo obejct
in this line :
std::list foo;
the compiler can tell you want an object named foo from the type std::list. so the () are redundand. as answered here, adding the () will make the compiler think you declare a function.