Copy-Ctor in Template-Klasse



  • Morgen zusammen,

    ich bin heute auf ein (für mich) recht merkwürdiges Problem gestoßen. Folgendes Beispiel:

    template <class T, class U>
    class A {
      public:
        A() {}
        A( const A<T, U> &other ) { std::cout << "COPY" << std::endl; }
    };
    
    int main() {
      A<int,int>  a;
      A<int,int>  b( a );
    }
    

    Hier funktioniert alles sauber, der Kopier-Konstruktor wird wie erwartet gerufen. Nun kommt eine Funktion hinzu:

    A<int,int> Create() { return A<int,int>(); }
    

    Und Folgendes in main():

    A<int,int>  c( Create() );
    

    Hier wird der Kopier-Konstruktor nicht mehr gerufen. Warum ist das so? Ich kann keinen großen Unterschied zur vorherigen Variante feststellen. Oder wird etwa wegoptimiert, so dass nur der Standard-Konstruktor in der Funktion gerufen und außerhalb nicht wirklich kopiert wird? Nutze den GCC.

    Durch die Optimierung könnte ich mir ja noch Obiges erklären, aber wo ich dann völlig aussteige ist hierbei:

    template <class T, class U>
    class A {
      public:
        A() {}
        A( const A<T, U> &other ) { std::cout << "COPY" << std::endl; }
    };
    
    typedef A<int,int>  intA;
    
    int main() {
      A<int,int>  a( intA );
    }
    

    Quasi dasselbe wie das obige Beispiel, nur statt einem direkten A<int,int> das Benutzen eines typedef.

    Hilfe wird hier mehr als dankend angenommen. 😉



  • Der C++ Standard erlaubt Compilern, "unnötige" Kopien einzusparen -- auch wenn Kopier-Konstruktor und/oder Destruktor Seiteneffekte besitzen. In Deinem Fall hat wahrscheinlich die Funktion das Objekt direkt in c erzeugt. Das läuft dann in der Regel so, dass der Funktion in Wirklichkeit nur ein Zeiger für den Rückgabewert übergeben wird. In diesem Fall zeigt der Zeiger direkt auf die Stelle, wo das c-Objekt erzeugt werden soll. Wie Objekte übergeben werden ist allerdings Sache der "Implementierung" (also des C++ Compilers).

    N1804 (2005/04/27), 12.8/15 schrieb:

    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. 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

    Gruß,
    SP



  • Und bzgl.

    typedef A<int,int>  intA; 
    
    int main() { 
      A<int,int>  a( intA ); 
    }
    

    Hiermit erzeugst du keine Variable vom Typ A<int, int>, sondern dies ist dann eine normale Funktionsdeklaration, d.h. a ist eine Funktion, welche ein A<int, int> als Parameter sowie als Rückgabetyp erwartet.

    Du müßtest dann schon

    A<int,int>  a( intA() );
    

    schreiben (bzw. wegen möglicher Mehrdeutigkeit beim Parsen wird dies auch noch als Funktionsdeklaration gewertet), d.h. die wirklich richtige Lösung lautet:

    A<int,int>  a( (intA()) ); // mit Klammern!!!
    

    s.a. http://www.c-plusplus.net/forum/viewtopic-var-t-is-250511-and-start-is-0.html (mit ähnlicher Problematik)



  • Super, ich danke für eure Antworten, das erklärt einiges. Merkwürdig, dass ich da vorher noch nicht drauf gestoßen bin. 😉


Anmelden zum Antworten