Limitations of C + + replication initialization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.