简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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