c++ - std::set::insert() 可以调用赋值运算符吗?

标签 c++ c++11 stl

我有一个带有自定义赋值运算符的类,它故意引入受控的副作用。

如果我有这些元素的 std::set,我想知道是否可以使用 insert 调用赋值运算符。

即:

class A
{
public:
    A & operator=(A const & rhs)
    {
        // custom assignment operator with deliberate side effects
        // Could this be called with use of std::set::insert()?
    }
};

std::set<A> a;

// Is it possible with this, or any use of "insert", that
// the assignment operator will ever be called?

a.insert(A());

可以使用std::set::insert调用赋值运算符吗?

最佳答案

在 C++11 之前的版本中,理论上是可以的。毫无疑问,C++11 禁止它 因为没有任何实现真正做到过。在 C++11 中, std::set 的成员不必支持赋值。 在 C++11 之前的版本中,一些支持概念的实现可能 有使用赋值的代码,作为检查的一种方法 你的类(class)支持它,但它应该是非评估的 上下文,因此实际上不会调用该运算符。

关于c++ - std::set::insert() 可以调用赋值运算符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860575/

相关文章:

c++ - 为什么 std::future 和 std::promise 不是最终的?

c++ - C++11 标准中的哪个子句允许我在下面的 `A` 中删除 `return` 语句中的 `A::operator-()`?

c++ - 是否可以使用 lower_bound() 对普通结构数组进行二进制搜索?

c++ - 将指针容器转换为智能指针?

c++ - 在 Raspberry Pi 2 上编译 OpenCV

c++ - C++ 中的 "query parameter"是什么?

c++ - 在对象实例上调用静态函数时解析错误?

c++ - 检查类型实例是否可以流式传输

c++ - 为什么 unique_ptr 有两个函数 reset 和 operator= 做类似的事情但不重载?

c++ - PIMPL:导出具有单个 STL 成员的类 (std::unique_ptr)