template <int* ip> struct test {};
struct q {
static int a;
int b;
constexpr q(int b_) : b(b_) {}
};
int i;
constexpr q q0(2);
int main()
{
constexpr test<&i> t1; // Works fine
constexpr test<&q::a> t2; // Works
constexpr test<&q0.b> t3; // Does not work; address of non-static member?
return 0;
}
The declaration of t3 in the above piece of code fails despite the template argument &q0.b
being known during compile time. Some googling revealed that this is disallowed by the standard (section 14.3.2):
[Note: Addresses of array elements and names or addresses of non-static class members are not acceptable template-arguments.
X<&s.m> x4; // error: address of non-static membe
So why exactly is this explicitly disallowed by the standard despite the addresses of non-static members of global variables being unique as well as known during compile-time?
(new q)->b
is not known at compile time – user2249683int*
, it'sint q::*
, isn't it? – skypjack