具有模板模板参数的模板类专用化
Template class specialization with a template template argument
#include <tuple>
#include <iomanip>
template <typename T, typename ...L>
struct foo{};
template <typename T>
struct bar{
using toto = T;
};
template <template<typename T, typename ...L> class F>
struct bar<F>{
using toto = T
};
int main(){
bar<foo<int,char,char>> a;
}
当参数是至少有一个模板参数的类时,我想专门化bar
<typename T, typename ...L>
我试过了:
template <template<typename T, typename ...L> class F>
struct bar<F<T,L...>>{
using toto = T
};
和
template <template<typename , typename ...> class F, typename T, typename ...L>
struct bar<F<T,L...>>{
using toto = T
};
这可能是有道理的,但我无法正确理解
从语法上讲,您忘记了样本上的很多东西
template <typename T, typename... L>
struct foo{};
template <typename T>
struct bar {
using toto = T; // Semicolon missing
};
template <template<typename, typename...> class F, typename T, typename... L>
struct bar<F<T,L...>> { // Wrong pack expansion
using toto = T;
};
int main() { // () missing
bar< foo<int,char,char> > a; // Pass the parameters to foo since you're
// partially specializing bar to just do that
}
关于 ideone 的示例
你的 ideone 代码只是有一堆印刷错误:
struct bar<F<T,...L>>{
//should be
struct bar<F<T,L...>>{
//missing brackets
int main{
//missing semicolon
using toto = T
bar<foo, int,char,char> a;
//should be
bar<foo<int,char,char>> a;
这里有一些语法问题。
-
bar
是一个采用一个类型参数的模板。因此,bar
的任何部分或显式专业化也必须采用一个类型参数。template <template<typename T, typename ...L> class F> struct bar<F> {
在这里,T
和L
名称是无关紧要的,实际上您正在专注于模板模板。这不符。您必须专注于F
的特定实例化:
template <template<typename , typename ...> class F,
typename T, typename... L>
struct bar<F<T, L...>> {
您缺少分号:
using toto = T; ^^
您的
main
声明缺少括号:int main() { bar<foo<int,char,char>> a; }
相关文章:
- 具有常量引用参数的函数模板专用化
- 通过依赖类型使用非类型模板参数的单类型模板参数类模板的部分专用化
- 具有多个参数的模板化类专用化,其中一个模板参数是模板本身
- 具有可变参数非类型参数的模板专用化
- 类专用化,没有用作专用化模板参数的类的模板参数
- 基于枚举参数调用专用模板方法
- 检查类是否具有模板专用化(使用布尔值或 int 等模板参数)
- 专用于可变参数模板成员函数
- 如何将模板类专用化为也接受模板模板参数
- 调用模板专用化,具有更多参数的单参数模板调用的特定值
- 有没有办法根据 lambda 参数返回类型部分专用化我的模板化函数?
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- cpp 模板专用化,错误说参数 1 的类型为 T,这取决于参数 T
- 具有不同非类型模板参数的模板类部分专用化
- 带有void类型和参数的C++11模板专用化
- 类模板专用化演绎是否应该考虑演绎指南参数初始化?
- 使用模板模板参数进行模板定义的函数专用化
- 在模板专用化中使用非类型模板模板参数
- C++具有可变参数专用参数的模板,用于参数数量
- 具有专用参数的类方法