问题描述
在C ++模板的上下文中,专门化和实例化之间有什么区别。根据我到目前为止阅读的内容,以下是我对专业化和实例化的理解。
template< typename T&
struct Struct
{
T x;
};
模板<>
struct struct< int> // specialization
{
// code
};
int main()
{
Struct< int> s; //专用版本发挥作用
Struct< float> r; // Struct< float>由编译器实例化如下所示
}
实例化 struct< float>
由编译器
template< typename T = float> ;
struct Struct
{
float x;我对模板实例化和专业化的理解是否正确?
$ b 解决方案 4个概念:
> :这是您所谓的实例化
显式实例化:你告诉编译器用给定的类型来实例化模板,例如
template Struct< char> ;; //用于控制模板的PLACE位置
显式)专业化: 这是您称为专业化的 >这是当你给一个模板的一个替代定义类型的子集,像这样:
template< class T&类Struct< T *> {...} //指针的部分专门化
What is the difference between specialization and instantiation in context of C++ templates. From what I have read so far the following is what I have understood about specialization and instantiation.
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
}
Instantiation of Struct <float>
by the compiler
template <typename T=float>
struct Struct
{
float x;
}
Is my understanding of template instantiation and specialization correct?
解决方案 4 concepts:
(implicit) instantiation: this is what you refer to as instantiation
explicit instantiation:: this is when you tell the compiler to instantiate the template with given types, like this
template Struct<char>; //used to control the PLACE where the template is inst-ed
(explicit )specialization: this is what you refer to as specialization
partial specialization this is when you give an alternative definition to a template for a subset of types, like this:
template<class T> class Struct<T*> {...} //partial specialization for pointers
这篇关于c ++模板中实例化和专业化之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!