Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 is a constexpr variable. Why can't I use it as a template parameter, even though using &AA::i directly works?

like image 838
Zhipeng YANG Avatar asked Apr 10 '15 08:04

Zhipeng YANG


1 Answers

Because those are the rules, at least in 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.

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.

like image 77
Mike Seymour Avatar answered Oct 10 '22 19:10

Mike Seymour