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

Unit test to check for `noexcept` property for a C++ method

I have several methods that have to be marked noexcept must not be marked noexcept How to write unit tests that check the method for being marked noexcept properly? Reason: to ensure that in the future those properties are not changed during…
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
3
votes
1 answer

Noexcept variadic is_nothrow_constructible_v

Which of them is the correct one and why? I think it's the first one because Ts already have the && or const & associated with the type but I like to be sure and there really isn't that many examples of this particular case of noexcept. template…
João Pires
  • 927
  • 1
  • 5
  • 16
3
votes
2 answers

Why std::any & operator= for ValueType is not conditionally noexcept?

The question is quite simple. This is the declaration of templated operator= for std::any: template any& operator=( ValueType&& rhs ); I would expect it to be: template any& operator=( ValueType&& rhs )…
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
3
votes
1 answer

When could std::priority_queue::pop throw an exception

The pop() method of std::priority_queue is not declared noexcept, so in theory could throw an exception. But when might it throw an exception, and what might those exceptions be?
Raedwald
  • 46,613
  • 43
  • 151
  • 237
3
votes
1 answer

What's the meaning of recursive noexcept()?

The standard library defines an swaps for arrays and std::pair like so: template void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))); template struct pair { … void swap(pair&…
Birchlabs
  • 7,437
  • 5
  • 35
  • 54
3
votes
1 answer

Is the exception specification part of the immediate context in SFINAE?

This is a follow-up to the following question: SFINAE in variadic constructor I wrote that some code such as template StrongAlias(Args&&... args) noexcept(noexcept(T(std::declval()...))) :…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
3
votes
0 answers

C++11 constexpr+noexcept doesn't tell if a function is compile-time constant function

I see this example from cppreference website, but the running result was not anything expected.(http://en.cppreference.com/w/cpp/language/constexpr) The page says:" Because the noexcept operator always returns true for a constant expression, it can…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
3
votes
1 answer

Double Free error with AddressSanitizer GCC 4.8

Consider the following toy program(prog.cpp): class A { public: vector vec; A() noexcept {} A(vector s) : vec(s) {} }; class B { private: vector> a_table; public: B(int capacity) : a_table(capacity) {} …
stillanoob
  • 1,279
  • 2
  • 16
  • 29
3
votes
1 answer

why object code generated for noexcept and throw() is same in c++11?

Code using noexcept . //hello.cpp class A{ public: A(){} ~A(){} }; void fun() noexcept{ //c++11 style A a[10]; } int main() { fun(); } Code using throw() . //hello1.cpp class A{ public: A(){} ~A(){} }; void fun()…
SACHIN GOYAL
  • 955
  • 6
  • 19
3
votes
1 answer

Using placement new on nullptr in decltype() or operator noexcept() context

Is it allowed by the Standard to write decltype(::new (nullptr) T(std::declval< Args >()...)) or noexcept(::new (nullptr) T(std::declval< Args >()...))? Particularly interested placement new on nullptr correctness. Considering following…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
3
votes
2 answers

Should I declare wrappers for C functions noexcept?

Suppose you have a C++ function that only makes calls to C functions, like int ClearTheBin() { int result = SHEmptyRecycleBinW( nullptr, nullptr, SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | …
James Ko
  • 32,215
  • 30
  • 128
  • 239
3
votes
2 answers

What's the difference between the noexcept keyword and _NOEXCEPT macro?

I'm a beginner to C++ and I'm learning about some of the C++11 features. One thing I noticed was that in some parts of the Visual C++ stdlib, the authors used the _NOEXCEPT macro instead of the noexcept keyword. Hovering over the macro says #define…
James Ko
  • 32,215
  • 30
  • 128
  • 239
3
votes
5 answers

How could the exception specifier on move assignment operator affect that of move constructor?

I've being testing with GCC 5.2 and clang 3.6, both in C++14 mode, and they give the same output. For the following code #include #include struct S { // S& operator= (S&&) noexcept { return *this; } }; int main() { …
Lingxi
  • 14,579
  • 2
  • 37
  • 93
3
votes
0 answers

noexcept depending on method of member

Related to this question, I want to specify my private section after my public interface. template class B { public: void g(int y) noexcept(noexcept(x.*f())) {} private: T& x; }; But I get an error from Clang…
John
  • 7,301
  • 2
  • 16
  • 23
3
votes
1 answer

nothrow construction of virtual classes in c++11

Take the following code snippet: #include struct X { virtual ~X(); }; static_assert(std::is_nothrow_default_constructible::value, "fail"); Under clang svn, it compiles fine. However, with gcc 4.7.2, the assertion fails. Which one…
DirtY iCE
  • 431
  • 2
  • 12