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

Behaviour of noexcept in a class method declarator

What's the expected behavior of the following code? With GCC the output is 0, whereas with clang it's 1. Which one is correct? #include static const bool ne = false; struct a { a() noexcept(ne) {} static const bool ne =…
sigsegv
  • 73
  • 3
4
votes
2 answers

Shall we use noexcept everywhere in this case?

I am working on a project which uses error codes, and exception handling is not allowed (we are using the nothrowing version of the new operator). Does it make sense to use the noexcept specifier on every function? Which are the possible pro and…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
4
votes
2 answers

noexcept practice for style and performance?

I began adding noexcept to my code, but I'm wondering if it's even wise to bother adding it to inline functions. I'm assuming the optimizer would omit the runtime check when it's clearly unneeded... but from a human/style perspective, is it worth…
VoidStar
  • 5,241
  • 1
  • 31
  • 45
4
votes
1 answer

Confusion around noexcept

After watching many videos, reading a book, I am unclear about when and when not to use noexcept. All of the books say that you should only use noexcept when a function WILL NEVER EVER throw. I think it should be used otherwise. Many say that…
Russell Greene
  • 2,141
  • 17
  • 29
4
votes
1 answer

Possible g++ bug with noexcept and templates

I get an error about mismatched noexcept specifications when I use templates in conjunction with the noexcept specifier. It compiles with various versions of clang I've used and fails in all versions of gcc. struct Y { void…
template boy
  • 10,230
  • 8
  • 61
  • 97
4
votes
2 answers

noexcept and reliability guarantees

Recently I tried to answer what I thought would be a simple question on the noexcept exception specification. The end result being that I found that my fundamental understanding of noexcept was wrong. While reading the current draft standard to…
Mgetz
  • 5,108
  • 2
  • 33
  • 51
4
votes
0 answers

is there any reason to use non noexcept Move constructor

Is there any practical reason to use NON NOEXCEPT move constructor in c++11 ? Because, std::vector uses move constructor if it's declared as noexcep, but I can't imagine any practical using non-noexcept move constructor.
Khurshid
  • 2,654
  • 2
  • 21
  • 29
4
votes
1 answer

What does "see below" mean when used as a type or exception specification?

Looking through the C++ standard (current draft http://isocpp.org/files/papers/N3690.pdf, sec 20.8.3 is one such place) and through LLVM's libc++ headers, I've found "see below" used as a type and exception specification. It seems to be used when…
Ben Jones
  • 919
  • 1
  • 8
  • 22
4
votes
1 answer

C++ throw() (_NOEXCEPT) after function declaration

I have seen in the Visual C++ include file using throw() after a function: size_type capacity() const _NOEXCEPT { // return current length of allocated storage return (this->_Myend - this->_Myfirst); } With the _NOEXCEPT a…
Joe
  • 1,059
  • 2
  • 11
  • 21
4
votes
1 answer

How to use noexcept operator correctly

I've implemented a smart pointer that stores an object of type T with proxy function that calls the internal object's methods: template inline bool call( Function (T::*function)(Args...) const,…
Pavel Davydov
  • 3,379
  • 3
  • 28
  • 41
4
votes
1 answer

How can I properly detect the available C++11 features among GCC versions?

Possible Duplicate: C++11 Feature Checking I'm particularly interested in the case of noexcept specifications which seem to have littered the C++11 standard library with the introduction of GCC 4.7. In this case, detecting compiler version is…
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
3
votes
2 answers

return by value and noexcept

I'm currently trying to get my head around noexcept (like almost everyone I avoided the old "runtime exception specification"). Whilst I think I get the basic idea of noexcept, I'm not sure what happens in a situation like: class sample { public: …
MFH
  • 1,664
  • 3
  • 18
  • 38
3
votes
1 answer

GCC compiles use of noexcept operator but clang and msvc rejects it

While writing code involving noexcept I made a typo and was surprised to see that the program compiled in gcc but not in clang and msvc. Demo struct C { void func() noexcept { } void f() noexcept(noexcept(C::func)) //gcc compiles…
Jason
  • 36,170
  • 5
  • 26
  • 60
3
votes
1 answer

Program using noexcept accepted by msvc and clang but rejected by gcc

I am learning C++ using the books listed here. In particular, I recently learnt about noexcept using the book C++ Primer. Now, to further clear my concept of the topic and to confirm that I've understood things correctly, I am writing simple…
3
votes
0 answers

Defaulted, explicitly noexcept destructor is not noexcept

I am on GCC version 8.1.0, and am using the type trait std::is_nothrow_default_constructible_v to check for throwing destructors (the implementation happens to be using noexcept() to check for non throwing constructors). However, I have stumbled…
super
  • 278
  • 1
  • 11