gpt4 book ai didi

c++ - 无法理解 C++ 引用手册中示例中使用的这种类型 `void(C::* volatile)(int) const `

转载 作者:行者123 更新时间:2023-11-28 04:01:43 25 4
gpt4 key购买 nike

<分区>

在 C++17 标准草案文档中,第 (6.7.3) 小节“CV 限定符”[basic.type.qualifier]:第6段,有这样一句话:

For a class type C, the type corresponding to the type-id void(C::* volatile)(int) const has the top-level cv-qualifier volatile.

我有 C 语言背景,随机阅读 C++ 标准,无法理解这一点,我试图将其解释为

a volatile function pointer to a function that can be called on const objects and that takes a single int argument and return a ??? ( C type / void I am lost here)

有人有解释吗?

我试过这段代码

int main()
{
class C
{
public:
typedef void(C:: * volatile X)(int)const ;
void f(int z) const
{cout << z << endl;}

X a = (X)&f; // had to add -fpermissive flag
};

C t;
t.f(5); // works obviously
(t.a)(5); // gives the following compilation message

main.cpp:22:18: warning: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&main()::C::f’ [-fpermissive]
X a = (X)&f;
^
main.cpp:27:12: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘t.main()::C::a (...)’, e.g. ‘(... ->* t.main()::C::a) (...)’
(t.a)(5);
^

更改为建议的内容是行不通的!

谢谢你的帮助

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com