2009-05-19 8 views
3

コンパイル(G ++の下で)し、次のコードを実行すると "Foo :: Foo(int)"が出力されます。ただし、コピーコンストラクタと代入演算子をプライベートにした後、次のエラーでコンパイルできません。 "エラー: 'Foo :: Foo(const Foo &)'はプライベートです。実行時に標準コンストラクタを呼び出すだけの場合、コピーコンストラクタはどのように必要ですか?コンストラクタのアクセス規則

#include <iostream> 

using namespace std; 

struct Foo { 
    Foo(int x) { 
     cout << __PRETTY_FUNCTION__ << endl; 
    } 


    Foo(const Foo& f) { 
     cout << __PRETTY_FUNCTION__ << endl; 
    } 

    Foo& operator=(const Foo& f) { 
     cout << __PRETTY_FUNCTION__ << endl; 
     return *this; 
    } 
}; 

int main() { 
    Foo f = Foo(3); 
} 

答えて

15

コピーコンストラクタがここで使用されます:

Foo f(Foo(3)); 

コピーコンストラクタの呼び出しの再括弧の最初のセット:

Foo f = Foo(3); 

これは同等です。

Foo f(3); 

注コンパイラはコピーコンストラクタ呼び出しを離れて最適化することを選択するかもしれませんが、コピーコンストラクタはまだ(プライベートすなわちない)が使用可能でなければならないこと:あなたが言ってこれを避けることができます。 C++標準では、コピーコンストラクタの実装が実際に行っていることにかかわらず、特にこの最適化が可能です(12.8/15節参照)。

+0

コピーコンストラクタを使用すると主張する場合、なぜそれが出力に表示されませんか。オリジナルのポスターは、:: Foo(int)のみが呼び出されたことを明確に示しました。 – KIV

+0

@ Neilしかし、コピーコンストラクタは何かを明確にしているので、コンパイラがそれを最適化する可能性は低いです... –

+1

@Matthew:いいえ、標準では、コンパイラは単にそのような呼び出しを最適化して、副作用。その塩の価値がある現代のコンパイラ*はこの呼び出しを最適化します。一方、標準では、呼び出しが依然として可能でなければならないことも明確に述べられています。したがって、Neilの説明は正しいし、その点まである。 –

2

コンパイラが一時的な作成を避ける標準的な最適化の結果です。コンパイラは、たとえ副作用があっても(例のIOのような)構造と代入を単純な構造に置き換えることができます。

コンパイラがこの最適化を行うかどうかは、実際にはプログラムが不正であるかどうかに依存してはいけません。だからこそ

Foo f = Foo(3); 

はコピーコンストラクタが必要です。そして

Foo f(3); 

はありません。おそらく同じバイナリコードにつながるでしょう。 12.8.15

When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.111) This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value

— when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy

から

の引用も、 "戻り値の最適化" を参照してください。