简体   繁体   中英

Using a pointer to data member as a non-type template argument

I'd like to know the rationale for preventing pointers to data members from being used as non-type template arguments, as explained in [temp.arg.nontype]:

[Note: The address of an array element or non-static data member is not an acceptable template-argument. [snip] — end note ]

Furthermore, cppreference.com says that pointers to data-members can be used as non-type template arguments, but they are required to be expressed as &Class::member . This seems to be confirmed by the following code (checked on both Clang and GCC):

#include <type_traits>
struct Foo { int bar; };
using X = std::integral_constant<int Foo::*, &Foo::bar>; // works
using Y = std::integral_constant<int Foo::*, X::value>; // fails

Hence, I would like to know whether I missed something in the standard or cppreference.com is wrong on this point. If cppreference.com is right, is there any rationale for allowing &Foo::bar but not X::value ?

I'm using the C++14 working draft .

I'd like to know the rationale for preventing pointers to data members from being used as non-type template arguments [...]

I do not believe it says that.

[Note: The address of an array element or non-static data member is not an acceptable template-argument. [snip] — end note ]

Now, maybe I'm reading to much into the wording of this note, but I see a distinction between address of (ie &s.s -> int* ) and pointer to member (ie &S::s -> int S::* -> int* ), which is allowed.

If you expand that [snip] you'll see that note answers part of your question already:

X<&s.s> x5;  // error: &S::s must be used
X<&S::s> x6; // OK: address of static member

So cppreference is not wrong.

It seems to me that current gcc accepts it, and clang does too, because http://open-std.org/JTC1/SC22/WG21/docs/papers/2014/n4198.html has been implemented by both. That is, of course, if you use their C++17 modes.

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