【发布时间】:2015-12-22 23:38:34
【问题描述】:
我想在模板模板参数的类型模板参数上专门化一个类模板。可能吗?如果是,语法是什么?
#include <type_traits>
template <typename T>
struct X {};
// primary template
template<template<class> class C>
struct Z : std::false_type {};
// specialization on the template template parameter
template<>
struct Z<X> : std::true_type {}; // OK
// specialization on the type template parameter of the
// template template parameter
template <template<class> class C>
struct Z<C<int>> {}; // ERROR
动机:假设模板模板参数表示集合(例如std::vector、std::deque)。我想在std::vector 上专门化Z 但我对std::vector 的类型模板参数不感兴趣,没关系。此外,我想专注于所有 Collection 类型,其中包含int。
这个问题和下面的问题类似,但是他们要么是在尝试专门化一个函数模板
或者他们试图不专注于模板模板参数
或者主模板中没有模板模板参数
【问题讨论】:
-
我不明白你在做什么。如果允许您尝试执行的“专业化”类型,那么在什么情况下会实例化该专业化?
Z的参数是模板,而不是类型。
标签: c++ templates template-specialization partial-specialization