According to this code :
#include <iostream>
using namespace std;
struct T {
T() { cout << "default"<<endl; }
T(string s) { cout << "ctor "<< endl; }
T(const T& t) { cout << "copy ctor"<< endl; }
};
int main()
{
T x = T(T(T()));
return 0;
}
output :
default
and according to standard:
(17) The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.
.....
(17.6.1) If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object. [Example: T x = T(T(T())); calls the T default constructor to initialize x. — end example]
it gives the same result in c ++ 11 (GNU GCC v7.1.1)
First question: Is the source type defined in this statement?
T x = T(T(T()));
Now if we add this function to this code :
string f(T t) {return "str";}
and replacement T x = T(T(f(T()))); for T x = T(T(T()));
output :
default
ctor
now is the source type defined?
isn't T() initializer expression?
if yes According to the standard, the default constructor should be called, but why is the T(string s) called for x?
Aucun commentaire:
Enregistrer un commentaire