noexcept

C++ noexcept for a function not throwing exceptions, but can cause a memory failure

独自空忆成欢 提交于 2019-11-29 09:23:25
For example, it's pretty common to have two separate ways to access elements of a private array, overloading the array subscripting operator, or defining at : T& operator[](size_t i) { return v[i]; } T const& operator[](size_t i) const { return v[i]; } T& at(size_t i) { if (i >= length) throw out_of_range("You shall not pass!"); return v[i]; } T const& at(size_t i) const { if (i >= length) throw out_of_range("You shall not pass!"); return v[i]; } The at version can throw an exception, but the array subscripting operator can't. My question is, altough the operator[] doesn't throw an exception,

Static analysis of noexcept “violations” in C++

≡放荡痞女 提交于 2019-11-29 06:30:04
问题 I'm trying to write exception safe code. I find that using C++11's noexcept specifier makes this goal a whole lot more achievable. The general idea, of course, is that a function should be marked as 'noexcept' if, and only if all the functions that it calls are also marked as 'noexcept'. The problem is that in a large code base, where patches from different people are often merged together, it is hard to ensure that this consistency is maintained. So I would like to be able to run a static

Can we refer to member variables in a noexcept specification?

跟風遠走 提交于 2019-11-29 04:06:59
Please consider the following code snippet: template<class Tuple> class vector { public: typename Tuple::size_type size() const noexcept(noexcept(m_elements.size())) { return m_elements.size(); } private: Tuple m_elements; }; class tuple { public: using size_type = std::size_t; size_type size() const { return 0; } size_type size() noexcept { return 0; } }; int main() { vector<tuple> x; static_assert(noexcept(x.size()), "x.size() might throw"); return 0; } Is the use of the member variable m_elements inside the noexcept specifier legal? GCC 5.2 (C++17) yields the compiler error m_elements was

Destructors and noexcept

别等时光非礼了梦想. 提交于 2019-11-28 11:53:12
I am a little bit confused with destructors and noexcept . My understanding was that in C++11 any destructor, including user-defined, is implicitly noexcept(true) , even if we throw from it. And one has to specify explicitly noexcept(false) if they want it to be that way for some reason. I'm seeing quite the opposite - with GCC 4.7.2, the user-defined destructor, no matter how primitive the class and destructor are, is implicitly noexcept(false) . What am I missing here? Is there some hidden gotcha with user-defined destructors? Andy Prowl This is a known bug (credits to the OP for finding the

`static constexpr` function called in a constant expression is…an error?

梦想与她 提交于 2019-11-28 10:43:57
I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire point of constexpr . clang++ is a little more helpful. In addition to an error message stating that the

Why vector access operators are not specified as noexcept?

坚强是说给别人听的谎言 提交于 2019-11-28 05:42:00
Why std::vector 's operator[] , front and back member functions are not specified as noexcept ? The standard's policy on noexcept is to only mark functions that cannot or must not fail, but not those that simply are specified not to throw exceptions. In other words, all functions that have a limited domain (pass the wrong arguments and you get undefined behavior) are not noexcept , even when they are not specified to throw. Functions that get marked are things like swap (must not fail, because exception safety often relies on that) and numeric_limits::min (cannot fail, returns a constant of a

noexcept, stack unwinding and performance

感情迁移 提交于 2019-11-28 05:14:45
The following draft from Scott Meyers new C++11 book says(page 2, lines 7-21) The difference between unwinding the call stack and possibly unwinding it has a surprisingly large impact on code generation. In a noexcept function, optimizers need not keep the runtime stack in an unwindable state if an exception would propagate out of the function, nor must they ensure that objects in a noexcept function are destroyed in the inverse order of construction should an exception leave the function. The result is more opportunities for optimization, not only within the body of a noexcept function, but

C++ noexcept for a function not throwing exceptions, but can cause a memory failure

∥☆過路亽.° 提交于 2019-11-28 02:48:39
问题 For example, it's pretty common to have two separate ways to access elements of a private array, overloading the array subscripting operator, or defining at : T& operator[](size_t i) { return v[i]; } T const& operator[](size_t i) const { return v[i]; } T& at(size_t i) { if (i >= length) throw out_of_range("You shall not pass!"); return v[i]; } T const& at(size_t i) const { if (i >= length) throw out_of_range("You shall not pass!"); return v[i]; } The at version can throw an exception, but the

Is there an automatic noexcept specifier?

匆匆过客 提交于 2019-11-27 22:45:26
I've heard that noexcept keyword is more like 'it should never throw an exception' rather than 'it doesn't'. I don't think it's good to use noexcept keyword if I'm not sure it throws an exception or not, but noexcept keyword is sometimes related to the performance like in a move constructor. So I tried to use noexcept qualifiers, but it gets harder if it has multiple statements in the definition and it becomes a kind of copy-and-paste thing. template <class T> void f(T&& t) noexcept(noexcept(statement_1) && noexcept(statement_2) && noexcept(statement_3) && noexcept(statement_4) && noexcept

Destructors and noexcept

微笑、不失礼 提交于 2019-11-27 19:21:54
问题 I am a little bit confused with destructors and noexcept . My understanding was that in C++11 any destructor, including user-defined, is implicitly noexcept(true) , even if we throw from it. And one has to specify explicitly noexcept(false) if they want it to be that way for some reason. I'm seeing quite the opposite - with GCC 4.7.2, the user-defined destructor, no matter how primitive the class and destructor are, is implicitly noexcept(false) . What am I missing here? Is there some hidden