简体   繁体   中英

Why doesn't constructor call?

I tried to examine behavior of the move constructor call:

#include <iostream>

struct A
{
    A(){ std::cout << "A()" << std::endl; };
    A(A&){ std::cout << "A(A&)" << std::endl; };
    A(A&&){ std::cout << "A(A&&)" << std::endl; };
};

A foo(){
    return A();
}

A t(A()); //produce no output, but because of A() is prvalue, I expected A(A&&) was produced
A d(foo()); //OK, produces A()\n A(A&&)\n A(A&&)

int main(){ }

DEMO

Could you explain that behavior?

This

A t(A()); 

is an abstract declaration of another function.

is a function declaration that has return type A and one parameter of type A() where A() in turn a function type-id.

Consider for example

#include <iostream>

int t( int() );
int t( int ( x ) ) { return x; }

int main() 
{
    std::cout << t( 10 ) << std::endl;
}   

Here is int() is a type-id with an abstract declarator of a function. It is very

Consider another abstract declarator char[10] . There is also type-id with abstract declarator [10] . You could write for example

char ( ( [10] ) )

So this

void f( char ( ( [10] ) ) );

is a valid function declaration.

You may omit parameter identifiers if they are not used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM