为什么不构造函数调用? [英] Why doesn't constructor call?

查看:178
本文介绍了为什么不构造函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查move constructor调用的行为:

  #include< iostream> 

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

foo(){
return A();
}

A t(A()); //产生没有输出,但由于A()是prvalue,我预期A(A&& amp;)被产生
A d(foo()); // OK,produce A()\\\
A(A&&)\\\
A(A&&)

int main(){}

DEMO



您能解释一下这种行为吗?

解决方案

  A t(A()); 

是另一个函数的抽象声明。



是返回类型 A 和一个类型 A()的参数的函数声明其中<$ c $



例如

  #include< iostream> 

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

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

这里是 int()是一个带有函数的抽象声明符的类型id。非常



考虑另一个抽象声明 char [10] 。还有type-id和抽象声明 [10] 。你可以写例如

  char(([10]))
pre>

所以这个

  void f(char ]))); 

是有效的函数声明。



如果未使用参数标识符,您可以省略。


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.

这篇关于为什么不构造函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆