C++ 由于GCC问题,手动实现构造函数

C++ 由于GCC问题,手动实现构造函数,c++,C++,(这不是另一个问题的重复,因为在另一个问题中,问题是关于编译问题(有人建议实现构造函数),这里有一个关于如何手动实现该构造函数的特定问题) 为了使用GCC 4.9.2编译以下程序,必须对其进行修改。我必须手动实现构造函数,但我不知道如何实现 6号线出现故障,需要修改: 'TabStats::TabStats(TabStats&&other)noexcept=default;' 作为一个条件,不能删除“除外无”说明符 源代码: #include "browser/resource_coordina

(这不是另一个问题的重复,因为在另一个问题中,问题是关于编译问题(有人建议实现构造函数),这里有一个关于如何手动实现该构造函数的特定问题)

为了使用GCC 4.9.2编译以下程序,必须对其进行修改。我必须手动实现构造函数,但我不知道如何实现

6号线出现故障,需要修改: '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
表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_

只有两种情况需要编写自己的移动构造函数,这两种情况都没有意义:

  • 当其中一个基类没有move语义,但为派生类提供move语义则毫无意义
  • 当您的一个类数据成员没有移动语义时,那么为您的类提供移动语义就没有什么意义了
通常这将涉及用副本替换移动操作

编写自己的移动构造函数的一个良好起点是执行编译器所做的操作:

  • 调用基类的move构造函数(必须使用std::move)
  • 调用所有类成员的移动构造函数。(使用std::move)
例:


这里没有什么令人兴奋的,这就是为什么默认值在大多数情况下都有效。无法移动的类非常罕见。

您是否尝试过在类定义中使用
default
说明符?例如,在头文件中?顺便问一下,如果所有构造函数、析构函数和复制赋值操作符都是默认值,为什么还要使用它们?你就不能删除它们,让编译器隐式地声明和定义它们吗?嗯,我不能重现这个问题,在gcc 4.9.2中编译。滥发相同的问题会让你现在到这里所以为了修复代码,替换第6行的构造函数应该是这样的?(如果我错了,请纠正我)TabStats::TabStats(TabStats&&other)noexcept:TabStats(std::move(other)),TimeTicks(std::move(other.TimeTicks)),ProcessHandle(std::move(other.ProcessHandle){}注释中的代码是无限递归的。在本例中,我调用基类move构造函数。所有成员都必须“手动”移动,包括整数等,每一个成员都必须移动。移动操作可以看作是复制操作,但源代码会被分解。
[20689/29018] CXX obj/chrome/browser/browser/tab_stats.o
FAILED: obj/chrome/browser/browser/tab_stats.o 
g++ -MMD -MF obj/chrome/browser/browser/tab_stats.o.d -DUSE_LIBSECRET -DV8_DEPRECATION_WARNINGS -DUSE_UDEV -DUSE_AURA=1 -DUSE_PANGO=1 -DUSE_CAIRO=1 -DUSE_GLIB=1 -DUSE_NSS_CERTS=1 -DUSE_X11=1 -DNO_TCMALLOC -DDISABLE_NACL -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -DNDEBUG -DUSE_CUPS -DUSE_GNOME_KEYRING -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_32 -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_26 -DUSE_GLX -DSK_SUPPORT_GPU=1 -DV8_USE_EXTERNAL_STARTUP_DATA -DWEBRTC_CHROMIUM_BUILD -DWEBRTC_POSIX -DWEBRTC_LINUX -DHUNSPELL_STATIC --param=ssp-buffer-size=4 -fstack-protector -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fPIC -pipe -pthread -m32 -msse2 -mfpmath=sse -mmmx -Wall -O2 -g0 -fvisibility=hidden -DLIBXML_STATIC= -std=gnu++11 -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include -Wno-narrowing -fno-rtti -fno-exceptions -fvisibility-inlines-hidden -c ../../chrome/browser/resource_coordinator/tab_stats.cc -o obj/chrome/browser/browser/tab_stats.o
../../chrome/browser/resource_coordinator/tab_stats.cc:14:1: error: function 'resource_coordinator::TabStats::TabStats(resource_coordinator::TabStats&&)' defaulted on its redeclaration with an exception-specification that differs from the implicit declaration 'resource_coordinator::TabStats::TabStats(resource_coordinator::TabStats&&)'
 TabStats::TabStats(TabStats&& other) noexcept = default;

 ^
struct A {};  // must have a move constructor, be it a default or not.

struct B : A
{
    B(B&& b) 
        : A(std::move(b))
        , data1_(std::move(b.data1_))
        , data2_(std::move(b.data2_))
    {}

    some_type        data1_;
    some_other_type  data2_;
};