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
2 answers

Can an uninitialised std::optional or boost::optional constructor throw?

Can either of the following template methods be declared noexcept? template std::optional foo(const T& value) // noexcept? { try { // code possibly returning a T or a std::nullopt } catch(...) { return…
Daniel
  • 8,179
  • 6
  • 31
  • 56
0
votes
1 answer

std::terminate and destructors of empty containers

Consider some standard container which uses dynamic memory (i.e. is an AllocatorAwareContainer) and has a size and capacity of zero. For example, take a std::vector and call vec.resize(0); vec.shrink_to_fit();. I would imagine that such container…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
0
votes
1 answer

noexcept operators ->() and operator*() for an iterator?

I am writing an iterator that should be very efficient and I was wondering whether I could declare operators operator->() and operator*() as noexcept since they will just call the same operators on the underlying pointer (but if the pointer is…
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
1 answer

Implementing a fixed run-time size array. Should move ctor and swap throw exceptions?

The problem with std::array is that it has a fixed compile-time size. I want a container that can be created with a dynamic size, but that size stays fixed throughout the life of the container (so std::vector won't work, because push_back will…
a06e
  • 18,594
  • 33
  • 93
  • 169
0
votes
1 answer

Why does std::is_nothrow_move_assignable depend on the presence of a destructor?

I have a class like the following: class C { public: C() : ... {} ~C() {} Member_1 m_1; // ... Member_N m_N; }; The two special member functions shown are the only ones…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
0
votes
0 answers

noexcept : different behavior on clang++ and g++

#include using namespace std; class A { public: void g() noexcept {} void f() noexcept( noexcept( g() )) {}; }; A a; int main() { cout<
lz96
  • 2,816
  • 2
  • 28
  • 46
0
votes
2 answers

Backwards compatible noexcept(false) for destructors

Up to C++03 destructors were generally allowed to throw arbitrary exceptions. In C++11, however, all destructors without an explicit exception specification became noexcept by default. This can be overridden with noexcept(false) but this code won't…
0
votes
1 answer

How to declare noexcept if only a member function of an attribute is noexcept?

#include class A { std::vector vec; void swap( A & other) noexcept(noexcept(vec.swap(other.vec))) { vec.swap(other.vec); } }; int main() { } This code compiles under clang(3.4) but not under gcc (4.7.1).…
qdii
  • 12,505
  • 10
  • 59
  • 116
0
votes
1 answer

noexcept(expression) - where expression is a noexcept function that actually throws

Looking at the C++11 Spec (n3485) section 5.3.7, note 3 says that the result of noexcept(expr) is false if: ... a potentially-evaluated call to a function... that does not have a non-throwing exception-specification ... a potentially-evaluated …
Arbalest
  • 1,095
  • 11
  • 23
0
votes
1 answer

Error: expected ‘;’ before ‘noexcept’

I think I don"t have any forget ";" .. I don't know why I have this error class Erreur : public std::exception { private: int m_numero; int m_niveau; std::string m_phrase; public: Erreur(int numero=0, int niveau=0, std::string const& phrase="")…
Choubidou
  • 351
  • 2
  • 4
  • 17
0
votes
1 answer

constexpr different exception specifier when splitting definition and declaration

I have the following test piece of code tested on gcc 4.7.2: #include #include #ifdef REMOVE_CONSTEXPR_NOEXCEPT # define CONSTEXPR_NOEXCEPT #else # define CONSTEXPR_NOEXCEPT noexcept #endif class ConstExpr { public: //…
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
0
votes
1 answer

noexcept specifying conditions under which function does not throw

I am having some trouble wrapping my head around noexcept. template int pop(int idx) noexcept(noexcept(SIZE > 0)) // this is what I dont understand { if (idx <= 0) throw std::out_of_range("My array doesnt go that high"); return…
bryan sammon
  • 7,161
  • 15
  • 38
  • 48
-1
votes
1 answer

can class having destructor with noexcept(false) be inherited from the base class having destructor with noexcept(true)

The scenario is like , I want to inherit my class from some third party library class whose destructor is noexcept(true) by default.and derived class's destructor is noexcept(false) as it can throw exception. There is following code: class…
-1
votes
3 answers

what is std::exception::what() and why to use it?

I came from c++98, and i am trying to make my way in to c++11 and so on. i came across the public member function , std::exception::what, <=> virtual const char* what() const noexcept; from this example givin in c++ reference : what_example, i can…
Adam
  • 2,820
  • 1
  • 13
  • 33
-1
votes
1 answer

Protected noexcept constructor doesn't seem noexcept from derived class. Why?

Here is the source: #include #include class A { protected: //public: // if public, it's fine. (?) A()noexcept{} A(A&&) noexcept{} }; class B : public A { …
Mate059
  • 103
  • 5
1 2 3
22
23