C++ 使用->;*调用成员函数未通过异常测试
我尝试使用以下代码测试使用操作符C++ 使用->;*调用成员函数未通过异常测试,c++,operators,noexcept,C++,Operators,Noexcept,我尝试使用以下代码测试使用操作符->*对类指针调用成员函数是否不例外 // test.cc #include <type_traits> template <class T> auto declval() noexcept -> std::add_rvalue_reference_t<T>; template <class T, class P, class ...Args> constexpr const static inline boo
->*
对类指针调用成员函数是否不例外
// test.cc
#include <type_traits>
template <class T> auto declval() noexcept -> std::add_rvalue_reference_t<T>;
template <class T, class P, class ...Args> constexpr const static inline bool __is_nothrow_pointer_to_function_member_accessable_v = noexcept( (declval<T>()->*declval<P>())(declval<Args>()...) );
struct A {
int i;
int F() { return 0; }
int F(int) noexcept { return 0; }
int F(bool) & noexcept { return 0; }
int F(bool) && { return 1; }
};
int main() {
static_assert(__is_nothrow_pointer_to_function_member_accessable_v<A*, int (A::*)(int), int>);
static_assert(__is_nothrow_pointer_to_function_member_accessable_v<A*, int (A::*)(bool) &, bool>);
}
//test.cc
#包括
模板自动declval()noexcept->std::add\u rvalue\u reference\t;
模板constexpr const static inline bool是指向函数成员可访问的指针((declval()->*declval)(declval()…);
结构A{
int i;
int F(){return 0;}
int F(int)noexcept{return 0;}
int F(bool)&noexcept{return 0;}
int F(bool)&{return 1;}
};
int main(){
静态断言(是指向函数的指针,成员,可访问);
静态断言(是指向函数的指针,成员,可访问);
}
我使用命令clang++-5.0-std=c++17 test.cc编译了它,两个静态断言都失败了。显然,如果不指定要测试的内容,就无法测试任何东西。函数指针(或成员函数指针)的一组潜在异常为“无限”(即所有类型),除非您显式将此函数指针设置为“noexcept”。在您的例子中,指针没有标记为noexcept,这意味着它们的潜在异常集是“无限”的,并且断言失败。这并没有解决问题,而是包含两个连续下划线的名称(
\u是指向函数的指针\u成员\u可访问的\u v
)以下划线开头,后跟大写字母的名称保留供实现使用。不要在代码中使用它们。@AnT它可以工作,谢谢。你能回答这个问题吗?