这不是我熟悉的语法,但我在另一个问题中看到了它,例如:
template<> struct Allowed<std::string> { };
Run Code Online (Sandbox Code Playgroud)
template<>实际意味着什么,没有模板类型/参数?
Mep*_*ane 21
这是一个模板专业化.典型案例是部分专业化:
#include <iostream>
template<class T1, class T2> struct foo
{
void doStuff() { std::cout << "generic foo "; }
};
template<class T1>
struct foo<T1, int>
{
void doStuff() { std::cout << "specific foo with T2=int"; }
};
Run Code Online (Sandbox Code Playgroud)
如您所见,专门化从模板参数中删除了一个元素,并明确指出了一个类型而不是删除的类型.这意味着如果只有一个模板类型,那么<>它就变为空:
template<class T1> struct bar
{
void doStuff() { std::cout << "generic bar"; }
};
template<>
struct bar<int>
{
void doStuff() { std::cout << "specific bar with T1=int"; }
};
Run Code Online (Sandbox Code Playgroud)
Pup*_*ppy 17
这是一个专业化.template<>意味着专业化本身不是模板化的 - 即,它是一种明确的专业化,而不是部分专业化.
您可能只是说它只是必需的语法.
正常的语法是template< typename T > struct Allowed;
因为我们知道在这种情况下T是std :: string,所以没有任何内容可以放在有角度的括号内,但是单词模板和有角度的括号仍然是必需的,因为单独编写
struct Allowed<std::string>并不意味着你专注于模板而只是那个你用std :: string作为类型实例化一个.("struct"这个词不是必须的,但仍然允许).
| 归档时间: |
|
| 查看次数: |
11697 次 |
| 最近记录: |