Zhipeng YANG
Zhipeng YANG

Reputation: 805

Why can't I use a constexpr data member pointer as a template argument 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?

Upvotes: 6

Views: 274

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254751

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.

Upvotes: 7

Related Questions