noexcept

What are the rules for noexcept on default defined move constructors?

我只是一个虾纸丫 提交于 2020-02-21 08:25:52
问题 Especially in connection with std::vector it is important that types are noexcept movable when possible. So when declaring a move constructor = default like in struct Object1 { Object1(Object1 &&other) = default; }; std::is_nothrow_move_constructible<Object1>::value will be true as every member (0 here) of Object1 is nothrow-move-constructible, which is answered here. Yet what happens if the move copy constructor is only declared and then later = default defined like in the following code?

How does noexcept in C++ change the assembly?

自作多情 提交于 2020-02-02 11:18:17
问题 How does noexcept in C++ change the assembly? I tried a bit with small functions, in godbolt, but the assembly did not change. float pi() //noexcept // no difference { return 3.14; } int main(){ float b{0}; b = pi(); return 0; } I am looking for a minimal working example, where I can see a change in the assembly due to noexcept . 回答1: Pretty simple examples can be constructed that involve destructors directly rather than introspection on noexcept status: void a(int); void b() noexcept; void c

How does noexcept in C++ change the assembly?

余生长醉 提交于 2020-02-02 11:18:04
问题 How does noexcept in C++ change the assembly? I tried a bit with small functions, in godbolt, but the assembly did not change. float pi() //noexcept // no difference { return 3.14; } int main(){ float b{0}; b = pi(); return 0; } I am looking for a minimal working example, where I can see a change in the assembly due to noexcept . 回答1: Pretty simple examples can be constructed that involve destructors directly rather than introspection on noexcept status: void a(int); void b() noexcept; void c

When could std::priority_queue::pop throw an exception

我怕爱的太早我们不能终老 提交于 2020-01-14 12:44:49
问题 The pop() method of std::priority_queue is not declared noexcept , so in theory could throw an exception. But when might it throw an exception, and what might those exceptions be? 回答1: It could be marked nothrow , but isn't. Why std::priority_queue::pop could * not throw void pop(); Removes the top element from the priority queue. Effectively calls std::pop_heap(c.begin(), c.end(), comp); c.pop_back(); c is by default an std::vector . [vector.modifiers]/4&5 void pop_back(); 4/ Complexity :

function-try-block and noexcept

假装没事ソ 提交于 2020-01-11 08:25:08
问题 For the following code struct X { int x; X() noexcept try : x(0) { } catch(...) { } }; Visual studio 14 CTP issues the warning warning C4297: 'X::X': function assumed not to throw an exception but does note: __declspec(nothrow), throw(), noexcept(true), or noexcept was specified on the function Is this a misuse of noexcept ? Or is it a bug in Microsoft compiler? 回答1: Or is it a bug in Microsoft compiler? Not quite. A so-called function-try-block like this cannot prevent that an exception will

Is `this` allowed inside a noexcept specification?

独自空忆成欢 提交于 2020-01-11 08:17:22
问题 I have some code which requires me to use *this , but I want it to be noexcept friendly: 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); } }; However, gcc rejects this: <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn()

Is `this` allowed inside a noexcept specification?

不打扰是莪最后的温柔 提交于 2020-01-11 08:17:05
问题 I have some code which requires me to use *this , but I want it to be noexcept friendly: 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); } }; However, gcc rejects this: <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn()

Should std::chrono::steady_clock::now be noexcept?

自闭症网瘾萝莉.ら 提交于 2020-01-05 05:57:33
问题 I've noticed that std::chrono::steady_clock::now has the noexcept specifier in the documentation at cplusplus.com. However, I haven't found any provision for this in the latest C++11 draft (unfortunately I don't have a copy of the standard). Is it a mistake in the cplusplus.com documenation or should std::chrono::steady_clock::now have the noexcept specifier? 回答1: § 20.11.7.2 of the C++11 standard's definition of steady_clock : class steady_clock { public: typedef unspecified rep; typedef

How to static cast throwing function pointer to noexcept in C++17?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 06:59:07
问题 C++17 makes noexcept part of a function's type. It also allows implicit conversions from noexcept function pointers to potentially throwing function pointers. void (*ptr_to_noexcept)() noexcept = nullptr; void (*ptr_to_throwing)() = ptr_to_noexcept; // implicit conversion http://eel.is/c++draft/expr.static.cast#7 says that static_cast can perform the inverse of such a conversion. void (*noexcept_again)() noexcept = static_cast<void(*)() noexcept>(ptr_to_throwing); Unfortunately, both GCC and

How to deal with noexcept in Visual Studio

别等时光非礼了梦想. 提交于 2019-12-31 08:34:28
问题 I'm trying to create a custom exception that derives from std::exception and overrides what() . At first, I wrote it like this: class UserException : public std::exception { private: const std::string message; public: UserException(const std::string &message) : message(message) {} virtual const char* what() const override { return message.c_str(); } }; This works fine in VS2012, but it doesn't compile in GCC 4.8 with -std=c++11 : error: looser throw specifier for ‘virtual const char*