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

noexcept, assertion handler and unit testing

I'm working on a test framework similar to LEST in which I provide a macro that checks that a given expression triggers a runtime assertion coming from BOOST_ASSERT macro. This is done by using BOOST_ASSERT_HANDLER to make the assertion be turned…
Joel Falcou
  • 6,247
  • 1
  • 17
  • 34
2
votes
0 answers

error: 'noexcept' does not name a type

Seems like it never happened to anyone so I guess I'm doing something wrong... The error I get is: error: expected ';' at end of member declaration error: 'noexcept' does not name a type the code is this: void* operator new (size_t size) noexcept …
user3445972
  • 94
  • 2
  • 10
2
votes
2 answers

Which operator is called to assign to const reference during constructor (re. noexcept)

When I call a constructor which assigns a passed const & to a const & member variable, what happens? Since a const ref, my understanding is 'very little' - no copies, moves, constructors called, etc - just the copying of something likely to turn out…
chrisb2244
  • 2,940
  • 22
  • 44
2
votes
3 answers

Invariants and noexcept

In C++, if a method may throw only because the invariants of the class are not maintained, should I mark it noexcept? For example, a list have a pointer to the link, which should be either nullptr or correct pointer, and a method dereferences this…
statnik
  • 61
  • 4
1
vote
2 answers

C++: How to write a concept that demands that constructor is noexcept?

How do I write a concept that demands that the class has a noexcept constructor? For example, the following static_assert is true in Clang 15.0.7, although I feel that it shouldn't. class Ragdoll { int age_ = -1; public: Ragdoll(int age) /*…
levzettelin
  • 2,600
  • 19
  • 32
1
vote
0 answers

Why does MSVC not auto-generate code for an explicitly-defaulted, noexcept default constructor used to copy construct a designated initializer?

See this godbolt: struct Foo { Foo() noexcept = default; int t{}; }; struct Bar { Foo baz{}; }; int main() { return Bar { .baz = Foo{} }.baz.t; } When deleting noexcept, code for the constructor is auto-generated…
1
vote
1 answer

When is `noexcept` required on move assignment?

I recently realized (pretty late in fact) that it's important to have move constructors marked as noexcept, so that std containers are allowed to avoid copying. What puzzles me is why if I do an erase() on a std::vector<> the implementations I've…
Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
1
vote
0 answers

Function moving std::vector exception safety

For the following code snippet (using C++14 standard), can we declare setDataVectoras noexcept? class Data { public: using Type = ...; // A class with a default move assignment operator. Or even just uint32_t void…
DarkLight
  • 141
  • 1
  • 9
1
vote
1 answer

noexcept, third party and STL calls

I'm trying to understand when I should use noexcept and when I should not. In my library, I have many methods. Some are using methods from third party, some are using the STL. Some use throw, some don't. I can put noexcept statements on all my…
Mathieu Westphal
  • 2,544
  • 1
  • 19
  • 33
1
vote
0 answers

Why is xcode generating more instructions when noexcept is used

Here's a trivial program, compiled on Intel Mac using C++11 #include void show() //noexcept { std::cout << "Hello, World!\n"; } int main(int argc, const char * argv[]) { show(); return 0; } I've been examining the difference…
David
  • 5,991
  • 5
  • 33
  • 39
1
vote
1 answer

noexcept preservation using bind

I have a problem with preservation of noexcept specifier in std::bind, using C++17. In GCC and Clang is not preserved, but in MSVC it is. It seems that using bind with a function with noexcept specifier, the resulting object doesn't preserve it,…
1
vote
1 answer

How to check copy assignment operator in c++ in noexcept() function

I try to check template class T in function do_math() on the possibility of throwing an exception in the copy assignment operator. But the following code throws an error: template void do_math() noexcept(noexcept(T(std::declval()))…
1
vote
1 answer

COleDateTime::SetDateTime and noexcept (code analysis)

See this code: COleDateTime CSpecialEventDlg::GetSpecialEventDate() noexcept { COleDateTime datEvent; if (datEvent.SetDateTime(m_datEvent.GetYear(), m_datEvent.GetMonth(), m_datEvent.GetDay(), 0, 0, 0)…
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
1
vote
1 answer

How to handle an exception thrown from a function defined as noexcept?

It is said that defining a function as noexcept will let the compiler do some optimizations to boost the program and if the function needs to throw the compiler will suppress that optimization. Here's my simple snippet: void func() noexcept(true){ …
Itachi Uchiwa
  • 3,044
  • 12
  • 26
1
vote
0 answers

Declaring a function noexcept conditionally on the testing flag?

If one is using exceptions to replace assertions for debugging/testing purposes. Does it make sense to declare a function noexcept conditionally on the testing flag or this would mess too much with the function interface? #ifdef MYTESTING #define…
alfC
  • 14,261
  • 4
  • 67
  • 118