- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我最近开始添加新的 noexcept
规范以尽可能移动构造函数/赋值。现在我开始想知道隐式生成的成员函数的异常规范是什么样的。由于具有 noexcept
移动函数允许使用更有效的代码路径(例如,在调整 vector
的大小时)我希望尽可能将它们声明为 noexcept。我无法理解标准对此的规定,因此尝试了 g++4.6 中的以下代码(使用 -std=c++0x
)来掌握它:
struct foobar{};
int main()
{
foobar a, b;
std::cout<<std::boolalpha
<<noexcept(foobar())<<", "<<noexcept(foobar(a))<<", "
<<noexcept(a = b) <<", "<<noexcept(a = std::move(b))<<", "
<<noexcept(foobar(std::move(a)))<<std::endl;
}
这给了我 True, True, True, False, False
的输出,这意味着 noexcept
的默认和复制构造函数/赋值,而没有的移动操作。
现在回答我的问题:
在什么情况下隐式生成(或默认)的成员函数声明为noexcept
?此外,观察到的 foobar
行为是正确的还是只是 gcc4.6 中的编译器错误?
最佳答案
库错误 - 它在 gcc 4.7 中显示 true, true, true, true, true
。
错误是 not 生成的移动构造函数不是 noexcept,但是 std::move
没有标记为 noexcept
,正如我们在附加测试中看到的那样:
std::cout << noexcept(a = static_cast<foobar&&>(b)) << ", " // true
<< noexcept(foobar(static_cast<foobar&&>(b))) << ", " // true
<< noexcept(std::move(b)) << std::endl; // false
gcc 4.6 中的大多数库函数都不是 noexcept 正确的,这已在 gcc 4.7 中得到解决。 ,
至于隐含生成的成员函数何时为 noexcept,这在 §15.4/14 中有记录。基本上,如果它需要调用的所有函数都是 noexcept
,则它是 noexcept
。
An implicitly declared special member function (Clause 12) shall have an exception-specification. If
f
is an implicitly declared default constructor, copy constructor, move constructor, destructor, copy assignment operator, or move assignment operator, its implicit exception-specification specifies the type-idT
if and only ifT
is allowed by the exception-specification of a function directly invoked byf
’s implicit definition;f
shall allow all exceptions if any function it directly invokes allows all exceptions, andf
shall allow no exceptions if every function it directly invokes allows no exceptions.
关于c++ - 隐式生成的成员和 noexcept,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9180164/
考虑: 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
我是一名优秀的程序员,十分优秀!