basic_string类是否真的具有采用多个参数的复制构造函数,或者仅仅是构造函数? [英] Does basic_string class really have copy constructor that takes more than one parameter or is it just constructor?
问题描述
我正在阅读>为什么复制构造函数会有多个参数?.
公认的答案是:
旧的std::basic_string
确实也有一个:
basic_string(const basic_string& s,
size_type pos = 0, size_type n = npos)
但是 http://www.cplusplus.com/reference/string/basic_string/basic_string/表示:
basic_string (const basic_string& str, size_type pos, size_type len = npos,
const allocator_type& alloc = allocator_type());
以上不是复制构造函数,而是子字符串构造函数,用于复制str
从字符位置pos
开始并跨越len
个字符的部分.
The above isn't a copy constructor but substring constructor that copies the portion of str
that begins at the character position pos
and spans len
characters.
C ++标准部分说:
C++ standard section says that:
如果类X的非模板构造函数是X的副本构造函数, 第一个参数的类型为X&,const X& ;、易失性X&或const volatile X& ;,或者没有其他参数或其他所有参数 参数具有默认参数
A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments
那么,该链接接受的答案是否不正确?这真的是sub-string的basic_string类构造函数吗?我已经在C ++ 98,C ++ 11&中检查了此原型.链接&上的C ++ 14规范全部显示相同.
So, Is the accepted answer from that link is incorrect? Is this really basic_string class constructor for sub-string? I've checked prototype for this in C++98, C++11 & C++14 specification on the link & all shows the same.
推荐答案
(以前)C ++ 11国际标准[basic.string] p5中basic_string
类模板的规范包含以下两个构造函数(其他):
The specification of the basic_string
class template in the (former) C++11 International Standard [basic.string]p5 contains the following two constructors (among others):
basic_string(const basic_string& str);
// ...
basic_string(const basic_string& str, size_type pos, size_type n = npos,
const Allocator& a = Allocator());
第一个显然是副本构造函数,第二个显然不是副本构造函数.请注意,规范中没有没有构造函数,其中pos
具有默认参数.
The first one clearly is a copy constructor, the second one is no copy constructor. Note that there is no constructor in the spec where pos
has a default argument.
在C ++ 03,C ++ 14和C ++ 1z中,情况基本上相同.在C ++ 98中,这两个构造器确实是一个构造器:
The situation is essentially the same in C++03, C++14 and C++1z. In C++98, those two constructors have indeed been a single one:
basic_string(const basic_string& str, size_type pos = 0, size_type n = npos,
// ~~~~
const Allocator& a = Allocator());
但是由于 <罢工> 据我所知,允许使用C ++标准库的实现将这两个构造函数合并为一个,然后成为 copy构造:
As far as I know, an implementation of the C++ Standard Library is allowed to merge those two constructors into a single one, which then becomes a copy constructor:
basic_string(const basic_string& str, size_type pos = 0, size_type n = npos,
// ~~~~
const Allocator& a = Allocator());
这具有不同的行为.如LWG 42中所述(请参阅 Shafik Yaghmour的
This has different behaviour. As explained in LWG 42 (see Shafik Yaghmour's answer), the actual copy ctor of basic_string
acquires a copy of str
's allocator, whereas the "substring" constructor by default uses a value-initialized new object.
感谢塞巴斯蒂安·雷德尔指出这一点.
Thanks to Sebastian Redl for pointing that out.
这篇关于basic_string类是否真的具有采用多个参数的复制构造函数,或者仅仅是构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!