Replication initialization is more restrictive than direct initialization.
1: The implicit conversion of a constructor declared as explicit cannot be used when copying initialization. The direct initialization is allowed:
struct EXP {explicit exp (const char*) {}}; Not convertible from const CHAR*EXP E1 ("abc"); OKEXP e2 = "ABC"; Error, Copy-initialization does not consider explicit constructor struct IMP {imp (const char*) {}}; Convertible from const CHAR*IMP I1 ("abc"); Okimp i2 = "abc"; Ok
The constructor declaration in the EXP class is declared in order to explicit, therefore, the copy initialization of exp e2 = "ABC" will result in a compile error: Error:conversion from ' const char [4] ' to non-scalar type ' EXP ' req Uested.
The constructors in the Imp class are not declared as explicit, so you can use the string "ABC" to initialize directly or copy the object that initializes the Imp.
2: In replication initialization, when using implicit conversions, you must convert from the initializer (= right expression) directly to the initialized object instead of indirectly. In direct initialization, you can use implicit conversions from the initializer to the constructor arguments.
struct S {s (std::string) {}}; Implicitly convertible from Std::strings S1 ("abc"); Ok:conversion from const char[4] to std::strings s2 = "abc"; Error:no conversion from const CHAR[4] to SS s3 = std::string ("abc"); Ok:conversion from std::string to S
Class S has a constructor that accepts the std::string parameter, so you can use "Const char*" to initialize the object S1 of S directly. Here the conversion sequence is: const char*àstd::stringàstruct S;
In replication initialization, this conversion is not allowed, because it is not a direct conversion, but an indirect conversion, so the initialization of S2 will have a compilation error;
In replication initialization, you can use direct implicit conversions, so you can use std::string objects to initialize S3.
Reference:
Http://en.cppreference.com/w/cpp/language/copy_initialization
Limitations of C + + replication initialization