初始化:括号对等号 [英] initialization: parenthesis vs. equals sign
问题描述
之间有什么区别
;
和
T a = b;
和
T a = T(b);
?
T a(b);
是直接初始化,除非它解析为函数声明,其中
T a = b;
是复制初始化,这意味着它的工作原理就像一个临时对象在右边构造,并且 a
然后被复制构造,或者在C ++ 11和更高版本中,可能从该临时构造移动构造。
编译器可以随时随地删除(删除)临时+复制/移动,但复制或移动构造函数(无论哪一个逻辑使用)都必须可访问, code>显式。
例如,在C ++ 03中,不能复制初始化 std :: ostringstream
,因为它没有复制构造函数。在C ++ 11中,如果初始化器是临时的,那么可以复制初始化 ostringstream
,这将导致逻辑移动构造(但是通常会被省略, )。例如,此副本初始化声明
ostringstream s = ostringstream(blah);
…不编译为C ++ 03,因为在C ++ 03中,复制初始化调用类的复制构造函数,它不存在。然而它编译为C ++ 11,因为在C + + 11复制初始化调用移动构造函数。而且(为了保持其作为流的幻觉),不能直接复制 std :: ostringstream
,它可以移动。 / p>
另一个这样的区别:在C ++ 03中只有复制初始化语法支持 initializer,在C ++ 03中, T
是聚合类型,例如原始数组。在C ++ 11中,大括号符号已经被扩展和推广为一个统一的初始化语法,因此它也可以用于直接初始化。因此,以下直接初始化声明,
int v [] {3,1,4,1,5,9,2 ,6,5,4};
…不会编译为C ++ 03,但编译为C ++ 11和更高版本。
=
复制初始化语法是C的原始初始化语法。
在C ++ 11和更高版本中,由于移动语义,它可以在更宽的范围比C ++ 03中的情况,如使用 std :: ostringstream
。
Possible Duplicate:
Is there a difference in C++ between copy initialization and assignment initialization?
What's the difference between
T a(b);
and
T a = b;
and
T a = T(b);
?
T a( b );
is direct initialization, unless it parses as a function declaration, in which case it's a function declaration.
T a = b;
is copy initialization, which means that it works as if a temporary object is constructed on the right hand side, and that a
is then copy constructed or, in C++11 and later, possibly move constructed, from that temporary.
The compiler is free to elide (remove) the temporary+copying/moving whenever it can, but a copy or move constructor, whichever would be logically used, must still be accessible and not explicit
.
For example, in C++03 you cannot copy-initialize a std::ostringstream
, because it doesn't have a copy constructor. In C++11 you can copy-initialize an ostringstream
if the initializer is a temporary, which then results in a logical move construction (which however will usually be elided, optimized away). For example, this copy initialization declaration,
ostringstream s = ostringstream( "blah" );
… doesn't compile as C++03, because in C++03 the copy initialization invokes the class' copy constructor, which doesn't exist. It does however compile as C++11, because in C++11 the copy initialization invokes the move constructor. And while (to maintain its illusion of being a stream) a std::ostringstream
can't be directly copied, it can be moved.
Another such difference: in C++03 only the copy initialization syntax supports curly braces initializer, which in C++03 you can use when T
is an aggregate type such as a raw array. In C++11 the curly braces notation has been extended and generalized as a uniform initialization syntax, so it can be used also with direct initialization. And so the following direct initialization declaration,
int v[]{ 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };
… does not compile as C++03, but does compile as C++11 and later.
The =
copy initialization syntax is the original initialization syntax from C.
And in C++11 and later, due to move semantics, it can be used in a much wider range of cases than in C++03, such as with a std::ostringstream
.
这篇关于初始化:括号对等号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!