Questions tagged [noexcept]

A C++ keyword used for exception-specifications and to query whether an expression can throw exceptions

When used at the end of a function declaration noexcept(constant)opt indicates whether or not a function can throw exceptions, replacing the deprecated C++03 form of exception specification using throw(…). If the constant expression in parentheses evaluates to true (or if the parentheses are omitted) it indicates the function will not throw, otherwise it indicates it might throw.

When used as an operator noexcept( expr ) does not evaluate the expression but returns a boolean indicating whether the expression cannot throw exceptions.

Use this tag for questions about either use of the noexcept keyword.

For the history and rationale of noexcept see Using noexcept

331 questions
2
votes
1 answer

Check if a lambda is noexcept

I'm trying to check if a lambda is noexcept or not but it looks like noexcept(lambda) doesn't do what I think it should. auto lambda = [&](Widget& w){ w.build(); }; auto isNoexcept = noexcept(lambda) ? "yes" : "no"; std::cout << "IsNoexcept: " …
randomThought
  • 6,203
  • 15
  • 56
  • 72
2
votes
0 answers

"Terminate called after throwing an instance of 'std::__ios_failure'" when trying to catch exception from `boost::asio::ip::tcp::iostream`

I wrote a simple blocking server which waits for a single client and floods it. I use boost::asio::ip::tcp::iostream class for interacting with the client as I want to use formatted I/O in the future. I also would like to use exceptions for error…
yeputons
  • 8,478
  • 34
  • 67
2
votes
0 answers

Why triggers this noexcept operator an "overloads have similar conversions" error for MSVC but not for Clang and GCC

I have the following code struct S { template operator T() const noexcept (noexcept(static_cast(1) - 1)) // comment out this line to make it work in MSVC { return static_cast(1) - 1; } }; struct R { …
jesses
  • 559
  • 3
  • 15
2
votes
1 answer

unique_ptr constructor requires that Deleter is nothrow

unique_ptr (constructor) @ cppreference unique_ptr( pointer p, /* see below */ d1 ) noexcept; (3) unique_ptr( pointer p, /* see below */ d2 ) noexcept; (4) Here are 2 constructors and the description for the case Deleter is non-reference a) If D…
e271p314
  • 3,841
  • 7
  • 36
  • 61
2
votes
1 answer

Parameter pack inside noexcept specifier

Currently in C++ neither of these are possible, the compiler complains that it expects an expression. This appears trivial to me, if you're building a tuple like object with a variadic amount of types, how to check if all those types are nothrow…
João Pires
  • 927
  • 1
  • 5
  • 16
2
votes
0 answers

Does it makes sense to specify noexcept on deleted operator?

Does it makes sense to specify noexcept on, for example. a deleted move assignment operator? For example: struct A { A& operator=( A&& ) noexcept = delete; }; What would change if I do not specify it, and I just write: struct A { A&…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
2
votes
0 answers

ADL in noexcept, who's wrong, who's right?

Look at this example on compiler explorer: #include #include template struct A { template void test(U&& u) noexcept(noexcept(inner_test(std::forward(u)))) { …
dodomorandi
  • 1,143
  • 1
  • 11
  • 18
2
votes
1 answer

Example for dependent name lookup in case when noexcept-specification is needed but not instantiated

From cppreference: When the noexcept-specification of a function template specialization is needed, but hasn't yet been instantiated, the dependent names are looked up and any templates used in the expression are instantiated as if for the…
ledonter
  • 1,269
  • 9
  • 27
2
votes
1 answer

Why std::unique_ptr does not explicitly require a noexcept Deleter?

The documentation says that Deleter should be: nothrow constructible nothrow callable (because it's called from ~unique_ptr() noexcept nothrow destructible (for the reason above) My question is why uniqut_ptr is defined to allow a Deleter that may…
Kentzo
  • 3,881
  • 29
  • 54
2
votes
2 answers

Is it okay to throw in a noreturn noexcept function instead of calling std::terminate?

I'm making a small error handling system and I want to do a fatal error that terminate the program. I have thought of two way to do that: [[noreturn]] inline void fatal_error1(char const* msg) { std::terminate(); } [[noreturn]] inline void…
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
2
votes
1 answer

Why noexcept move constructor is not getting called during vector reallocation?

As we know when new element is added to std::vector (via push_back) it’s possible that it lacks space for it and for these cases vector allocates larger chunk of memory to hold all its elements and then transfer from existing chunk to new one.In…
PapaDiHatti
  • 1,841
  • 19
  • 26
2
votes
0 answers

default noexcept move semantics

I'm having trouble understanding the reason behind the following error. Example code: #include class Message { private: std::string from; std::string to; std::string message; public: Message() = default; Message(Message&&)…
Zdenik
  • 406
  • 1
  • 4
  • 13
2
votes
2 answers

Usage of noexcept in derived classes

I encounter an issue while using the noexcept specifier on derived classes, more precisely when the parent is an abstract class (has protected constructors). Hereafter is an example of the way I declare my classes. With a public constructor in the…
S.Clem
  • 491
  • 5
  • 10
2
votes
0 answers

C++11 noexcept doesn't affect "copy/move" choice in VC. Is this a VC bug?

[Effective modern C++]says, vector::push_back will allocate new memory block when capacity is not enough,and then "copy or move" elements to the new block Whether to choose copy or move depends on a class A's move ctor having "noexcept" or not.…
Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
2
votes
0 answers

Why Visual C++ makes difference on noexcept of destructor according to /EHsc?

I saw the following sentence in cppreference.com. Any user-defined destructor is noexcept(true) by default, unless the declaration specifies otherwise, or the destructor of any base or member is noexcept(false). To know whether Visual C++ 2015…
ikh
  • 10,119
  • 1
  • 31
  • 70