noexcept

How to use noexcept in assignment operator with copy-and-swap idiom?

拟墨画扇 提交于 2019-12-30 00:55:15
问题 The move assignment operator should often be declared noexcept (i.e. to store the type in STL containers). But the copy-and-swap idiom allows both copy- and move- assignment operators to be defined in a single piece of code. What to do with noexcept specifier in this case? The copy construction can throw, but I doubt whether it can violate the noexcept specifier. // Is it correct considering that T copy constructor can throw? T& operator=(T other) noexcept; 回答1: Since the copy is made on the

noexcept depending on method of member

梦想的初衷 提交于 2019-12-23 18:53:37
问题 Related to this question, I want to specify my private section after my public interface. template<class T, void (T::*f)()> class B { public: void g(int y) noexcept(noexcept(x.*f())) {} private: T& x; }; But I get an error from Clang that x is an undeclared identifyer. mm_test.cpp:14:34: error: use of undeclared identifier 'x' void g(int y) noexcept(noexcept(x.*f())) ^ It compiles just fine if the declaration of member x occurs before the declaration of g. Am I not supposed to be able to use

Is knowledge about noexcept-ness supposed to be forwarded when passing around a function pointer?

对着背影说爱祢 提交于 2019-12-23 09:28:10
问题 I have written the following code to test noexcept propagation across function calls, and it seems that it doesn't work as I would have thought. In GCC 4.7.2, A function can effectively be tested against being noexcept only directly or when passed as a template specialization argument; but not when passed as an argument to a templated function, or as a function pointer to a normal function -- even when that function declares its formal parameter as being noexcept . Here's the code: #include

Rationale for std::move_if_noexcept still moving throwing move-only types?

▼魔方 西西 提交于 2019-12-23 08:45:07
问题 move_if_noexcept will: return an rvalue -- facilitating a move -- if the move constructor is noexcept or if there is no copy constructor (move-only type) return an lvalue -- forcing a copy -- otherwise I found this rather surprising, as a move-only type that has a throwing move-ctor will still have this move-ctor invoked by code that uses move_if_noexcept . Has there been given a thorough rationale for this? (Maybe directly or between the lines of N2983?) Wouldn't code be better off not

Why does std::vector use the move constructor although declared as noexcept(false)

早过忘川 提交于 2019-12-23 08:25:09
问题 Wherever I read in the internet, it is strongly adviced that if I want my class to be working well with std::vector (i.e. move semantics from my class were used by std::vector ) I should delcare move constructor as 'noexcept' ( or noexcept(true) ). Why did std::vector use it even though I marked it noexcept(false) as an experiment? #include <iostream> #include <vector> using std::cout; struct T { T() { cout <<"T()\n"; } T(const T&) { cout <<"T(const T&)\n"; } T& operator= (const T&) { cout <<

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

♀尐吖头ヾ 提交于 2019-12-22 02:04:50
问题 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. 回答1: 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

std::function with noexcept in C++17

北战南征 提交于 2019-12-21 06:48:43
问题 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

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

余生颓废 提交于 2019-12-19 08:59:36
问题 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 no type exists, but it seemed strange to use a 2 word phrase for that instead of some sort of valid identifier. Is it discussed somewhere in the standard or elsewhere? Why/how is it used? 回答1: see below is simply a place holder for one of a few

Are C++ standard library implementations allowed to strengthen noexcept specifications?

本秂侑毒 提交于 2019-12-18 12:54:29
问题 According to the C++ standard, are implementations of the C++ standard library allowed to strengthen noexcept specifications of methods and other functions of the C++ standard library as defined by the standard? For example, if the C++ standard specifies some function std::f as void f(); are standard library implementations allowed to implement it as void f() noexcept; instead? 回答1: The Standard says yes: § 17.6.5.12.1 Restrictions on exception handling [res.on.exception.handling] Any of the

Passing null pointer to placement new

孤人 提交于 2019-12-18 10:44:49
问题 The default placement new operator is declared in 18.6 [support.dynamic] ¶1 with a non-throwing exception-specification: void* operator new (std::size_t size, void* ptr) noexcept; This function does nothing except return ptr; so it is reasonable for it to be noexcept , however according to 5.3.4 [expr.new] ¶15 this means that the compiler must check it doesn't return null before invoking the object's constructor: -15- [ Note: unless an allocation function is declared with a non-throwing