6

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?

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The DIS for C++14 is N4141 I think, which you can check out from GitHub yourself.
That said, I compared the two and indeed the change was only applied after C++14.
Thanks for your sincere answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.