noexcept

Enforce “noexcept” on std::function?

痞子三分冷 提交于 2019-12-04 20:37:47
问题 This code compiles and runs, throwing the int : #include <functional> void r( std::function<void() noexcept> f ) { f(); } void foo() { throw 1; } int main() { r(foo); } However I would like the compiler to reject the line r(foo); because r should only be passed a noexcept function. The noexcept specifier appears to be ignored. Is there any way to achieve that? Edit: This question is different to Is knowledge about noexcept-ness supposed to be forwarded when passing around a function pointer?

Does the C++ standard mandate that C-linkage functions are `noexcept`?

ぃ、小莉子 提交于 2019-12-04 09:58:07
问题 I can't find anything in the standard that forces functions declared with extern "C" to be noexcept , either implicitly or explicitly. Yet, it should be clear that C calling conventions cannot support exceptions... or is it? Does the standard mention this, somewhere that I've missed? If not, why not? Is it simply left as an implementation detail of sorts? 回答1: As far as I can tell there is no guarantee that function defined with "C" linkage will not throw exceptions. The standard allows a C++

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

蹲街弑〆低调 提交于 2019-12-03 23:29:05
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' is implicitly deleted because its exception-specification does not match the implicit

std::function with noexcept in C++17

对着背影说爱祢 提交于 2019-12-03 22:14:45
In C++17 noexcept has been added to the type system : void r1( void (*f)() noexcept ) { f(); } void foo() { throw 1; } int main() { r1(foo); } The latest versions of GCC and Clang in C++17 mode reject the call r1(foo) , because void (*)() cannot be implicitly converted to void (*)() noexcept . But with std::function instead: #include <functional> void r2( std::function<void() noexcept> f ) { f(); } void foo() { throw 1; } int main() { r2(foo); } Clang accepts the program, apparently ignoring the noexcept specifier; and g++ gives a strange error regarding std::function<void() noexcept> . What

“noexcept” vs “Throws: nothing” [closed]

风流意气都作罢 提交于 2019-12-03 17:12:01
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . While going through the last edits of the C++0x Working draft I found a lot of removal of the keyword noexcept addition of textual Throws: nothing at the same place and vice versa. Just some examples: replacement of noexcept against Throws: nothing : 20.6.4 Pointer safety

Determine whether a constructor of an abstract base class is noexcept?

此生再无相见时 提交于 2019-12-03 16:27:06
问题 In C++11 and later, how to determine whether a constructor of an abstract base class is noexcept ? The following methods don't work: #include <new> #include <type_traits> #include <utility> struct Base { Base() noexcept; virtual int f() = 0; }; // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_constructible<Base>::value, ""); // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_default

What is noexcept useful for?

牧云@^-^@ 提交于 2019-12-03 14:38:11
问题 I saw that C++ 11 added the noexcept keyword. But I don't really understand why is it useful. If the function throws when it's not supposed to throw - why would I want the program to crash? So when should I use it? Also, how will it work along with compiling with /Eha and using _set_se_translator ? This means that any line of code can throw c++ exception - because it might throw a SEH exception (Because of accessing protected memory) and it will be translated to c++ exception. What will

How will C++17 exception specifier type system work?

一笑奈何 提交于 2019-12-03 11:07:12
问题 Studying about "noexcept specifier(and operator)", I wrote a simple code. And I am surprised that this piece of code: void asdf() noexcept {} int main() { auto f = asdf; std::cout << std::boolalpha << noexcept(f()) << std::endl; } prints false , even function "asdf" is noexcept-specified. So while searching why this mysterious phenomenon is happening, I found C++17's "exception specifier type system"- P0012R1. According to this (accepted) proposal, since C++17; as noexcept is part of function

Why is swapping multidimensional arrays not noexcept?

久未见 提交于 2019-12-03 08:31:57
问题 I have the following snippet: #include <algorithm> #include <iostream> int main(int argc, char** argv) { int x[2][3]; int y[2][3]; using std::swap; std::cout << noexcept(swap(x, y)) << "\n"; return 0; } Using GCC 4.9.0, this prints 0 . I don't understand why. According to the standard there's two overloads for std::swap : namespace std { template<class T> void swap(T& a, T& b) noexcept( is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value ); template<class T, size_t

“noexcept” vs “Throws: nothing” [closed]

老子叫甜甜 提交于 2019-12-03 05:32:25
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . While going through the last edits of the C++0x Working draft I found a lot of removal of the keyword noexcept addition of textual Throws: nothing at the same place and vice versa. Just some examples: replacement of noexcept against Throws: nothing : 20.6.4 Pointer safety [util.dynamic.safety] template<class T> T*undeclare_reachable(T*p); addition of noexcept : 20.6.3.2. Pointer