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
0
votes
1 answer

Can the ctor be marked noexcept based on allocator type?

How should I make sure that a constructor is noexcept if the allocator does not throw? Here is an MRE: #include #include #include #include #include #include template <…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
0 answers

Detect C++ noexcept context at runtime

In C++, is there a way to detect at runtime whether a function is operating in a noexcept context or not? For example, I have an imaginary function called noexcept_context(): void a() noexcept { // f set to true bool f = noexcept_context()…
maybourne
  • 23
  • 5
0
votes
2 answers

How to check in c++ noexcept(T + T), where T is template

How to c++ check in noexcept that the template parameter T defined through template will throw or not throw an exception during the operation+. In fact, you need to check the addition T + T for throwing a possible exception. I wrote the following…
0
votes
0 answers

static assert on std::is_nothrow_move_constructible_v is not working

class Base { public: Base(Base&&) = default; }; class Derived: public Base { public: Derived(Derived&&) = default; }; int main() { static_assert(std::is_nothrow_move_constructible_v, "Error noexcept"); } In this code…
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
0
votes
4 answers

What is the compiler and runtime behaviour of throwing an exception inside a noexcept function in C++?

What is the compiler and runtime behaviour of throwing an exception inside a function which is declared noexcept? According to a quick experiment with some code, the gcc compiler produces the following warning message when a throw statement is…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
0
votes
0 answers

What are the pros/cons of using noexcept specifiers at Embedded Systems?

Is there any advantage of using the noexcept specifiers of C++ while coding for an embedded system? Does it make the binary smaller? Does it make the code run faster? How does it differ from the GCC's -fno-exceptions flag? Does using that flag…
Caglayan DOKME
  • 948
  • 8
  • 21
0
votes
0 answers

noexcept specifier depending on a template class member function

In the following example, I have an algorithm depending on another algorithm through a template. class absolute_distance { public: absolute_distance() noexcept {} ~absolute_distance() noexcept {} double compute(double x1,…
Caduchon
  • 4,574
  • 4
  • 26
  • 67
0
votes
1 answer

Why all the synthesized copy control operations are noexcept by dedault?

For curiosity sake I'd like to know the default exception specification for default (synthesized) Copy Control Operations. Here is what I've tried: struct Foo{ Foo() = default; Foo(Foo const&) = default; Foo(Foo&&) = default; Foo&…
Maestro
  • 2,512
  • 9
  • 24
0
votes
0 answers

How do I figure out why my defaulted move assignment operator doesn't match the "implicit exception-specification"

Background I'm helping debug a code base. I can't give exact details about it, but such details should be out of scope of an SO question anyway, my actual question is how I actually diagnose why an arbitrary noexcept defaulted function gets deleted…
Krupip
  • 4,404
  • 2
  • 32
  • 54
0
votes
1 answer

noexcept swap and move for classes with mutexes

In general it is a good practice to declare a swap and move noexcept as that allows to provide some exception guarantee. At the same time writing a thread-safe class often implies adding a mutex protecting the internal resources from races. If I…
Triskeldeian
  • 590
  • 3
  • 18
0
votes
0 answers

Should I add noexcept to every function when there are no exceptions?

I never use exceptions in my code and never will. Is it sain to add noexcept to every possible function? What happens if a user that uses my code is using exceptions (feel sorry for him) where there are internal function calls like stl stuff that…
0
votes
3 answers

A question about c++ grammar [static const auto compare = [](const std::string& now, const std::string& next) noexcept ->bool ]

static const auto compare = [](const std::string& now, const std::string& next) noexcept ->bool { return now.size() == next.size() ? now < next : now.size() < next.size(); }; I saw this code on Hacker rank and I do not understand…
Geoboy
  • 37
  • 5
0
votes
2 answers

How can noexcept possibly be specified for a C++ function?

Given some function called X, which calls some 10-20 other functions, member functions, etc. etc., how can I possibly know whether noexcept can be specified as true or not for function X ? Wouldn't I theoretically have to use the noexcept operator…
Edward Diener
  • 97
  • 1
  • 2
0
votes
1 answer

Why was the old empty throw specification rewritten with a new syntax `noexcept`?

The title says it all: why did C++ retire the perfectly satisfying, useful, empty throw specification throw() to replace it with another syntax, with the introduction of the new keyword noexcept? The empty throw specification is the "only throw…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
0
votes
1 answer

null-terminated string_view wanted

Using std::string_view in following scenario: struct A : public std::exception{ A (const char* c) : v_(c){} const char* what() const noexcept override; private: std::string_view v_; }; The above idea works fine and now the copy-ctor…
darune
  • 10,480
  • 2
  • 24
  • 62