为什么从std::string的initializer列表中填充std::向量不会调用std::字符串构造函数
Why does filling a std::vector from an initilizer list of std::string does not call std::string constructor
我有以下类:
class StringContainer {
public:
StringContainer(const std::string& s1, const std::string& s2) {
string_array_ = {s1, s2};
}
std::vector<std::string> string_array_;
};
然后我从这个结构中创建一个对象,如下所示:
StringContainer con(s1, s2);
其中s1和s2是本地定义的非常量std::字符串变量。我注意到初始值设定项列表中的string_array_赋值至少不会调用std::string的构造函数一次。(不适用于初始值设定项列表中的临时对象或插入到向量中的对象)。我读到,这可能适用于平凡可构造类,但平凡可构造性的要求之一是具有隐式默认构造函数/副本构造函数。但是std::string(std::basic_string)已经定义了两者。我在这里错过了什么?如果我想写一个字符串类来利用这种优化,我应该怎么做?
我的调试器让我确信构造函数是被调用的,但如果我不能使用调试器,我决定做一些小实验来获得证据。你也可以自己试试。
创建std::string的子类,用string2替换代码中的std::字符串,添加一些打印输出,并获得构造函数被调用的可视化证明:
class string2 : public string
{
public:
string2(std::string s) : std::string(s)
{
std::cout << "string2 constructor" << std::endl;
}
string2(const string2& s) : std::string(s)
{
std::cout << "string2 copy constructor" << std::endl;
}
};
可能是因为COW?
参见:
- C++11内部std::字符串表示法(libstdc++)
引用那里的报价:
* This approach has the enormous advantage that a string object
* requires only one allocation. All the ugliness is confined
* within a single %pair of inline functions, which each compile to
* a single @a add instruction: _Rep::_M_data(), and
* string::_M_rep(); and the allocation function which gets a
* block of raw bytes and with room enough and constructs a _Rep
* object at the front.
另请参阅:
- STL中的写时复制支持
相关文章:
- std::向量与传递值的动态数组
- 如何在旧c++中初始化const-std向量
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 当键值是 std 向量时,为什么使用 at in C++ 访问映射值如此缓慢?
- 如何初始化 std::向量的映射?
- 迭代时将指向differents类型的指针保存在std::向量中
- std::向量迭代器和调整大小/保留的奇怪/有趣行为
- yaml-cpp到std::向量迭代的怪异行为
- 故意泄漏std::向量的内存
- 调整STD ::向量的大小是否可以降低其能力
- std ::向量距离函数如何给出比.size()更高的值
- C++ std::向量插入两个元素替代算法失败
- 错误:数字常数之前的预期无限制ID:std :: array和std ::向量大小分配
- 在我提供的此示例中,如何将2维std ::向量的逻辑更改为具有向量[row] [col] [col] [col] [co
- GUI滑块的动态数量,该数量更新具有回调中值的std ::向量
- 是一个std ::向量线程,我的目的是安全的
- 将具有std ::向量的C 函数称为Julia的输入和输出参数
- 将std ::向量作为指针参考
- STD ::向量如何调整其内部缓冲区大小
- 在C 中,是否有可能在不兼容类型的std ::向量对象之间传输不同类型的缓冲区