c++ - Initialization by user-defined constructor -
consider following program:
#include <iostream> struct { a(int, int){ std::cout << "a(int, int)" << std::endl; } }; a(4,4); b{4,4}; c({4,4}); int main(){ }
output:
a(int, int) a(int, int) a(int, int)
i' m intereste in there difference between initialization of a
, b
, c
? of theme direct initialized.
a a(4,4);
bog standard direct-initialization.
a b{4,4};
bog standard direct-list-initialization. since a
has no initializer_list
constructor, ends doing same thing above. no std::initializer_list
object ever constructed or destroyed.
a c({4,4});
this direct-initializes c
a
temporary, in turn copy-list-initialized braced-init-list {4, 4}
. note won't work if a::a(int, int)
explicit
, since in case copy-list-initialization ill-formed , left no viable constructor call. construction , destruction of temporary may elided, , is.
Comments
Post a Comment