noexcept

What is noexcept useful for?

你。 提交于 2019-12-03 05:23:48
I saw that C++ 11 added the noexcept keyword. But I don't really understand why is it useful. If the function throws when it's not supposed to throw - why would I want the program to crash? So when should I use it? Also, how will it work along with compiling with /Eha and using _set_se_translator ? This means that any line of code can throw c++ exception - because it might throw a SEH exception (Because of accessing protected memory) and it will be translated to c++ exception. What will happen then? Dietmar Kühl The primary use of noexcept is for generic algorithms, e.g., when resizing a std:

Determine whether a constructor of an abstract base class is noexcept?

偶尔善良 提交于 2019-12-03 04:58:53
In C++11 and later, how to determine whether a constructor of an abstract base class is noexcept ? The following methods don't work: #include <new> #include <type_traits> #include <utility> struct Base { Base() noexcept; virtual int f() = 0; }; // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_constructible<Base>::value, ""); // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_default_constructible<Base>::value, ""); // invalid cast to abstract class type 'Base': static_assert(noexcept(Base())

Does the C++ standard mandate that C-linkage functions are `noexcept`?

泪湿孤枕 提交于 2019-12-03 04:34:57
I can't find anything in the standard that forces functions declared with extern "C" to be noexcept , either implicitly or explicitly. Yet, it should be clear that C calling conventions cannot support exceptions... or is it? Does the standard mention this, somewhere that I've missed? If not, why not? Is it simply left as an implementation detail of sorts? As far as I can tell there is no guarantee that function defined with "C" linkage will not throw exceptions. The standard allows a C++ program both to call an external function with "C" language linkage, and to define functions written in C++

How will C++17 exception specifier type system work?

安稳与你 提交于 2019-12-03 01:32:06
Studying about "noexcept specifier(and operator)", I wrote a simple code. And I am surprised that this piece of code: void asdf() noexcept {} int main() { auto f = asdf; std::cout << std::boolalpha << noexcept(f()) << std::endl; } prints false , even function "asdf" is noexcept-specified. So while searching why this mysterious phenomenon is happening, I found C++17's "exception specifier type system"- P0012R1 . According to this (accepted) proposal, since C++17; as noexcept is part of function type, will the code above print true ? And one more, in this question's one line: std::function<void(

Why is swapping multidimensional arrays not noexcept?

对着背影说爱祢 提交于 2019-12-03 00:00:19
I have the following snippet: #include <algorithm> #include <iostream> int main(int argc, char** argv) { int x[2][3]; int y[2][3]; using std::swap; std::cout << noexcept(swap(x, y)) << "\n"; return 0; } Using GCC 4.9.0, this prints 0 . I don't understand why. According to the standard there's two overloads for std::swap : namespace std { template<class T> void swap(T& a, T& b) noexcept( is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value ); template<class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))); } In my understanding the

How to deal with noexcept in Visual Studio

谁都会走 提交于 2019-12-02 16:59:29
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* UserException::what() const’ So I add noexcept : virtual const char* what() const noexcept override This works

function-try-block and noexcept

只愿长相守 提交于 2019-12-01 17:01:41
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? Columbo Or is it a bug in Microsoft compiler? Not quite. A so-called function-try-block like this cannot prevent that an exception will get outside. Consider that the object is never fully constructed since the constructor can't finish

Is `this` allowed inside a noexcept specification?

橙三吉。 提交于 2019-12-01 15:49:33
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() noexcept(noexcept(::do_something(x))) { ::do_something(x); } }; However, if I access the member

Is there any point in declaring a deleted function as noexcept?

二次信任 提交于 2019-12-01 15:43:09
Consider these two possible definitions for a class: Exhibit A: struct A { A() = delete; }; Exhibit A′: struct A { A() noexcept = delete; } Is there any point in declaring a deleted function as noexcept ? (Posted this initially as a comment, but encouraged to post as an answer.) Simply, no. A function that is deleted cannot be called (or, in the case of a constructor, used to initialise an object) let alone throw an exception. Edit: hvd mentioned in comments below that noexcept(f()) does not call f() . If the constructor of class A is delete d, then noexcept(A()) will fail to compile,

Is there any point in declaring a deleted function as noexcept?

萝らか妹 提交于 2019-12-01 14:33:09
问题 Consider these two possible definitions for a class: Exhibit A: struct A { A() = delete; }; Exhibit A′: struct A { A() noexcept = delete; } Is there any point in declaring a deleted function as noexcept ? 回答1: (Posted this initially as a comment, but encouraged to post as an answer.) Simply, no. A function that is deleted cannot be called (or, in the case of a constructor, used to initialise an object) let alone throw an exception. Edit: hvd mentioned in comments below that noexcept(f()) does