首次声明后将函数声明为默认函数 [英] Declaring a function as defaulted after its first declaration

查看:113
本文介绍了首次声明后将函数声明为默认函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

8.4.2标准的显式默认函数[dcl.fct.def.default] 中,

显式默认函数和隐式声明函数统称为
,统称为默认函数,实现应
为它们提供隐式定义(12.1 12.4、12.8),这可能
表示将其定义为已删除。如果一个特殊的成员函数是用户声明的且未明确指定默认值,则该函数是用户提供的
或在其第一个声明中删除的
。用户提供的显式默认
函数(即在其首次声明后显式默认)是在显式默认的位置定义的
;如果这样的
函数隐式定义为Delete,则程序格式错误。
[注意:在第一个声明
之后将函数声明为默认函数可以提供有效的执行和简洁的定义,同时使
a稳定的二进制接口能够适应不断发展的代码库。 / strong>

Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (12.1 12.4, 12.8), which might mean defining them as deleted. A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. A user-provided explicitly-defaulted function (i.e., explicitly defaulted after its first declaration) is defined at the point where it is explicitly defaulted; if such a function is implicitly defined as deleted, the program is ill-formed. [ Note: Declaring a function as defaulted after its first declaration can provide efficient execution and concise definition while enabling a stable binary interface to an evolving code base.—end note ]

最后的注释是什么意思?据我所知,在第一个声明之后将函数声明为默认函数将使函数由用户提供,从而使函数变得不平凡,从而使类型具有不平凡的默认构造函数或使类型变得平凡-可复制,并且当然使类型非平凡和非POD,同时仍具有实现功能来提供函数的实际定义。但是我不明白这如何导致 提供有效的执行和简洁的定义,同时为不断发展的代码库提供稳定的二进制接口。欢迎任何想法,并高度赞赏实际示例。

What does the note at the end mean? From what I can see, declaring a function as defaulted after its first declaration will make the function user-provided, thus make the function non-trivial, and thus either make the type having non-trivial default constructor or make the type non-trivially-copyable, and of course make the type non-trivial and non-POD, while still have the implementation to provide the function's actual definition. But I don't understand how this leads to "provide efficient execution and concise definition while enabling a stable binary interface to an evolving code base". Any thoughts are welcome and real-world examples are highly appreciated. Thanks.

此类示例:

struct A {
    A();
};
A::A() = default;


推荐答案

假设您有

// A.h
struct A {
  A();
};

// A.cc
A::A() { }

您可以将其更改为

// A.cc
A::A() = default;

不强制使用 Ah 进行编码重新编译。

to not force code using A.h to be recompiled.

对于默认构造函数,这没有多大意义。 =默认值 {} 占用更多字符。但是,请考虑其他构造函数类型:如果不再需要显式提及每个字段,则复制或移动构造函数可能会变得更短,并且根据所处理的编译器和类型,默认的复制/移动构造函数甚至可能会执行更好,例如,当使用 = default 语法时,如果编译器只能检测到 memcpy 调用就足够了。

For a default constructor, this doesn't make much sense. = default takes up more characters than { }. But think of other constructor types: a copy or move constructor may become much shorter if it is no longer necessary to explicitly mention each field, and depending on the compiler and type you're dealing with, the defaulted copy/move constructor may even perform better, for example if the compiler can only detect that a memcpy call will suffice when you use the = default syntax.

这篇关于首次声明后将函数声明为默认函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆