How to call the right constructor of a template type?
Use braced-init-list(or uniform-initiation) to initlize the instance of Class
.
Class<std::string> a{ std::string("abc") }; // works
Class<std::string> b{ "abc" }; // also works
Class<std::string> b = "abc";
is copy-initialization. It doesn't work because it would involve two user-defined conversions:
- from
const char*
tostd::string
, - from
std::string
toClass<std::string>
.
But at most one is allowed.
When you write
Class<std::string> b("abc");
// or
Class<std::string> b{"abc"};
you use direct-initialization. It works because now only one user-defined conversion is used:
- from
const char*
tostd::string
.
Use direct initialization:
Class<std::string> b("abc");