是否有一个类型trait来计算构造函数的总数
Is there a type trait to count the total number of constructors?
是否有可能在编译期间推断出类型具有的构造函数的数量?
#include <iostream>
#include <type_traits>
struct A{
int m_i;
float m_f
//constructor 1
A(int i): m_i(i) {}
//constructor 2
A(float f): m_f(f) {}
};
int main() {
//prints 2
std::cout << number_of_constructors<A>::value << 'n';
}
我希望避免任何与构造函数相关的宏,但也许这是唯一的方法。
是否有可能在编译期间推断出类型具有的构造函数的数量?
在c++中11/14吗?没有,据我所知。
为什么?因为c++还没有对反射的支持,但是现在有了研究组SG7:反射,他们将致力于在c++中添加反射的提案。
相关文章:
- "error: no matching function for call to"构造函数错误
- 递归函数计算序列中的平方和(并输出过程)
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 编译器在构造函数中计算的成员偏移量不正确
- 具有空构造函数使数组未初始化会使计算速度变慢
- 构造函数中初始化列表的计算顺序是否固定?
- 如何重新计算启动3D矢量的构造函数
- 在计算后,在基类之后调用参数化构造函数
- 使用构造函数来比较/计算用户输入
- 构造函数中成员启动列表的计算顺序
- 是否可以在初始化列表以外的构造函数体中进行一些计算
- C++:使用计算参数调用基类构造函数
- c++使用计算参数调用超类构造函数
- c++构造函数和计算字段
- 是否有一个类型trait来计算构造函数的总数