noexcept

Can we refer to member variables in a noexcept specification?

旧城冷巷雨未停 提交于 2019-12-18 04:07:10
问题 Please consider the following code snippet: template<class Tuple> class vector { public: typename Tuple::size_type size() const noexcept(noexcept(m_elements.size())) { return m_elements.size(); } private: Tuple m_elements; }; class tuple { public: using size_type = std::size_t; size_type size() const { return 0; } size_type size() noexcept { return 0; } }; int main() { vector<tuple> x; static_assert(noexcept(x.size()), "x.size() might throw"); return 0; } Is the use of the member variable m

`static constexpr` function called in a constant expression is…an error?

偶尔善良 提交于 2019-12-17 19:08:39
问题 I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire

Why was the old empty throw specification rewritten with a new syntax `noexcept`?

别来无恙 提交于 2019-12-13 20:12:18
问题 The title says it all: why did C++ retire the perfectly satisfying, useful, empty throw specification throw() to replace it with another syntax, with the introduction of the new keyword noexcept ? The empty throw specification is the "only throw these enumerated exceptions guarantee" (written throw(X,Y,Z) ), but with zero enumerated exceptions: instead of throwing X , Y or Z (and derived types), you can throw the empty set: that is the guarantee for a function to never throw anything at the

`static constexpr` function called in a constant expression is…an error?

狂风中的少年 提交于 2019-12-13 15:42:54
问题 I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire

How to declare noexcept if only a member function of an attribute is noexcept?

可紊 提交于 2019-12-13 04:43:11
问题 #include <vector> class A { std::vector<int> vec; void swap( A & other) noexcept(noexcept(vec.swap(other.vec))) { vec.swap(other.vec); } }; int main() { } This code compiles under clang(3.4) but not under gcc (4.7.1). Anyone can tell me what I am doing wrong? EDIT gcc error message is : error: invalid use of incomplete type ‘class A’ error: forward declaration of ‘class A’ 回答1: As a work around, you may use (which works for gcc 4.7.1, gcc 4.8.1 and clang 3.4): void swap(A& other) noexcept

std::terminate and destructors of empty containers

大兔子大兔子 提交于 2019-12-12 03:26:00
问题 Consider some standard container which uses dynamic memory (i.e. is an AllocatorAwareContainer) and has a size and capacity of zero. For example, take a std::vector and call vec.resize(0); vec.shrink_to_fit(); . I would imagine that such container instances would contain only nullptr pointers for their logical contents and std::size_t members to track information like size . I would also imagine that their destructors would do essentially nothing, as there is no dynamic memory to be freed.

Usage of noexcept in derived classes

一曲冷凌霜 提交于 2019-12-11 03:48:52
问题 I encounter an issue while using the noexcept specifier on derived classes, more precisely when the parent is an abstract class (has protected constructors). Hereafter is an example of the way I declare my classes. With a public constructor in the base class: Everything is ok. Same code with protected and the derived class is no more "nothrow movable". Do I miss something? Is std::is_nothrow_move_constructible the correct traits to use in derived class declarations or should I use something

Can a function marked as noexcept have exceptions inside?

你。 提交于 2019-12-10 18:23:52
问题 Let's say that I have a function marked as noexcept but there's a line of code inside that can throw. That line of code will be in a try block and the exception will be caught. Does that cause anything? void MyFunc() noexcept { try { throw std::exception("..."); } catch (const std::exception & e) { // I'll deal with it here... } } 回答1: Yes, this is perfectly legal, as long as the exception doesn't leak out of the function. An implementation shall not reject an expression merely because when

“Default member initializer needed within definition of enclosing class outside of member functions” - is my code ill-formed?

时光怂恿深爱的人放手 提交于 2019-12-10 03:09:11
问题 struct foo { struct bar { ~bar() {} // no error w/o this line }; bar *data = nullptr; // no error w/o this line foo() noexcept = default; // no error w/o this line }; Yes, I know, there is another question with exactly the same title, but a somewhat different problem (involving a noexcept operator and no nested type). The solution suggested there (replacing the constructor of foo with foo() noexcept {} ) changes the semantics and it not necessary here: here we have a better answer (hence the

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

我怕爱的太早我们不能终老 提交于 2019-12-10 01:23:37
问题 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