[英]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]: 我想知道防止指向数据成员的指针用作非类型模板参数的基本原理,如[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
. 此外,cppreference.com 说,指向数据成员的指针可以用作非类型模板参数,但是必须将它们表示为
&Class::member
。 This seems to be confirmed by the following code (checked on both Clang and GCC): 以下代码(在Clang和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. 因此,我想知道在这一点上我是否错过了standard或cppreference.com中的某些内容是错误的。 If cppreference.com is right, is there any rationale for allowing
&Foo::bar
but not X::value
? 如果cppreference.com是正确的,是否有任何理由允许
&Foo::bar
但不允许X::value
?
I'm using the C++14 working draft . 我正在使用C ++ 14工作草案 。
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. 现在,也许我正在阅读本注释的措辞,但是我看到了地址 (即
&s.s -> int*
)和指向成员的指针(即&S::s -> int S::* -> int*
之间的区别。 &S::s -> int S::* -> int*
),这是允许的。
If you expand that [snip]
you'll see that note answers part of your question already: 如果您扩展该
[snip]
您将看到该注释已经回答了部分问题:
X<&s.s> x5; // error: &S::s must be used
X<&S::s> x6; // OK: address of static member
So cppreference is not wrong. 因此,cppreference并没有错。
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. 在我看来,当前的gcc接受了它,clang也接受了,因为http://open-std.org/JTC1/SC22/WG21/docs/papers/2014/n4198.html都已实现。 That is, of course, if you use their C++17 modes.
当然,如果您使用它们的C ++ 17模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.