小编Sau*_*729的帖子

c ++模板中的实例化和专业化之间的区别

C++模板上下文中的特化和实例化有什么区别.从我到目前为止所读到的内容,以下是我对专业化和实例化的理解.

template <typename T>
struct Struct
{

     T x;
};

template<>
struct Struct <int> //specialization
{

    //code
};

int main()
{
   Struct <int> s; //specialized version comes into play
   Struct <float> r; // Struct <float> is instantiated by the compiler as shown below

}
Run Code Online (Sandbox Code Playgroud)

Struct <float>由编译器实例化

template <typename T=float>
struct Struct
{
    float x;
}
Run Code Online (Sandbox Code Playgroud)

我对模板实例化和专业化的理解是否正确?

c++ templates

39
推荐指数
5
解决办法
1万
查看次数

标签 统计

c++ ×1

templates ×1