是否有一个类型trait来计算构造函数的总数

Is there a type trait to count the total number of constructors?

本文关键字:构造函数 计算 有一个 类型 trait 是否      更新时间:2023-10-16

是否有可能在编译期间推断出类型具有的构造函数的数量?

#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++中添加反射的提案。