使用类型和模板模板参数对模板类中的类型参数进行部分特化(Partial specialization of type parameter in template class with type & template template parameters)

我想专门化以下模板类的类型参数,它具有类型参数和模板模板参数:

template < typename T, template <typename E> class Foo > class Bar;

我尝试了在以下每个片段的最后一行中添加和/或省略.template和typename每个排列,并且没有编译:

1.)

template < template <typename E> class Foo > class Bar<int, Foo<typename E>>;

2.)

template < template <typename E> class Foo > class Bar<int, Foo.template <typename E>>;

3.)

template < template <typename E> class Foo > class Bar<int, Foo<E>>;

4)

template < template <typename E> class Foo class Bar<int, Foo.template <E>>;

为什么它们都不起作用?

关于每个适用代码段的最后一行:

不是typename E是Foo类使用的类型,或者这个语法只能在Bar的类定义的{}体中使用? template是否澄清Foo是一个模板,因此阻止编译器解析Foo <因为Foo “小于”,或者这个语法只能在Bar的类定义的{}体内使用?

我怎样才能让它发挥作用?

I want to specialize the type parameter of the following template class, which has a type parameter and a template template parameter:

template < typename T, template <typename E> class Foo > class Bar;

I tried every permutation of adding and/or omitting .template and typename in the last line of each of the following snippets, and none compiles:

1.)

template < template <typename E> class Foo > class Bar<int, Foo<typename E>>;

2.)

template < template <typename E> class Foo > class Bar<int, Foo.template <typename E>>;

3.)

template < template <typename E> class Foo > class Bar<int, Foo<E>>;

4.)

template < template <typename E> class Foo class Bar<int, Foo.template <E>>;

Why doesn't any of them work?

Regarding the last line of each applicable snippet:

Doesn't typename clarify E is a type used by class Foo, or can this syntax only be used within the {} body of Bar's class definition? Doesn't template clarify Foo is a template and therefore prevent the compiler from parsing Foo < as Foo "is less than", or can this syntax only be used within the {} body of Bar's class definition?

How can I get this to work?

最满意答案

不是typename E是Foo类使用的类型,或者这个语法只能在Bar的类定义的{}体中使用?

typename仅在定义模板定义中的类型(也可以使用class )时或在访问依赖类型(依赖于模板参数的类型)时使用。

有关更多信息(甚至关于何时使用template ),请参阅此主题 。

我怎样才能让它发挥作用?

实际上不能使用模板模板参数中的类型名称。 它只是一种形式。 您必须将另一个模板参数添加到主模板中:

template <
    template<typename> class Foo,
    typename E
> class Bar<int, Foo<E>> { ... };
 

此外,如果这是模板Bar ,那么Bar需要一个主模板来专门化:

template<typename T, typename U> struct Bar; template < template<typename> class Foo, typename E > class Bar<int, Foo<E>> { ... };

Doesn't typename clarify E is a type used by class Foo, or can this syntax only be used within the {} body of Bar's class definition?

typename is only used when you are defining a type within a template definition (class could also be used) or when you are accessing a dependent type (a type that depends on a template parameter).

For more information (even about when template is used) see this thread.

How can I get this to work?

The name of a of a type within template template parameter can't actually be used. It's just there as a formality. You have to add another template parameter to your main template instead:

template <
    template<typename> class Foo,
    typename E
> class Bar<int, Foo<E>> { ... };
 

Moreover, if this is a specialization of the template Bar, then Bar needs a primary template to specialize:

template<typename T, typename U> struct Bar; template < template<typename> class Foo, typename E > class Bar<int, Foo<E>> { ... };使用类型和模板模板参数对模板类中的类型参数进行部分特化(Partial specialization of type parameter in template class with type & template template parameters)

我想专门化以下模板类的类型参数,它具有类型参数和模板模板参数:

template < typename T, template <typename E> class Foo > class Bar;

我尝试了在以下每个片段的最后一行中添加和/或省略.template和typename每个排列,并且没有编译:

1.)

template < template <typename E> class Foo > class Bar<int, Foo<typename E>>;

2.)

template < template <typename E> class Foo > class Bar<int, Foo.template <typename E>>;

3.)

template < template <typename E> class Foo > class Bar<int, Foo<E>>;

4)

template < template <typename E> class Foo class Bar<int, Foo.template <E>>;

为什么它们都不起作用?

关于每个适用代码段的最后一行:

不是typename E是Foo类使用的类型,或者这个语法只能在Bar的类定义的{}体中使用? template是否澄清Foo是一个模板,因此阻止编译器解析Foo <因为Foo “小于”,或者这个语法只能在Bar的类定义的{}体内使用?

我怎样才能让它发挥作用?

I want to specialize the type parameter of the following template class, which has a type parameter and a template template parameter:

template < typename T, template <typename E> class Foo > class Bar;

I tried every permutation of adding and/or omitting .template and typename in the last line of each of the following snippets, and none compiles:

1.)

template < template <typename E> class Foo > class Bar<int, Foo<typename E>>;

2.)

template < template <typename E> class Foo > class Bar<int, Foo.template <typename E>>;

3.)

template < template <typename E> class Foo > class Bar<int, Foo<E>>;

4.)

template < template <typename E> class Foo class Bar<int, Foo.template <E>>;

Why doesn't any of them work?

Regarding the last line of each applicable snippet:

Doesn't typename clarify E is a type used by class Foo, or can this syntax only be used within the {} body of Bar's class definition? Doesn't template clarify Foo is a template and therefore prevent the compiler from parsing Foo < as Foo "is less than", or can this syntax only be used within the {} body of Bar's class definition?

How can I get this to work?

最满意答案

不是typename E是Foo类使用的类型,或者这个语法只能在Bar的类定义的{}体中使用?

typename仅在定义模板定义中的类型(也可以使用class )时或在访问依赖类型(依赖于模板参数的类型)时使用。

有关更多信息(甚至关于何时使用template ),请参阅此主题 。

我怎样才能让它发挥作用?

实际上不能使用模板模板参数中的类型名称。 它只是一种形式。 您必须将另一个模板参数添加到主模板中:

template <
    template<typename> class Foo,
    typename E
> class Bar<int, Foo<E>> { ... };
 

此外,如果这是模板Bar ,那么Bar需要一个主模板来专门化:

template<typename T, typename U> struct Bar; template < template<typename> class Foo, typename E > class Bar<int, Foo<E>> { ... };

Doesn't typename clarify E is a type used by class Foo, or can this syntax only be used within the {} body of Bar's class definition?

typename is only used when you are defining a type within a template definition (class could also be used) or when you are accessing a dependent type (a type that depends on a template parameter).

For more information (even about when template is used) see this thread.

How can I get this to work?

The name of a of a type within template template parameter can't actually be used. It's just there as a formality. You have to add another template parameter to your main template instead:

template <
    template<typename> class Foo,
    typename E
> class Bar<int, Foo<E>> { ... };
 

Moreover, if this is a specialization of the template Bar, then Bar needs a primary template to specialize:

template<typename T, typename U> struct Bar; template < template<typename> class Foo, typename E > class Bar<int, Foo<E>> { ... };