简体   繁体   English

如何专门化给定模板的模板

[英]How to specialize a given template for a template

Is it possible to specialize this template for any basic_string's? 是否可以将这个模板专门用于任何basic_string?

template<class T> struct X {};

Since basic_string is a template itself, I know this would be a solution: 由于basic_string本身就是模板,所以我知道这是一种解决方案:

template <template <class, class, class> class T> struct X {}; template <> struct X<basic_string> {};

However, I would like to know if the language allows to preserve the first template definition, by specializing it somehow for basic_string's only. 但是,我想知道该语言是否允许保留第一个模板定义,只需以某种方式专用于basic_string即可。

Yes: 是:

#include <string>

template <typename> struct X;

template <typename TChar, typename TTraits, typename TAlloc>
struct X<std::basic_string<TChar, TTraits, TAlloc>>
{
    // ...
};

Your primary template takes one type parameter, so every specialization must supply one type parameter for X , one way or another. 您的主要模板采用一个类型参数,因此每个专业化必须以一种或另一种方式为X提供一个类型参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM