我可以强制默认的特殊成员函数为noexcept吗?

时间:2013-09-11 00:43:25

标签: c++ c++11 noexcept

以下结构无法在C ++ 11下编译,因为我已将移动赋值运算符声明为noexcept

struct foo
{
  std::vector<int> data;
  foo& operator=(foo&&) noexcept = default;
};

编译器生成的默认移动赋值运算符为noexcept(false),因为std::vector<int>的移动赋值也是noexcept(false)。这反过来是由于默认分配器将std::allocator_traits<T>:: propagate_on_container_move_assignment设置为std::false_type。另请参阅this question

我相信这已在C ++ 14中修复(见library defect 2103)。

我的问题是,我有没有办法强制noexcept在默认的移动分配赋值运算符上而不必自己定义它?

如果无法做到这一点,有没有办法可以让std::vector<int>欺骗noexcept移动可分配,以便将noexcept(true)传递给我的结构?

2 个答案:

答案 0 :(得分:6)

  

我相信这已在C ++ 14中修复(参见库缺陷2103)。

作为修复的DR应该被视为对C ++ 11的修正,因此一些C ++ 11实现已经修复了它。

  

我的问题是,我有没有办法强制noexcept在默认的移动分配赋值运算符上而不必自己定义它?

对于默认的移动赋值运算符为noexcept,您需要使其子对象具有noexcept移动赋值运算符。

我能想到的最明显的可移植方式是使用std::vector周围的包装,强制移动为noexcept

template<typename T, typename A = std::allocator<T>>
  struct Vector : std::vector<T, A>
  {
    using vector::vector;

    Vector& operator=(Vector&& v) noexcept
    {
      static_cast<std::vector<T,A>&>(*this) = std::move(v);
      return *this;
    }
    Vector& operator=(const Vector&) = default;
  };

另一个类似的选择是使用DR 2013修复定义您自己的分配器类型并使用:

template<typename T>
  struct Allocator : std::allocator<T>
  {
    Allocator() = default;
    template<typename U> Allocator(const Allocator<U>&) { }
    using propagate_on_container_move_assignment  = true_type;
    template<typename U> struct rebind { using other = Allocator<U>; };
  };

template<typename T>
  using Vector = std::vector<T, Allocator<T>>;

另一种选择是使用标准库实现,例如GCC,它实现DR 2013的解析,并且当知道所有分配器类型时,也为std::vector的移动赋值运算符noexcept allocator实例比较相等。

答案 1 :(得分:1)

我认为你不能逼迫任何东西,但你可以把它包起来:

#include <iostream>
#include <vector>

template <typename T>
struct Wrap
{
    public:
    Wrap() noexcept
    {
        new (m_value) T;
    }

    Wrap(const Wrap& other) noexcept
    {
        new (m_value) T(std::move(other.value()));
    }

    Wrap(Wrap&& other) noexcept
    {
        std::swap(value(), other.value());
    }


    Wrap(const T& other) noexcept
    {
        new (m_value) T(std::move(other));
    }

    Wrap(T&& other) noexcept
    {
        new (m_value) T(std::move(other));
    }

    ~Wrap() noexcept
    {
        value().~T();
    }

    Wrap& operator = (const Wrap& other) noexcept
    {
        value() = other.value();
        return *this;
    }

    Wrap& operator = (Wrap&& other) noexcept
    {
        value() = std::move(other.value());
        return *this;
    }

    Wrap& operator = (const T& other) noexcept
    {
        value() = other;
        return *this;
    }

    Wrap& operator = (T&& other) noexcept
    {
        value() = std::move(other);
        return *this;
    }

    T& value() noexcept { return *reinterpret_cast<T*>(m_value); }
    const T& value() const noexcept { return *reinterpret_cast<const T*>(m_value); }
    operator T& () noexcept { return value(); }
    operator const T& () const noexcept { return value(); }

    private:
    typename std::aligned_storage <sizeof(T), std::alignment_of<T>::value>::type m_value[1];
};


struct Foo
{
    public:
    Foo& operator = (Foo&&)  noexcept = default;

    std::vector<int>& data() noexcept { return m_data; }
    const std::vector<int>& data() const noexcept { return m_data; }

    private:
    Wrap<std::vector<int>> m_data;
};

int main() {
    Foo foo;
    foo.data().push_back(1);
    Foo boo;
    boo = std::move(foo);
    // 01
    std::cout << foo.data().size() << boo.data().size() << std::endl;
    return 0;
}

(谢谢Jonathan Wakely)

相关问题