草庐IT

noexcept-ness

全部标签

c++ - 为什么 std::vector 使用 move 构造函数,尽管声明为 noexcept(false)

无论我在互联网上的什么地方阅读,强烈建议如果我希望我的类(class)与std::vector一起工作(即std使用我类(class)的move语义::vector)我应该将构造函数delcaremove为'noexcept'(或noexcept(true))。为什么std::vector使用它,即使我将它标记为noexcept(false)作为实验?#include#includeusingstd::cout;structT{T(){coutt_vec;t_vec.push_back(T());}输出:T()T(T&&)~T()~T()为什么?我做错了什么?在gcc4.8.2上编译,

c++ - 在 std::function 上强制执行 "noexcept"?

此代码编译并运行,抛出int:#includevoidr(std::functionf){f();}voidfoo(){throw1;}intmain(){r(foo);}但是我希望编译器拒绝r(foo);行,因为r应该只传递一个noexcept函数。noexcept说明符似乎被忽略了。有什么办法可以实现吗?编辑:这个问题不同于Isknowledgeaboutnoexcept-nesssupposedtobeforwardedwhenpassingaroundafunctionpointer?因为我要求补救措施,特别是在std::function的情况下。

c++ - STL 容器元素是否明确要求 (noexcept) 可破坏?

C++11(和C++14)STL容器有noexcept析构函数和clear()成员函数。这意味着元素应该有noexcept析构函数,或者至少存储在容器中的实际元素在被销毁时不应该抛出任何异常——或者更准确地说,相应的allocator_traits::destroy调用不应抛出。这是否在标准的任何地方指定为要求(明确或由另一个明确要求暗示)?如果不是,为什么?我知道is_nothrow_constructible需要noexcept析构函数,但是is_constructible单独没有,并且容器要求是根据概念而不是类型特征模板指定的。 最佳答案

C++ noexcept 声明更改模板推导

我正在修补以确认EffectiveModernC++第91页上的示例,我遇到了一个似乎很奇怪的问题。这段代码templatevoiddoStuff(C&a,C&b)noexcept(noexcept(doStuff(a.front(),b.front()))){std::coutvoiddoStuff(int&x,int&y)noexcept{std::coutv1={1,2,3};vectorv2={4,5,6};intx=5;inty=6;doStuff(x,y);doStuff(v1,v2);}给我一​​个错误,比如error:requestformember‘front’in‘

c++ - 为什么 vector 的 move ctor 不推导出 noexcept()?

为什么要为std::vectormove构造函数使用自定义分配器不会推断出noexcept()来自分配器的行为?这导致封装此类vector的类无法形成可以在某些中正常move的(其他)vector秒。即使基础类型满足必要的要求(MoveInsertable和DefaultInsertable)。 最佳答案 我假设“使用自定义分配器为std::vectormove构造函数”是指分配器扩展的move构造函数,即这个构造函数:vector(vector&&v,constallocator_type&a);主要原因是如果v.get_allo

c++ - 在模板类型推导之前评估 noexcept 说明符

请看下面的代码:#includestructA{A(int,int){}};structtag{};templatestructis_noexcept{staticconstexprboolvalue=noexcept(A{std::declval()...});};structB:A{//#1templateB(tag,Args&&...args)noexcept(/*Here*/is_noexcept::value):A{std::forward(args)...}{}//#2B(intx,inty):A{x,y}{}};intmain(){Bx{0,0};}这段代码似乎被GCC/

c++ - Noexcept 对派生类构造函数的 promise : can that be used without promising noexcept on base constructor?

假设我有一个类classC:publicB{public:C()noexcept;}noexcept说明符是否需要基类的相同promise?也就是说,当我考虑使用noexcept时,我是只看C::C()的行为还是我还需要考虑B::B()是否可能抛出异常?例如,如果B::B抛出异常,它会传播到C::C还是传播到请求新类实例的代码?--如果传播到C::C,如果基类不是noexceptforconstructor,那将是避免noexceptforconstructor的原因之一。 最佳答案 技术上†不要求将基类构造函数声明为noexcep

c++ - noexcept 规范中是否允许使用 `this`?

我有一些代码要求我使用*this,但我希望它是noexcept友好的:structfoo;//Wouldactuallybesomethingwithconditionalnoexceptvoiddo_something(foo&);structfoo{voidfn()noexcept(noexcept(::do_something(*this))){::do_something(*this);}};然而,gccrejectsthis::7:43:error:invaliduseof'this'attoplevelnoexcept(noexcept(::do_something(*th

c++ - 如果类包含标准容器,我可以将类 move 操作标记为 noexcept 吗?

在具有标准容器成员的类上实现move操作的惯用方法不能是noexcept因此不能通过vector.push_back()等操作move.还是我弄错了?从中获取速度vectordata;//...data.push_back(elem);鼓励我们进行move操作noexcept--因此在vector调整大小时,库可以安全地将元素move到重新分配的存储空间。classElem{//...Elem(Elem&&)noexcept;//noexceptimportantformoveElem&operator=(Elem&&)noexcept;//noexceptimportantformo

c++ - 为什么 unique_ptr operator* 不是 noexcept?

这个问题在这里已经有了答案:"No-throwdereferencing"ofstd::unique_ptr(1个回答)关闭4年前。在为我的爱好操作系统实现一个基本的std库时,我遇到了这个并想知道为什么:operator->()和T*get()都被标记为noexcept,但是operator*()不是。根据引用资料,它应该等同于*get(),这将允许它成为noexcept并查看一些实现,我看不出为什么不是。为什么unique_ptr的取消引用运算符没有标记为noexcept?