一种可能的方法是利用以下特点:
当推导模板模板参数时,别名模板永远不会被模板参数推导所推导。
http://en.cppreference.com/w/cpp/language/type_alias
因此,使用这样的类型特征:
template<
typename T,
template<typename...> class Template
>
struct instantiated_from
: std::false_type ;
template<
template<typename...> class Template,
typename... Arguments
>
struct instantiated_from<Template<Arguments...>, Template>
: std::true_type ;
我们可以确定,当我们将
Alias<int>
作为第一个模板参数,
Alias
作为第二个时,编译器不会选择该特化。这立即给我们带来了以下结果:
template<
template<typename...> class Template,
typename... Args
>
struct is_alias_template
: std::integral_constant<
bool,
! instantiated_from<Template<Args...>, Template>::value
>
{};
通过这个方法,我们可以- 在提供一些适当的模板参数后 - 检测一个模板是否是别名模板:
int main() {
static_assert( is_alias_template<Alias, int>::value, "");
static_assert(! is_alias_template<Normal, int>::value, "");
static_assert( is_alias_template<Alias, Alias<int>>::value, "");
static_assert( is_alias_template<Alias, Normal<int>>::value, "");
static_assert(! is_alias_template<Normal, Alias<int>>::value, "");
static_assert(! is_alias_template<Normal, Normal<int>>::value, "");
}
这里的主要缺点是必须知道一组适当的模板参数。仅猜测(或使用 int)不适用于具有固定数量(>1)的模板参数的模板,例如。
此外,这无法与具有非类型(或模板)模板参数的(别名或非别名)模板配合使用。
template<class T> using Alias = Normal<T>
,那么这种方法适用于 msvc 和 clang,但不适用于 gcc。你知道还有其他可能性吗? - undefinedinstantiated_from<T>::value==true
。它需要与OP的问题中的template<class T> using Alias = void
不同,或者使用可变模板参数template<class... T> using Alias = Normal<T...>
或者使用虚拟参数template<class T, class Dummy = void> using Aliase = Normal<T>
。请参见godbolt。 - undefined