c++ - Using the noexcept operator to chain noexcept declarations -
why noexcept
operator take expression rather function signature/declaration?
consider following dummy example:
#include <string> void strprocessor(const std::string& str) noexcept(true) { }; struct type{ void method1() noexcept(strprocessor("")) { //error: call nonconstexpr function strprocessor(""); } };
it won't compile because method1
has non-constexpr expression in noexcept, why need put expression in there in first place?
all want tell compiler method1
noexcept iff invocation of strprocessor
succesfully constructed string noexcept (which is).
so why not noexcept(void strprocessor(const std::string&))
?
another similar dummy example:
struct type{ type(bool shouldthrow=false) noexcept(false) { if(shouldthrow) throw "error"; }; void method1() noexcept(true) {}; void method2() noexcept(noexcept(type().method1())) { method1(); }; }
here i'd method2
noexcept iff invoking method1
on succesfully constructed instance of type noexcept (which in case), type
isn't complete @ point method2
id defined.
please explain if understanding of feature wrong.
void method1() noexcept(noexcept(strprocessor(""))) { // second 'noexcept' ^^^^^^^^^ ^
the first 1 noexcept
specifier, specifies whether or not method1()
noexcept.
the nested 1 noexcept
operator, checks whether strprocessor()
noexcept when called ""
.
your second case bit tricky : type
still incomplete @ point we'd use method1()
inside noexcept
. i've come following workaround, abusing pointer-to-member :
void method2() noexcept(noexcept( (std::declval<type>().*&type::method1)() )) {};
however, don't think there's case deduce method2()
's noexcept specification of method1()
.
Comments
Post a Comment