- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑一个例子:
class ClassThatMayThrowInCtor
{
ClassThatMayThrowInCtor()
{
if (g_unlucky) throw "Exception";
}
};
class Aggregate
{
ClassThatMayThrowInCtor m_member;
};
据我所知,在某些情况下,C++14 中的编译器可能会生成一个默认构造函数,即 noexcept
。
它会在这种情况下,还是会理解,因为成员的默认 ctor 不提供任何此类保证,所以它也不应该?
要求引用上述声明(在某些情况下...),我未能在标准中找到它,但是 Andrzej's blog确实提到:
This does not mean that
noexcept
is useless. Compiler will annotate implicitly generated member functions of your classes (constructors, copy and move assignments and destructor) withnoexcept
, as appropriate, and STL components will be querying for this annotation. The feature will enable significant optimizations (by using move constructors) even though you will not see a singlenoexcept
keyword. And when compiler annotates functions withnoexcept
it does it correctly (except for destructors), so there is no risk that some noexcept move constructor will throw.
(强调我的)
最佳答案
来自[except.spec]:
14 - [...] If
f
is [...] an implicitly declared default constructor [...]f
allows all exceptions if any function it directly invokes allows all exceptions, andf
has the exception-specificationnoexcept(true)
if every function it directly invokes allows no exceptions. [...]
Aggregate
的隐式声明默认构造函数直接调用 ClassThatMayThrowInCtor::ClassThatMayThrowInCtor()
允许所有异常,所以 Aggregate::Aggregate()
允许所有异常并且不是 noexcept
。
关于c++ - 编译器为类生成 noexcept ctor,其成员在没有此类保证的情况下构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35991071/
考虑: class test { private: int n; int impl () const noexcept { return
#include class A { std::vector vec; void swap( A & other) noexcept(noexcept(vec.swap(other.
在 the C++ standard有如下定义: template void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a,
从 c++20 开始,我们可以使用 consteval 说明符定义立即数函数。当一个函数被声明为 consteval 时,对该函数的每次调用都必须产生一个编译时常量,否则该程序就是病式的。此外,由于
为什么 noexcept 运算符采用表达式而不是函数签名/声明? 考虑以下虚拟示例: #include void strProcessor(const std::string& str) noexc
我有一段通用代码,其性能对我来说很重要,因为我面临着与用 C 编写的著名手工代码的运行时间相匹配的挑战。在开始使用 noexcept 之前,我的代码运行时间为 4.8 秒。通过将 noexcept 放
如果我将函数标记为 noexcept(false),或任何其他计算结果为 false 的表达式,这意味着什么? (1) 我是否向编译器保证该函数可以抛出异常?,(2) 还是我不保证它是否可以抛出异常?
我们遇到过这种情况,想知道解决它的最佳方法 template struct A : T { A(T &&t) noexcept(noexcept(T(std::move(t)))) :T
此代码使用 gcc 4.8.2 (-std=c++11) 编译失败,但使用 clang 3.4 (trunk) (-std=c++11) 编译: #include #include struct
当函数标记为 noexcept 时,GCC 或 Clang 中是否有一个标志会抛出编译时错误(或警告)尝试调用未标记为 noexcept 的函数? 如果不是,那么当您删除 noexcept 时,您应该
假设我有一个类 class C : public B { public: C() noexcept; } noexcept 说明符是否需要基类的相同 promise ?也就是说,当我考虑使
长标题:为什么 std:variant 的 operator=(T&& t) 的 noexcept 规范不依赖于内部类型的析构函数的 noexcept 规范? 我可以在 cppreference 上看
在下面的代码中,我尝试对函数使用条件异常规范,但编译失败,尽管如果在函数外部使用它就可以了。 void may_throw(); // ERROR: expression must have bool
目前在C++这些都不可能,编译器提示它需要一个表达式。 这对我来说似乎微不足道,如果您正在构建一个具有可变数量类型的类似元组的对象,如何检查所有这些类型是否都是 nothrow default/mov
在 The C++ Programming Language 一书中写道,您可以将函数声明为有条件的 noexcept .例如: template void my_fct(T& x) noexcept
根据this question的回答,默认移动构造函数可以定义为 noexcept在一定条件下。例如,下面的类生成一个 noexcept移动构造函数: class C {}; 根据对this ques
拿这个代码: template void my_func() { T::some_method(); } int main() { std::cout ()) ? "noexcept" :
这个声明没问题: void memberFunction(T& functor, double value)noexcept(noexcept(functor(value))); 对于一个 templ
我在思考 noexcept 时遇到了一些麻烦。 template int pop(int idx) noexcept(noexcept(SIZE > 0)) // this is what I do
为什么 std::decay 不从函数指针中移除 noexcept 说明符? 例如这符合 c++17 #include template struct is_noexcept {}; templat
我是一名优秀的程序员,十分优秀!