是sizeof ...允许在模板参数专业化? [英] Is sizeof... allowed in template arguments for specialization?

查看:159
本文介绍了是sizeof ...允许在模板参数专业化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用GCC 4.7快照做这些事情:

I'm trying to do something along the lines of this using GCC 4.7 snapshot:

template <int n, int... xs>
struct foo { 
  static const int value = 0;
};

// partial specialization where n is number of ints in xs:

template <int... xs>
struct foo<sizeof...(xs), xs...> { // error: template argument ‘sizeof (xs ...)’
                                   //  involves template parameter(s)
  static const int value = 1;
};

template <int... xs>
struct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int 
                                // even though packs aren't expanded
  static const int value = 2;
};

错误是奇怪的,因为sizeof而不是sizeof ...在这种情况下工作。它们似乎都很容易在编译期计算。

The error is strange because sizeof instead of sizeof... works in this case. Both seem like they could be easily computed during compile time.

编译器是否正确,我不能使用 sizeof ...

Is the compiler correct that I can't use sizeof... in template arguments for specialization?

推荐答案

我将假设这是在阅读此帖后


部分专用的非类型参数表达式不涉及部分专业化的模板参数,除非参数表达式是简单的

A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier.

正在争议的是此处

GCC不正确地解压缩参数包, c $ c> sizeof 过早。

GCC is either incorrectly unpacking the parameter pack, or evaluating sizeof prematurely.

对我提交的错误报告的回复可能会有所帮助。

这篇关于是sizeof ...允许在模板参数专业化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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