显式复制构造函数的行为和实际用途 [英] Explicit copy constructor behavior and practical uses

查看:104
本文介绍了显式复制构造函数的行为和实际用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近的一个问题让我想知道显式副本构造函数。这是我尝试在Visual Studio 2005下编译的示例代码:

 结构A 
{
A (){}
显式A(const A&){}
} ;;

//#1>编译错误(预期行为)
A retByValue()
{
return A();
}

//#2>编译很好,但是为什么呢?
void passByValue(A a)
{
}

int main()
{
A a;
A b(a); //#3>显式复制构造:OK(预期行为)
A c = a; //#4>隐式副本构造:KO(预期行为)

//在多次注释后添加:根据VS 2005,这不是错误。
passByValue(a);
返回0;
}

现在开始提问:




  • 标准是否允许#2?如果是,相关部分将描述这种情况吗?

  • 您知道显式拷贝构造函数的实际用法吗?



我刚刚在 MSDN 具有完全相同的情况,并且来自主要功能的神秘注释:复制了c(好像很明显)。正如Oli Charlesworth指出的那样:gcc不会编译此代码,我相信他没有这样做。

解决方案

我相信相关章节C ++ 03的§12.3.1 2:


显式构造函数与非显式构造函数一样构造对象,但仅在直接初始化语法( 8.5 )或在其中进行强制转换的地方( 5.2.9 5.4 )被明确使用。默认构造函数可以是显式构造函数。这样的构造函数将用于执行默认初始化或值初始化( 8.5 )。


§8.5 12:


在参数传递,函数返回,引发异常( 15.1 ),处理异常( 15.3 ),以及用大括号括起来的初始化程序列表( 8.5.1 )称为复制初始化,等效于以下形式

  T x = a; 

在新表达式中进行的初始化( 5.3.4 ),static_cast表达式( 5.2.9 ),功能符号类型转换( 5.2.3 )以及基本和成员初始化程序( 12.6.2 )称为直接初始化,等效于以下形式

  T x(a) ; 


调用 passByValue(a)
,code>涉及复制初始化,而不是直接初始化,因此应该是错误。

A recent question got me wondering about explicit copy constructors. Here is a sample code that I tried compiling under Visual Studio 2005 :

struct A
{
    A() {}
    explicit A(const A &) {}
};

// #1 > Compilation error (expected behavior)
A retByValue()
{
    return A();
}

// #2 > Compiles just fine, but why ?
void passByValue(A a)
{
}

int main()
{
    A a;
    A b(a); // #3 > explicit copy construction : OK (expected behavior)
    A c = a; // #4 > implicit copy construction : KO (expected behavior)

    // Added after multiple comments : not an error according to VS 2005.
    passByValue(a);
    return 0;
}

Now for the questions :

  • Is #2 allowed by the standard ? If it is, what is the relevant section describing this situation ?
  • Do you known of any practical use for an explicit copy constructor ?

[EDIT] I just found a funny link on MSDN with the exact same situation, and a mysterious comment from the main function : "c is copied" (as if it was obvious). As pointed by Oli Charlesworth : gcc does not compile this code and I believe he's right not to.

解决方案

I believe the relevant sections of C++03 are §12.3.1 2:

An explicit constructor constructs objects just like non-explicit constructors, but does so only where the direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are explicitly used. A default constructor may be an explicit constructor; such a constructor will be used to perform default-initialization or value-initialization (8.5).

and § 8.5 12:

The initialization that occurs in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and brace-enclosed initializer lists (8.5.1) is called copy-initialization and is equivalent to the form

    T x = a;

The initialization that occurs in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initialization and is equivalent to the form

    T x(a);

Calling passByValue(a) involves copy-initialization, not direct-initialization, and thus should be an error, according to C++03 § 12.3.1 2.

这篇关于显式复制构造函数的行为和实际用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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