c++ - 是否有类型特征来计算构造函数的总数?

标签 c++ templates c++14 template-meta-programming typetraits

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

#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/

相关文章:

C++ 将成员函数传递给另一个类

c++ - 如果在等待过程中condition_variable::wait_for延迟参数发生变化怎么办?

Visual Studio 2013 和 2015 中的 C++ 编译器错误 C2280 "attempting to reference a deleted function"

c++ - 如何在cocos2dx中使用CCTargetedTouchDelegate?

c++ - 链接器错误 : undefined reference to vtable

c++ - 在奇怪的重复模板类中使用方法的返回类型作为另一个方法的参数类型

c# - 样式,模板和ResourceDictionaries

c++ - std::get like(部分)模板特化

c++ - 重载本地 lambda 函数

c++11 - C++ : Deriving a function return type using decltype