在最小数量的参数上专用于可变参数模板模板参数:是否合法? [英] Specializing a variadic template template parameter on the minimum number of arguments: legal or not?

查看:105
本文介绍了在最小数量的参数上专用于可变参数模板模板参数:是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

#include <cstdio>

template<template<typename...> class>
struct Foo 
{ 
    enum { n = 77 };
};

template<template<typename, typename...> class C>
struct Foo<C>
{
    enum { n = 99 }; 
};

template<typename...> struct A { };

template<typename, typename...> struct B { };

int main(int, char**)
{
    printf("%d\n", Foo<A>::n);
    printf("%d\n", Foo<B>::n);
}

这个想法是 template< typename,typename。 ..> class template< typename ...>的子集。 class ,因此有可能专门研究它。但这很深奥,所以也许不是。

The idea is that template<typename, typename...> class is a subset of template<typename...> class, so it might be possible to specialize on it. But it's pretty esoteric, so maybe not. Let's try it out.

GCC 4.7说:

$ g++ -std=c++11 test157.cpp 

已编译!

运行它:

$ ./a.out 
77
99

有效!

Clang 3.1说:

Clang 3.1 says:

$ clang++ -std=c++11 test157.cpp
test157.cpp:10:8: error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
struct Foo<C>
       ^  ~~~
test157.cpp:9:10: error: too many template parameters in template template parameter redeclaration
template<template<typename, typename...> class C>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test157.cpp:3:10: note: previous template template parameter is here
template<template<typename...> class>
         ^~~~~~~~~~~~~~~~~~~~~
2 errors generated.

谁是对的?

推荐答案

Clang拒绝部分专业化是错误的。要了解如何解释错误消息,您需要了解clang诊断的内容。这意味着要诊断其参数与主类模板的隐式参数列表完全匹配的局部专业化(< param1,param2,...,paramN> )。

Clang is wrong to reject the partial specialization. To know how to interpret the errormessage, you need to understand what clang diagnoses. It means to diagnose a partial specialization whose arguments match exactly the implicit argument list of the primary class template (<param1, param2, ... , paramN>).

但是参数列表不同,因此clang不能对其进行诊断。特别地,这与部分专业化或多或少地匹配参数无关。考虑

However the argument lists are differently so clang shall not diagnose it. In particular this has nothing to do wheter the partial specialization matches more or less arguments. Consider

template<typename A, typename B> class C;
template<typename B, typename A> class C<A, B> {};

这里的部分专业化匹配所有内容,而不是主要模板匹配的内容。而且这两个模板的参数列表是不同的,因此这种局部专业化是有效的,就像您一样。

The partial specialization here matches everything and not more that the primary template would match. And the argument lists of both templates are different so this partial specialization is valid, just like you'rs.

这篇关于在最小数量的参数上专用于可变参数模板模板参数:是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆