noexcept

Who manages the exception thrown by a copy constructor in parameters? [duplicate]

狂风中的少年 提交于 2021-02-19 01:35:07
问题 This question already has an answer here : Constructor with by-value parameter & noexcept (1 answer) Closed 1 year ago . Assume I have this function void foo() noexcept { // Safely noexcept code. } And then this class: class Bar { Bar(const Bar&) { ... } // Is not noexcept, so might throw // Non movable: Bar(Bar&&) = delete; }; Now, I need to modify foo() to receive a Bar by value: void foo(Bar bar) // noexcept? { // Safely noexcept code } I assume the copy of Bar is done before the call to

How to detect a noexcept method using SFINAE

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-10 05:29:25
问题 I'm asking about a variation of a (popular) question - detecting the existence of a method of a class. I've read many answers here in SO, and most of the (post C++17) solutions look like this: #include <type_traits> template<class ...Ts> struct voider{ using type = void; }; template<class T, class = void> struct has_foo : std::false_type{}; template<class T> struct has_foo<T, typename voider<decltype(std::declval<T>().foo())>::type> : std::true_type{}; Basically, we're letting the compiler

Should I declare the copy constructor of my exceptions noexcept?

本小妞迷上赌 提交于 2021-02-07 11:52:45
问题 In More Effective C++ , Scott Meyers says C++ specifies that an object thrown as an exception is copied. I suppose then, that if the copy constructor throws an exception in turn, std::terminate is called, so this is a good reason for declaring all my exceptions' copy constructors noexcept (and also, I guess, to not throw objects which allocate memory from the heap, like std::string ). Yet I was surprised to see that the standard library implementation shipped with GCC 4.7.1 doesn’t define

copy elision of return values and noexcept

江枫思渺然 提交于 2020-05-29 03:58:19
问题 I have a function template like this: template <typename T> constexpr auto myfunc() noexcept { return T{}; } Is this function template guaranteed to be noexcept because of copy elision? If an exception is thrown inside the constructor, does this happen inside or outside the function? 回答1: All that copy elision does is eliminate the actual copy or move. Everything happens "as-if" things happen without the copy-elision taking place (except for the copy itself, of course). The construction

Does adding `noexcept(false)` benefit the code in any way?

戏子无情 提交于 2020-05-12 11:14:39
问题 Recently in my code I have been explicitly writing noexcept(false) on functions that I know do throw exceptions, mainly for people reading the code. However, I am wondering if this affects the behavior of my code or the way the compiler interprets it. Does it make any difference? Note: I am aware that destructors are implicitly noexcept and that you have to specify noexcept(false) to change that, I am wondering about other functions. 回答1: Having no exception-specifier and explicitly stating

`noexcept` behavior of `constexpr` functions

你。 提交于 2020-03-17 11:33:30
问题 The wording of [expr.unary.noexcept] changed in C++17 (edited following a comment by @L.F.: C++20 ) . Previously (n4140, 5.3.7 noexcept operator [expr.unary.noexcept]), my emphasis : The result of the noexcept operator is false if in a potentially-evaluated context the expression would contain (3.1) a potentially-evaluated call to a function, member function, function pointer, or member function pointer that does not have a non-throwing exception-specification ([except.spec]), unless the call