c++ - 识别自动生成的成员函数

标签 c++ gcc clang

特殊成员函数是“如果使用它们,编译器将自动生成的函数,但没有由程序员显式声明”。

http://en.wikipedia.org/wiki/Special_member_functions

详细信息在 C++11 标准的第 12 节中:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions. [Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are odr-used (3.2). See 12.1,12.4 and 12.8. —end note]

有哪些已知的方法可以在编译时识别所有生成的特殊成员函数?

我首选的编译器是 gcc 和 clang。

最佳答案

在 c++11 中, header <type_traits> 定义了一组以下函数:

is_constructible
is_default_contructible
is_copy_contructible
is_move_contructible
is_assignable
is_copy_assignable
is_move_assignable
is_destructible

您可以使用它们在编译时测试是否存在隐式生成的方法, 例如:

std::is_constructible<ClassName>::value

关于c++ - 识别自动生成的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821495/

相关文章:

c++识别函数中的当前线程?

c++ - 在 Mac OS X 上结合 Objective-C 和 C/C++

c++ - 将 reinterpret_cast 转换为可变大小的数组

c++ - 与静态库链接时,为什么要强制执行命令(例如source.cxx -lstatic)?

c - 为 C 代码生成调用图

gcc - 比例索引寻址模式是一个好主意吗?

c++ - 无法将所需数量的元素推送到 vector 中

c++ - std::unordered_map 插入错误

c++ - 一个奇怪的c++代码现象

c++ - 为什么函数的定义与声明分开?