2012-11-15 9 views
10

देखो:अस्थिर और आभासी सदस्यों द्वारा प्रभावित सी ++ संश्लेषित चालक कन्स्ट्रक्टर कैसा है? निम्नलिखित कोड पर

struct node 
{ 

    node(); 
    //node(const node&); //#1 
    //node(node&&);   //#2 

    virtual     //#3 
    ~node(); 

    node* 
    volatile    //#4 
    next; 

}; 

int main() 
{ 

    node m(node());   //#5 
    node n=node();   //#6 
} 

साथ संकलित जब जीसीसी-4.6.1 यह निम्न त्रुटि पैदा करता है:

g++ -g --std=c++0x -c -o node.o node.cc 
node.cc: In constructor node::node(node&&): 
node.cc:3:8: error: expression node::next has side-effects 
node.cc: In function int main(): 
node.cc:18:14: note: synthesized method node::node(node&&) first required here 

मैं समझता हूँ संकलक डिफ़ॉल्ट चाल बनाने के लिए विफल रहता है या पर निर्माता के रूप में कॉपी लाइन # 6, अगर मैं लाइन # 1 या # 2 को अपूर्ण करता हूं तो यह ठीक से संकलित होता है, यह स्पष्ट है। कोड C++ 0x विकल्प के बिना ठीक संकलित करता है, इसलिए त्रुटि डिफ़ॉल्ट चाल कन्स्ट्रक्टर से संबंधित है।

हालांकि, नोड वर्ग में क्या डिफ़ॉल्ट चाल निर्माता बनने से रोकता है? अगर मैं किसी भी पंक्ति # 3 या # 4 पर टिप्पणी करता हूं (यानी विनाशक गैर-वर्चुअल बनाते हैं या डेटा सदस्य को अस्थिर बनाते हैं) तो यह फिर से संकलित होता है, तो क्या यह इन दोनों का संयोजन संकलित नहीं करता है?

एक और पहेली, रेखा # 5 संकलन त्रुटि का कारण नहीं है, लाइन # 6 से अलग क्या है? क्या यह सभी जीसीसी के लिए विशिष्ट है? या जीसीसी-4.6.1?

+1

आप 'मुख्य' के रिटर्न प्रकार को भूल गए हैं skippy –

उत्तर

12

[C++11: 12.8/9]:If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared copy assignment operator,
  • X does not have a user-declared move assignment operator,
  • X does not have a user-declared destructor, and
  • the move constructor would not be implicitly defined as deleted.

[ Note: When the move constructor is not implicitly declared or explicitly supplied, expressions that otherwise would have invoked the move constructor may instead invoke a copy constructor. —end note ]

यही कारण है कि आपका # 3 संश्लेषण तोड़ रहा है।

इसके अतिरिक्त, it's far from clearvolatile types (including your node* volatile) are trivially copyable; यह निष्कर्ष निकाला जा सकता है कि it is implementation-defined whether they are or not और, आपके मामले में, ऐसा लगता है कि वे नहीं हैं।

कम से कम, v4.7 में GCC made it stop working quite deliberately, एक प्रस्ताव v4.6.1 कि मैं केवल आगे बढ़े अनुमान कर सकते हैं में backport के साथ ...

तो, निम्नलिखित दिया:

[C++11: 12.8/11]: An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/move constructor for a class X is defined as deleted (8.4.3) if X has:

  • a variant member with a non-trivial corresponding constructor and X is a union-like class, a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M ’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,
  • a direct or virtual base class B that cannot be copied/moved because overload resolution (13.3), as applied to B ’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,
  • any direct or virtual base class or non-static data member of a type with a destructor that is deleted or inaccessible from the defaulted constructor,
  • for the copy constructor, a non-static data member of rvalue reference type, or
  • for the move constructor, a non-static data member or direct or virtual base class with a type that does not have a move constructor and is not trivially copyable.

... यही कारण है कि आपका # 4 संश्लेषण तोड़ रहा है, स्वतंत्र रूप से # 3 का।

# 5 का सवाल है, कि नहीं वास्तव में एक node की घोषणा बिल्कुल है, लेकिन एक समारोह के लिए एक घोषणा में कहा जाता m — है यही कारण है कि यह एक node के निर्माण से संबंधित (इस Most Vexing Parse के रूप में जाना जाता है) लक्षण प्रजनन नहीं कर रहा है ।

+0

धन्यवाद, आपने इसे बहुत स्पष्ट बना दिया है। अभी भी मुझे क्या पहेली है, यह है कि यह कोड ठीक काम करता है: – user1827766

+0

अभी भी मुझे क्या पहेली है, यह है कि यह कोड ठीक काम करता है: struct node {~ node(); नोड * अगली अस्थिर; }; और यह भी: संरचना नोड {वर्चुअल ~ नोड(); नोड * अगला; }; इसलिए gcc-4.6.1 अस्थिर डेटा सदस्यों और विनाशकों के साथ कक्षाओं के लिए ctors को स्थानांतरित करने की अनुमति देता है लेकिन किसी भी तरह वर्चुअल dtor/अस्थिर डेटा सदस्य संयोजन पर शिकायत करता है। – user1827766

+0

मुझे आश्चर्य है कि अस्थिरता के साथ समस्या को तुलनीय रूप से कॉपी करने योग्य मुद्दा केवल परमाणु अर्थशास्त्र है कि कुछ कार्यान्वयन इसे देना चाहते हैं। यदि ऐसा है तो मुझे लगता है कि यह परिवर्तन उचित नहीं होगा, क्योंकि आईएसओ सी ++ अस्थिरता में परमाणु अर्थशास्त्र नहीं है – bames53

संबंधित मुद्दे