C++ noexcept规范中是否允许使用“this”?
我有一些代码需要我使用C++ noexcept规范中是否允许使用“this”?,c++,this,language-lawyer,noexcept,C++,This,Language Lawyer,Noexcept,我有一些代码需要我使用*此,但我希望它不会异常友好: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() noexcept(noexcept(::do_something(*this))) { ::do_something(*this); } };
*此
,但我希望它不会异常友好:
struct foo;
// Would actually be something with conditional noexcept
void do_something(foo&);
struct foo {
void fn()
noexcept(noexcept(::do_something(*this)))
{
::do_something(*this);
}
};
然而:
但是,如果我通过this
指针访问该成员:
诊断:
<source>:7:42: error: invalid use of 'this' at top level
noexcept(noexcept(::do_something(this->x)))
:7:42:错误:在顶层无效使用“this”
noexcept(noexcept(::dou_something(this->x)))
我试过的所有其他编译器,但我不知道是gcc还是所有其他编译器都有缺陷
关键字此
是否可以在noexcept规范中使用?是的,它是允许的。说:
如果声明声明了类X
的成员函数或成员函数模板,则表达式this
是可选cv限定符seq和函数定义末尾[…]之间类型为“指向cv限定符seqX
”的PR值
cv限定符seq是指成员函数的cv限定符,它:
因此,
这个
是在noexcept说明符中使用的有效表达式。这是一个DR(),gcc没有实现。.在bug报告中找到了不错的结果。我在gcc bugzilla中搜索了,但没有找到这个。@Justin我搜索了“这个没有例外”:
void do_something(int);
struct bar {
int x;
void fn()
noexcept(noexcept(::do_something(x)))
{
::do_something(x);
}
};
struct baz {
int x;
void fn()
noexcept(noexcept(::do_something(this->x)))
{
::do_something(this->x);
}
};
<source>:7:42: error: invalid use of 'this' at top level
noexcept(noexcept(::do_something(this->x)))
parameters-and-qualifiers:
( parameter-declaration-clause ) cv-qualifier-seq[opt] ref-qualifier[opt]
noexcept-specifier[opt] attribute-specifier-seq[opt]