noexcept

Use of the noexcept specifier in function declaration and definition?

一个人想着一个人 提交于 2019-12-08 15:50:03
问题 Consider the following function: // Declaration in the .h file class MyClass { template <class T> void function(T&& x) const; }; // Definition in the .cpp file template <class T> void MyClass::function(T&& x) const; I want to make this function noexcept if the type T is nothrow constructible. How to do that ? (I mean what is the syntax ?) 回答1: Like this: #include <type_traits> // Declaration in the .h file class MyClass { public: template <class T> void function(T&& x) noexcept(std::is

noexcept specifier mysteriously breaks compilation (clang, gcc disagree)

与世无争的帅哥 提交于 2019-12-08 15:48:49
问题 The code in question is #include <functional> #include <utility> template <typename F> void for_each(F&&) noexcept {} template <typename F, typename T, typename... Us> void for_each(F&& f, T&& v, Us&&... us) { std::invoke(std::forward<F>(f), std::forward<T>(v)); for_each(std::forward<F>(f), std::forward<Us>(us)...); } void func(void*) noexcept {} int main() { for_each(func, nullptr); } It compiles on gcc 8, but fails on clang 6 with the following error: /opt/wandbox/clang-6.0.0/include/c++/v1

Should I use noexcept for simple functions that obviously cannot throw? [duplicate]

独自空忆成欢 提交于 2019-12-07 14:34:25
问题 This question already has answers here : When should I really use noexcept? (8 answers) Closed 2 years ago . Item 14 of "Effective Modern C++" recommends declaring functions noexcept whenever they don't emit exceptions. I have a class with a number of small member functions that cannot throw for very trivial reasons e.g. they only perform simple mathematical operations on PODs. Should I declare such functions noexcept ? It seems like overkill to me when even the compiler can surely detect

Can a noexcept function still call a function that throws in C++17?

为君一笑 提交于 2019-12-07 13:59:35
问题 In P0012R1, " Make exception specifications be part of the type system ", I see that noexcept is now becoming a part of the function type. I can't tell whether this will prevent noexcept(true) functions from still being able to call noexcept(false) functions. Will the following code still be valid for C++17? void will_throw() noexcept(false){ throw 0; } void will_not_throw() noexcept(true){ will_throw(); } 回答1: According to cppreference: Note that a noexcept specification on a function is not

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

坚强是说给别人听的谎言 提交于 2019-12-07 13:32:22
问题 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 <iostream> #include <type_traits> struct S { // S& operator= (S&&) noexcept { return *this; } }; int main() { std::cout << std::is_nothrow_move_constructible<S>::value << std::is_nothrow_move_assignable<S>::value; } the result 11 is obtained. But if uncomment the move assignment operator, the output becomes 01 . How could an explicit noexcept specification on the

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

跟風遠走 提交于 2019-12-06 01:47:58
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 <iostream> #include <type_traits> struct S { // S& operator= (S&&) noexcept { return *this; } }; int main() { std::cout << std::is_nothrow_move_constructible<S>::value << std::is_nothrow_move_assignable<S>::value; } the result 11 is obtained. But if uncomment the move assignment operator, the output becomes 01 . How could an explicit noexcept specification on the move assignment operator possibly affect that of the move constructor? By defining the move assignment

Should I use noexcept for simple functions that obviously cannot throw? [duplicate]

心不动则不痛 提交于 2019-12-05 23:20:39
This question already has answers here : When should I really use noexcept? (8 answers) Closed 2 years ago . Item 14 of "Effective Modern C++" recommends declaring functions noexcept whenever they don't emit exceptions. I have a class with a number of small member functions that cannot throw for very trivial reasons e.g. they only perform simple mathematical operations on PODs. Should I declare such functions noexcept ? It seems like overkill to me when even the compiler can surely detect that there is no possibility of throwing. EDIT: To clarify my question somewhat, the advice given in this

Program with “noexcept” constructor accepted by gcc, rejected by clang

孤者浪人 提交于 2019-12-05 11:43:49
问题 The code: struct T { T() {} }; struct S { T t; S() noexcept = default; }; int main() { // S s; } g++ 4.9.2 accepts this with no errors or warnings, however clang 3.6 and 3.7 report for line 7: error: exception specification of explicitly defaulted default constructor does not match the calculated one However, if the line S s; is not commented out, g++ 4.9.2 now reports: noex.cc: In function 'int main()': noex.cc:12:7: error: use of deleted function 'S::S()' S s; ^ noex.cc:7:5: note: 'S::S()

noexcept, inheriting constructors and the invalid use of an incomplete type that is actually complete

老子叫甜甜 提交于 2019-12-05 00:29:33
I'm not sure if it's a bug of the GCC compiler or the intended behavior of noexcept . Consider the following example: struct B { B(int) noexcept { } virtual void f() = 0; }; struct D: public B { using B::B; D() noexcept(noexcept(D{42})): B{42} { } void f() override { } }; int main() { B *b = new D{}; } If the noexcept is removed, it compiles. Anyway, as it is in the example, I got this error from GCC v5.3.1: test.cpp:8:31: error: invalid use of incomplete type ‘struct D’ D() noexcept(noexcept(D{42})): B{42} { } ^ As far as I know, struct D is not an incomplete type, but inheriting constructors

Why are the swap member functions in STL containers not declared noexcept?

狂风中的少年 提交于 2019-12-04 23:42:44
As of N3797 the C++ standard requires swap functions of containers to not throw any exceptions unless specified otherwise [container.requirements.general] ( 23.2.1§10 ). Why are the swap member functions that are specified to not throw not declared noexcept ? The same question applies to the specialized non-member swap overloads. Lightness Races with Monica Further to what refp said , here's a post from Daniel Krügler on the std-discussion mailing list: The internal policy to declare a function as unconditional noexcept is explained in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011