Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "std::string(blablabla());" compile without errors?

Why does the following code compile without warnings. Notice that blablabla() is not defined anywhhere.

I tested it in gcc 5.1.0 and clang-3.7.0 (with and without the -std=c++11 flag).

#include <string>

int main()
{
    std::string(blablabla());
}

This questions is not a duplicate of the most vexing parse ambiguity, since related examples declare functions with a parameter.

like image 909
oo_miguel Avatar asked Oct 22 '15 06:10

oo_miguel


1 Answers

Ahhhh I'm dumb.

It's not treated as a call. The compiler just sees it as a declaration...

Try, for comparison:

int main() {
    int(blablabla);
}

This gives:

test.c++: In function ‘int main()’:
test.c++:6:9: warning: unused variable ‘blablabla’ [-Wunused-variable]
     int(blablabla);
         ^

More precisely, your statement std::string(blablabla()) declares blablabla to be a function returning an std::string, the same as

std::string blablabla();

would do. A local function declaration.

like image 88
Kamajii Avatar answered Nov 13 '22 14:11

Kamajii