निम्नलिखित कार्यक्रम पर विचार करें:रिटर्निंग temporaries/कॉपी ctor
#include<iostream>
using namespace std;
struct S
{
S() = default;
S(const S& other) = delete;
S(S&& other) = delete;
int i;
};
S nakedBrace()
{
return {}; // no S constructed here?
}
S typedBrace()
{
return S{};
}
int main()
{
// produce an observable effect.
cout << nakedBrace().i << endl; // ok
cout << typedBrace().i << endl; // error: deleted move ctor
}
नमूना सत्र:
$ g++ -Wall -std=c++14 -o no-copy-ctor no-copy-ctor.cpp
no-copy-ctor.cpp: In function 'S typedBrace()':
no-copy-ctor.cpp:19:12: error: use of deleted function 'S::S(S&&)'
return S{};
^
no-copy-ctor.cpp:8:5: note: declared here
S(S&& other) = delete;
यह मुझे हैरान है कि जीसीसी nakedBrace()
स्वीकार करता है। मैंने सोचा कि अवधारणात्मक रूप से दो कार्य समकक्ष हैं: अस्थायी S
का निर्माण और लौटाया गया है। प्रतिलिपि कॉपी करें या नहीं किया जा सकता है, लेकिन मानक (12.8/32) के अनुसार, चाल या प्रतिलिपि ctor (दोनों को यहां हटा दिया गया है) अभी भी सुलभ होना चाहिए।
क्या इसका मतलब है कि nakedBrace()
कभी एस नहीं बनाता है? या यह करता है, लेकिन सीधे ब्रेस-प्रारंभिकरण के साथ वापसी मूल्य में, ताकि कोई प्रतिलिपि/ctor अवधारणात्मक रूप से आवश्यक न हो?