'noexcept = default'编译错误

'noexcept = default' compilation error

本文关键字:错误 编译 noexcept default      更新时间:2023-10-16

我在尝试编译下面的程序时收到以下错误。应该修改什么才能编译?看起来 GCC 4.9.2 无法实现一些 noexcept 移动构造函数作为"=default"。7号线造成麻烦:TabStats::TabStats(TabStats&& other) noexcept = default;

编译错误:/resource_coordinator/tab_stats.cc:14:1: 错误: 函数 'resource_coordinator::TabStats::TabStats(resource_coordinator::TabStats&&)' 在其重新声明时默认使用与隐式声明 'resource_coordinator::TabStats::

TabStats::TabStats(resource_coordinator:TabStats&&&)' 不同的异常规范 TabStats::TabStats(TabStats&& other) noexcept = default;

^

源代码

#include "browser/resource_coordinator/tab_stats.h"
#include "build/build_config.h"
namespace resource_coordinator {
TabStats::TabStats() = default;
TabStats::TabStats(const TabStats& other) = default;
TabStats::TabStats(TabStats&& other) noexcept = default;
TabStats::~TabStats() {}
TabStats& TabStats::operator=(const TabStats& other) = default;
}  // namespace resource_coordinator

tab_stats.h包括如下:

#ifndef BROWSER_RESOURCE_COORDINATOR_TAB_STATS_H_
#define BROWSER_RESOURCE_COORDINATOR_TAB_STATS_H_
#include <stdint.h>
#include <vector>
#include "base/process/process.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "build/build_config.h"
namespace content {
class RenderProcessHost;
}  // namespace content
namespace resource_coordinator {
struct TabStats {
  TabStats();
  TabStats(const TabStats& other);
  TabStats(TabStats&& other) noexcept;
  ~TabStats();
  TabStats& operator=(const TabStats& other);
  bool is_app = false;            // Browser window is an app.
  bool is_internal_page = false;  // Internal page, such as NTP or Settings.
  // Playing audio, accessing cam/mic or mirroring display.
  bool is_media = false;
  bool is_pinned = false;
  bool is_in_visible_window = false;
  bool is_in_active_window = false;
  // Whether this is the active tab in a browser window.
  bool is_active = false;
  bool is_discarded = false;
  // User has entered text in a form.
  bool has_form_entry = false;
  int discard_count = 0;
  bool has_beforeunload_handler = false;
  base::TimeTicks last_active;
  base::TimeTicks last_hidden;
  content::RenderProcessHost* render_process_host = nullptr;
  base::ProcessHandle renderer_handle = 0;
  int child_process_host_id = 0;
  base::string16 title;
#if defined(OS_CHROMEOS)
  int oom_score = 0;
#endif
  int64_t tab_contents_id = 0;  // Unique ID per WebContents.
  bool is_auto_discardable = true;
};
typedef std::vector<TabStats> TabStatsList;
}  // namespace resource_coordinator
#endif  // BROWSER_RESOURCE_COORDINATOR_TAB_STATS_H_
我认为

,问题是编译器无法生成默认noexcept移动构造函数,因为并非所有类成员都具有noexept移动构造函数。

例如,下一个代码将不会编译相同的错误:

example::A::A

(example::A&&)' 在其重新声明时默认使用与隐式异常规范不同的异常规范

namespace example
{
    class B
    {
    public:
        B(B&&);
        int mData;
    };
    B::B(B&&) = default;
    class A
    {
    public:
        A(A&&) noexcept;
        std::string mData;
        B mField;
    };
    A::A(A&&) noexcept = default;
}