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

Use of noexcept operator in template

I have tried to understand a bit more how does the noexcept operator works and how it can be used in template. My goal was to enable or disable a template function, depending on the noexcept type of one the member function class. class…
Olivier
  • 37
  • 4
0
votes
2 answers

Is it safe to handle a std::optional in the body of a noexcept function?

I wanna use std::optional as a Maybe type and I'm concerned wether I can use it to indicate a potential failure in some computation, the so observed as an empty option. For example, consider: // This function doesn't have state std::optional
Nazinho
  • 311
  • 2
  • 8
0
votes
1 answer

C++11 - How to fix noexcept operator that fails to detect function declaration with noexcept specifier

I'm setting up a new library for personal research purpose, and i'm trying to fully understand c++ standard library tools and core functionalities. Now i have a problem understanding the noexcept operator. I wrote some test examples involving the…
Valeriop84
  • 125
  • 1
  • 6
0
votes
2 answers

Use noexcept operator depenendet

I wonder if it is possible to use C++11's noexcept operator to define the noextcept specifier of e. g. a destructor that calls a method of another class (e. g. std::allocator::deallocate): template
Sonic78
  • 680
  • 5
  • 19
0
votes
0 answers

Lambda that decays to pointer and noexcept specification

Consider the following code: int main() { void(*ptr)() noexcept = []() noexcept { // ... }; } It compiles with gcc/clang but it fails to compile with msvc. The error is quite obscure: error C2440: 'initializing': cannot convert…
skypjack
  • 49,335
  • 19
  • 95
  • 187
0
votes
0 answers

Calling member function using ->* fails noexcept test

I tried to use the following code to test whether calling a member function on a pointer to class using operator ->* is noexcept // test.cc #include template auto declval() noexcept -> std::add_rvalue_reference_t; template…
JiaHao Xu
  • 2,452
  • 16
  • 31
0
votes
3 answers

noexcept for different operations with template type inside function

I want to write a template function foo, that do some operations on type T, and inside this function values of type T can be: copied assigned summed with operator+ So, I need to specify noexcept for this function with restrictions I've mentioned…
brightside90
  • 243
  • 3
  • 13
0
votes
3 answers

Is there anything wrong with gcc when compiling a nonthrowing pointing to a function which might throw?

I have read the book C++ Primer 5th ed. In section Exception Specifications and Pointers, Virtuals, and Copy Control, it says: That is, if we declare a pointer that has a nonthrowing exception specification, we can use that pointer only to point…
zhenguoli
  • 2,268
  • 1
  • 14
  • 31
0
votes
1 answer

C++ template function are expected to throw exception?

I was searching for reasons that prevent my compiler (intel 15) to vectorize my code. The reasons were : loop with early exits cannot be vectorized unless it meets search loop idiom criteria although I did not have any thing like break or exit(0) in…
Viridya
  • 640
  • 5
  • 13
0
votes
0 answers

Why is destructor of base class called from noexcept constructor

Objects of class B are never deleted, so i want disable destructors, to save space. I wonder, why is A::~A() used in B(), despite noexcept. struct A { A() noexcept {} ~A() = delete; }; struct B : public A { B() noexcept {} ~B() =…
Dobby
  • 46
  • 1
  • 5
0
votes
1 answer

How to model to ignore Coverity Scan C++ 'noexcept' false-positives?

With Coverity starting to recognize C++11 noexcept as throw(), it is producing spurious false positives in code calling third-party libraries like Boost. Moreover, some code deliberately intents to crash on exception because the exception in that…
0
votes
1 answer

Is noexcept specifier always necessary for mov constructor?

I'm reading some libraries for my project. Most of them specify "noexcept" in any move constructor. Is it necessary to write "noexcept" in move constructors or is it occasionally just happened in my reading codes? Thank you.
mallea
  • 534
  • 6
  • 17
0
votes
1 answer

Noexcept for classes that use a stringstream

I have a class that has a std::stringstream member: class Buffer { std::stringstream ss_; }; Its move constructor is Buffer::Buffer(Buffer&& buf) : ss_(std::move(buf.ss_)) { } I suspect that the move operation will not throw and the move…
hochl
  • 12,524
  • 10
  • 53
  • 87
0
votes
2 answers

No-except without passing argument

Can you say how can I assertion, is function noexcept(without passing arguments)? Thanks.
ma13
  • 53
  • 1
  • 9
0
votes
1 answer

C++ exception, undefined behavior and noexcept

About this function int test(int a,int b) { return a/b; } If I call test(2,1), nothing happens. But if I call test(2,0), it causes undefined behavior. As a result, should I define it as noexcept? BTW, why is std::vector::operator[] not…
Caesar
  • 971
  • 6
  • 13