【问题标题】:At compilation what happen with class template [duplicate]在编译时类模板会发生什么[重复]
【发布时间】:2017-08-06 20:41:46
【问题描述】:

当类模板实例化时会发生什么。即我hv一个类模板,我已经为int和float创建了类,那么在编译时会发生什么(编译器将为int和float创建2个单独的类)与否? 例如:

template <typename T>
class A
{
    public:
        void foo(T t)
        {
            //...
        };
};

int main()
{

   A<int> a; 
    A<float> b;

}

【问题讨论】:

  • 这样想,如果类型参数是针对生成类的成员的。布局不同,怎么可能是同一个类?

标签: c++ templates


【解决方案1】:

是的,编译器会即时创建两个新类 - 一个用于 int,一个用于 float。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    相关资源
    最近更新 更多