简体   繁体   English

为什么我不能在C ++ 11中使用constexpr指针作为模板参数?

[英]Why can't I use a constexpr pointer as template parameter in C++11?

Please consider the following code: 请考虑以下代码:

template <typename T, typename P, T P:: *s> struct H {};

struct AA { int i; };

int main()
{
  typedef int AA::*PI;
  constexpr PI pi = &AA::i;

  H<int, AA, &AA::i> h1;    // OK
  // H<int, AA, pi> h2;     // compile error
}

I have member pointer pi pointing to AA::i . 我有成员指针pi指向AA::i pi is a constexpr variable. piconstexpr变量。 Why can't I use it as a template parameter, even though using &AA::i directly works? 为什么我不能将它用作模板参数,即使使用&AA::i直接工作?

Because those are the rules, at least in C++11; 因为这些是规则,至少在C ++ 11中; 14.3.2/1 only allows "a pointer to member expressed as described in 5.3.1", which describes the &AA::i syntax. 14.3.2 / 1只允许“指向成员的指针,如5.3.1所述”,它描述了&AA::i语法。

This has changed in the latest draft , and now the requirement for any type is just "a converted constant expression of the type of the template-parameter", under which your code would be fine. 这在最新的草案中有所改变,现在对任何类型的要求只是“模板参数类型的转换常量表达式”,在这种情况下你的代码就可以了。

I don't know whether or not this change is in C++14, since I don't yet have access to that published standard. 我不知道这个改变是否在C ++ 14中,因为我还没有访问该已发布的标准。

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

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