草庐IT

Noexcept

全部标签

c++ - 复制构造函数中初始化列表中的 make_unique 是不使用 noexcept 说明符的良好目的吗?

我的复制构造函数旁边有一个noexcept说明符。#include#includeclassFoofinal{public:Foo()noexcept=default;Foo(constFoo&oth):impl_(std::make_unique()){}//impl_;};classFoo::Impl{...private:std::vectorsome_data;}当std::make_unique可以抛出bad_alloc时,我不确定是否应该将noexcept放在复制构造函数旁边。我们将不胜感激! 最佳答案 cpp编码指南在

c++ - 奇怪的运算符重载, "operator T& () const noexcept { return *_ptr; }"

我研究了一下,operator函数的格式是(returnvalue)operator[space]op(arguments){implementation}但是,在std::reference_wrapper实现中,有一个运算符重载函数声明为operatorT&()constnoexcept{return*_ptr;。这个运算符和T&operator()constnoexcept{return*_ptr;不同吗?}?.如果两者不同,那么第一个有什么用? 最佳答案 运算符T&()constnoexcept;是一个user-define

C++ throw() 优化

根据OptimizingC++,Usetheemptyexceptionspecification(thatis,appendthrow()tothedeclaration)forthefunctionsyouaresurewillneverthrowexceptions.如果我知道我的90%的方法不会抛出异常怎么办?将throw()附加到所有这些方法似乎非常规且冗长。如果没有,有什么好处?还是我误解了什么? 最佳答案 C++11引入了noexcept,throw有点被弃用(并且根据this效率较低)noexceptisanimpr

c++ - noexcept 是否适用于从初始化列表传播的异常

假设我有一个这样的构造函数Something(SomethingElse)noexcept:member{SomethingElse,something_that_might_throw()}{...}如果member的构造可以抛出,在这种情况下noexcept是否可以?在上面的例子中,成员变量member是我不知道的类型。旁注:在使用noexcept时是否还有其他需要担心的边缘情况? 最佳答案 #UPDATE:(基于您的编辑):原始答案适用于函数block作用域内的所有内容(包含构造函数,包括构造函数-初始化列表)。你为什么不tr

c++ - 在什么情况下 std::unique_ptr::operator[] 可能抛出?

我的类有一个operator[],它所做的就是在unique_ptr上调用std::unique_ptr::operator[]>成员(member)。相关部分就是这样:templatestructFoo{T&operator[](constsize_tpos)constnoexcept{returndata_[pos];}std::unique_ptrdata_;};我已将运算符标记为noexcept。但是,unique_ptr::operator[]不是noexcept。我无法找出原因,也不知道我是否可以假设它永远不会抛出。unique_ptr::operator[]本身没有在文档

c++ - noexcept 一个函数返回一个具有抛出析构函数的类

在下面的代码中,我认为断言不应该触发,但它确实触发了。structA{~A()noexcept(false);};Af()noexcept;intmain(){static_assert(noexcept(f()),"fmustbenoexcept");}函数f()显然是noexcept,但是noexcept(f())被评估为false。(在gcc和clang中)我是不是遗漏了什么或者是错误? 最佳答案 表达式e上的noexcept运算符告诉您表达式的潜在异常集是否为空。根据[except.spec]/(13.2),此集合包含析构函

c++ - 在 C++17 中使用容器时,noexcept move 操作是否有好处?

当阅读C++11时,我有一种感觉,当使用标准容器(如std::vector)和用户定义的数据类型时,鼓励提供noexceptmove操作,如果有的话,因为那时且只有那时容器会在内部真正move数据而不是复制。今天尝试时,我发现-std=c++1y(对于C++14)和g++-4.8没有区别。也许我错过了规范中的更新,也许我的示例是错误的。我比较了三种应该可以move的数据结构的区别默认情况下可按照“零规则”move可通过提供move操作来movewithoutnoexcept可通过noexcept提供move操作来move框架:#include#include#include#inclu

c++ - std::function 的移动赋值不是 noexcept,但 std::function::swap 是。是什么赋予了?

std::function在func.wrap.func中的概要告诉我们function&operator=(function&&);移动赋值运算符不是noexcept,禁止将其用于标准容器中的仅移动类型。但是!它还告诉我们voidswap(function&)noexcept;同样,默认的构造函数是function()noexcept;因此我们可以使用默认构造函数后跟交换来实现移动构造函数。因为我们可以用swap实现移动赋值运算符(swap有更强的后置条件):如何在std::function中实现noexcept交换?为什么std::function的移动赋值运算符不是noexcep

c++ - 递归 noexcept 规范

用g++4.9和clang3.4测试,为什么这段代码不能编译:namespace{templateconstexprautof(T&&t)noexcept{returntrue;}templateconstexprautof(T&&t,Ts&&...ts)noexcept(noexcept(f(ts...))){returnf(ts...);}}//namespaceintmain(){f(true,0,5u);}但这段代码确实:namespace{templateconstexprautof(T&&t)noexcept{returntrue;}templateconstexpraut

c++ - 为什么在我的析构函数中抛出时总是得到 "terminate called after throwing an instance of..."?

我正在尝试编写一个单元测试来检测对我的类的lock()功能的无效使用。为此,我想使用析构函数并从那里抛出异常。不幸的是,g++没有捕获异常,而是决定调用std::terminate()。类有一个非常简化的版本:classA{public:A():f_lock(0){}~A(){if(f_lock)throwmy_exception("stilllocked");}lock(){++f_lock;}unlock(){--f_lock;}private:intf_lock;};有一个有效的测试:A*a=newA;a->lock();...a->unlock();deletea;我正在尝试编