@twonek @vpiotr @mwl4:
Konstruktor (destruktor) jest "funkcją o nazwie takiej jak nazwa klasy". Zatem http://melpon.org/wandbox/permlink/9MkXkGYC1glvoNvj jest "najbardziej poprawne". Dlaczego?
foo
to class template
. class template
nie jest klasą. Klasą (typem) jest foo<int>
. Stąd też konstruktor nazywa się foo<int>
.
Dlaczego zatem http://melpon.org/wandbox/permlink/ISwFsqsiLaRahThB działa?
Ficzer nazywa się class name injection
. Działa podobnie jakby w klasie foo
znajdował się member foo
wskazujący na *this
.
http://melpon.org/wandbox/permlink/8zpGWdPsUui2SHiX
linie 6, 13: typ foo<int>::foo
okazuje się być tym samym co foo<int>
linia 7: foo
to injected class name
, to samo co typ naszej klasy, czyli foo<int>
linia 8: podoba sytuacja. Zwracamy *this
i bindujemy to do foo
. Działa, chociaż *this
jest typu foo<int>
C++11 9.2: A class-name is inserted into the scope in which it is declared immediately after the class-name is seen.
The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name.
For purposes of access checking, the injected-class-name is treated as if it were a public member name. A
class-specifier is commonly referred to as a class definition. A class is considered defined after the closing
brace of its class-specifier has been seen even though its member functions are in general not yet defined.
The optional attribute-specifier-seq appertains to the class; the attributes in the attribute-specifier-seq are
thereafter considered attributes of the class whenever it is named.
Działa nawet w bardzo pokręconych przepadkach: http://melpon.org/wandbox/permlink/vbQ7fDiIJkmgn21E