具有模板模板参数的模板类专用化

Template class specialization with a template template argument

本文关键字:专用 参数      更新时间:2023-10-16
#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;

这里有一些语法问题。

  1. bar 是一个采用一个类型参数的模板。因此,bar的任何部分或显式专业化也必须采用一个类型参数。

    template <template<typename T, typename ...L> class F>
    struct bar<F> {
    

在这里,TL名称是无关紧要的,实际上您正在专注于模板模板。这不符。您必须专注于F的特定实例化:

    template <template<typename , typename ...> class F,
        typename T, typename... L>
    struct bar<F<T, L...>> {
  1. 您缺少分号:

    using toto = T;
                 ^^
    
  2. 您的main声明缺少括号:

    int main() {
        bar<foo<int,char,char>> a;
    }