Direct initialization and initialization of a copy of a link

I read the "C ++ primer". To initialize an object, C ++ supports two forms of initialization: direct and copy. but the book does not refer to link initialization. And in the book, I never saw direct initialization (if any) of a link. All this is a copy:

int i; int &j = i;//but not int &j(i);which also works in my experiment 

I want to know that this is the same as initializing the link. for the following codes:

 string null_book = "9-999-99999-9"; 

First, the temporary tmp string object is initialized (for example), which will be initialized with the string style parameter c, and then initializes the null_book variable with the copy instance. That makes sense to me. for this:

 int &j = i; 

will j be initialized in the same way? It will be a temporary reference it & t (for example) initialized by i, and then initialize j with t? it does not make sense??? Why does the book never use direct initialization for reference? Thanks for attention!

+7
source share
3 answers

The main differences between direct initialization and initialization of a copy are described in paragraph 17 of standard 8.5 of the standard. In general, the difference is that for class types during copy-initialization explicit constructors are not considered (only constructor conversions are taken into account) and, possibly, the copy is fixed; direct initialization considers explicit constructors, and the goal is built directly. From section 8.5 of the standard:

14 - The initialization form (using parentheses or = ) is usually not significant, but it matters when the initializer or the object being initialized has the class type [...]

For nonclass types (including references), direct initialization and copy-initialization have similar semantics; for links, linking occurs in any case, as specified in 8.5.3 Links [dcl.init.ref] . Direct initialization and copying of link initialization have only different semantics in which the conversion function is involved ( 13.3.1.6 Initialization using the conversion function for direct link binding [over.match.ref] ); again, direct initialization is allowed to call explicit conversion functions where copying is not performed.

So in

 int &j = i; 

8.5.3p5 is applied, and the link j bound directly to lvalue i . No temporary numbers are called.

In terms of complexity, references are closer to fundamental (primitive) types than to class types. Primitives are initialized without a temporary build (8.5p17, the last bullet), and in general, links too. This is probably why the book uses the form = to initialize links; as with primitives, there is usually no difference, and the entry is int i = x; usually clearer than int i(x); .

+6
source

The terms copy-initialization and direct-initialization are part of the C ++ grammar. they don’t immediately tell you what code generation is. The meaning of any grammatical construction is described by the standard, and in the context of initialization there are many different special consequences depending on the types of things being initialized.

In particular, for primitive types, pointer types, and reference types, both direct and copy-initialization (grammatical construction!) Have exactly the same effect. That is, the fundamental and types of pointers are initialized with the value of the initializer, and the links are attached to the object referenced by the initializer:

 int a1 = 5; int a2(5); // same thing Foo * p1 = &x; Foo * p2(&x); // same thing Bar & b1 = y; Bar & b2(y); // same thing 

(However, for custom types, there is a difference between direct and copy-initialization, although this is subtle, which is usually not important.)

+4
source

The C ++ link is just a pointer in a funny hat. Statement:

 TYPE& x = y; 

always equivalent to:

 TYPE* const x = &y; 

All links are to save the character input * and declare their belief that there will never be null. Thus, initialization is trivial because it is the initialization of a pointer, not an object, and there is no difficulty with objects, in temporary or other ways.

This is a conceptual model; Of course, there are optimizations. If you declare a function like:

 void takeARef(FOO const& ref) { ... } 

and name it, then the compiler will just pass the pointer to the function and not create anything.

+3
source

All Articles