是否有可能推断出一个类型在编译期间拥有的构造函数的数量?
#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';
}
我希望避免任何与构造函数有关的宏,但也许这是唯一的方法。
最佳答案
Is it possible to deduce the number of constructors a type has during compile time?
在 C++11/14 中? 没有,据我所知。
为什么?因为 C++ 不支持 Reflections , 但现在有学习群SG7: Reflection这将适用于将反射添加到 C++ 的提案。
关于c++ - 是否有类型特征来计算构造函数的总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30538046/