C++ 理解模板部分专门化
我试图理解模板部分专门化的概念。然而,我似乎把它与模板专门化混淆了。我正在考虑以下两个例子C++ 理解模板部分专门化,c++,templates,C++,Templates,我试图理解模板部分专门化的概念。然而,我似乎把它与模板专门化混淆了。我正在考虑以下两个例子 template <typename T> struct test { ///Some stuff }; template <> //Specialization struct test<int*> { ///Some stuff }; template<typename t> //Partial specialization struc
template <typename T>
struct test
{
///Some stuff
};
template <> //Specialization
struct test<int*>
{
///Some stuff
};
template<typename t> //Partial specialization
struct test<t*>
{
///Some stuff
};
模板
结构测试
{
///一些东西
};
模板//专门化
结构测试
{
///一些东西
};
模板//部分专门化
结构测试
{
///一些东西
};
我正在尝试以下方法
test<int*> s;
tests;
这就调用了专门的模板。我怎样才能称之为部分专用类。有人能用一个例子解释一下部分模板和专门模板之间的区别吗
更新:
#include <iostream>
template<typename T, typename U> //Primary template
struct test
{
void foo() { std::cout << "\nPrimary"; }
};
template <typename T> //Specialization
struct test<T, int*>
{
void foo() { std::cout << "\nPartial Specialization"; }
};
template<> //Full specialization
struct test<int*, int*>
{
void foo() { std::cout << "\nFull Specialization"; }
};
int main()
{
test<int, double> t1;
t1.foo();
test<double, int*> t2;
t2.foo();
test<int*, int*> t3;
t3.foo();
}
在阅读了答案之后,我意识到部分模板专门化只有在需要专门化参数子集时才有帮助。所以我试过这样的方法
template <>
struct test<int*>
{
};
//Partial Specialized
template<typename t>
struct test<t, std::string>
{
};
test<int*,std::string> s; //Error : Too many arguments for class template
模板
结构测试
{
};
//部分专业化
模板
结构测试
{
};
试验s//错误:类模板的参数太多
为什么会这样?看看关于模板专门化的这一点
template <>
模板
将是处理特定类型的方式,而
template<typename t> //Partial specialization
struct test<t*>
template//部分专门化
结构测试
是处理泛型类型的方式。库斯
test<int*> s;
tests;
调用专用模板,因为这是您在上面指定的。要调用部分专用的模板,请使用模板的任何其他类型调用。比如说
test<char*> s;
tests;
这是一个模板:
tempalte <typename A, typename B>
class Foo {};
tempalte
类Foo{};
您可以将其专门化:
template <>
class Foo<int, int> {};
模板
类Foo{};
您还可以保留其中一个参数(部分专用化):
模板
类Foo{};
在谈到类模板时,要简明扼要:
- 指定了所有模板参数
- 通过仅指定模板参数的子集来自定义模板
#include <iostream>
template<typename T, typename U> //Primary template
struct test
{
void foo() { std::cout << "\nPrimary"; }
};
template <typename T> //Specialization
struct test<T, int*>
{
void foo() { std::cout << "\nPartial Specialization"; }
};
template<> //Full specialization
struct test<int*, int*>
{
void foo() { std::cout << "\nFull Specialization"; }
};
int main()
{
test<int, double> t1;
t1.foo();
test<double, int*> t2;
t2.foo();
test<int*, int*> t3;
t3.foo();
}
#包括
模板//主模板
结构测试
{
void foo(){std::cout部分专业化可以实现两种效果
首先,您可以将一个或多个模板参数“固定”为具体值,同时保持其他参数“未绑定”。以下是一个示例:
template <class A, class B>
struct Foo // The primary template
{
static const int i = 0;
};
template <class A>
struct Foo<A, int> // Partial specialisation which "fixes" B
{
static const int i = 1;
};
template <>
struct Foo<char, int> // Full specialisation
{
static const int i = 2;
};
int main()
{
std::cout << Foo<char, double>::i; // 0
std::cout << Foo<double, int>::i << Foo<void, int>::i; // 11
std::cout << Foo<char, int>::i; //2
}
模板
struct Foo//主模板
{
静态常数int=0;
};
模板
struct Foo//部分专业化“修复”了B
{
静态常数int=1;
};
模板
struct Foo//完全专业化
{
静态常数int=2;
};
int main()
{
std::cout no,部分专门化是指当您提供通用模板的另一个实现时,显式地精确化一个或多个类型。这不是您在这里要做的。您评论为部分专门化的确实是通用模板。我认为您的专门化是错误的。它应该有一个模板@Rajeshwar t他的评论是错误的,代码是正确的(见th live demo)@Rajeshwar no,这就是我回答的重点。部分专门化只指定一些参数,而完整(模板
)指定所有参数。希望有帮助。我刚刚更新了我的问题。你能更新你的答案吗?你可以用模板myclass{};
部分专门化模板myclass{}
那么最后一句是错误的在最后一个示例中,主模板必须有两个正确的参数?@Rajeshwar是的,我使用了我答案第一部分中的Foo
。
template <class A>
struct Bar // Primary template
{
static const int i = 0;
};
template <class A>
struct Bar<A*> // Patrial specialisation for pointers
{
static const int i = 1;
};
template <>
struct Bar<int*> // Full specialisation
{
static const int i = 2;
};
int main()
{
std::cout << Bar<int>::i; // 0
std::cout << Bar<void*>::i << Bar<int**>::i; // 11
std::cout << Bar<int*>::i; // 2
}
template <class A>
struct Foo<std::vector<A>, int> // Partial specialisation of primary Foo from above, for any std::vector and an int
{
static const int i = 3;
};