这个问题对于嵌入式开发尤为重要.异常处理为生成的二进制输出增加了一些空间.另一方面,没有例外,错误需要以其他方式处理,这需要额外的代码,这最终也会增加二进制大小.
我对你的经历很感兴趣,特别是:
请仅以我的问题为指导.欢迎任何输入.
附录:对于特定的C++对象/可执行文件,是否有任何人具有具体的方法/脚本/工具,它将显示由编译器生成的代码和专用于异常处理的数据结构占用的已加载内存占用的百分比?
当我用GCC编译时:
struct A
{
A();
A(const A &);
A(A &&) = delete;
};
void ugh(A);
A bar()
{
A a;
ugh(a);
return(a);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
x.cpp: In function ‘A bar()’:
x.cpp:14:13: error: use of deleted function ‘A::A(A&&)’
return(a);
^
x.cpp:5:5: note: declared here
A(A &&) = delete;
^
Run Code Online (Sandbox Code Playgroud)
实际上,将return语句中的a视为rvalue(因为它即将被销毁)确实是有意义的.但是,标准是否要求rvalues是匿名的?
此外,即使在这种情况下允许使用移动构造函数,为什么还需要它?为什么编译器不能使用复制构造函数,因为移动构造函数不可用?
他之前的问题的答案,返回本地对象是否需要移动语义?,主要解决我的问题,但不要触及命名变量如何成为没有调用std :: move()的右值.
编辑:这两个链接解释了为什么gcc的行为是正确的,并阐明了为什么标准需要这种行为: 为什么C++ 11删除的函数参与重载解析? http://en.cppreference.com/w/cpp/language/return
我不知道为什么这个函数需要"C"而不是"C++"链接.
具体来说,以下之间是否有任何有效的区别:
i = a.load(memory_order_acquire);
Run Code Online (Sandbox Code Playgroud)
或者
a.store(5, memory_order_release);
Run Code Online (Sandbox Code Playgroud)
和
atomic_thread_fence(memory_order_acquire);
i = a.load(memory_order_relaxed);
Run Code Online (Sandbox Code Playgroud)
或者
a.store(5, memory_order_relaxed);
atomic_thread_fence(memory_order_release);
Run Code Online (Sandbox Code Playgroud)
分别?
非宽松原子访问是否提供信号栅栏和线程栅栏?
为什么大多数C++编译器能够推断出它的类型::isspace并将其隐式转换为std::function,但是他们无法做到这一点std::isspace?
请参阅以下不编译的内容:
#include <cctype>
#include <functional>
template <typename Functish>
bool bar1(Functish f) { return f('a'); }
inline bool bar2(std::function<bool(char)> f) { return f('a'); }
#if 1
#define ff &std::isspace
#else
#define ff &::isspace
#endif
#if 0
bool foo()
{
return bar1(ff);
}
#else
bool foo()
{
return bar2(ff);
}
#endif
Run Code Online (Sandbox Code Playgroud)
在Compiler Explorer支持的编译器中,ELLCC似乎是唯一std::isspace具有我期望的可推导性/可转换性的编译器.
举个具体的例子,std::vector的push_back()成员函数在C++20中变成了constexpr。
https://en.cppreference.com/w/cpp/container/vector/push_back
如果我将下面代码中的CLASS更改为0而不是1,它将使用gcc或clang进行编译.但是如果CLASS为1,则两个编译器都会失败(在第二次使用bar时).
#define CLASS 1
#include <utility>
void f(int *a);
template <typename Arg>
#if CLASS
struct bar {
#else
void
#endif
bar(Arg && arg) { f(std::forward<Arg>(arg)); }
#if CLASS
};
#endif
void foo()
{
int dummy;
int *p = &dummy;
#if !CLASS
#define a
#define b
#endif
bar a(p + 0);
bar b(p);
}
Run Code Online (Sandbox Code Playgroud)
作为两个单独的片段,没有条件编译:
失败:
#include <utility>
void f(int *a);
template <typename Arg>
struct bar {
bar(Arg && arg) { f(std::forward<Arg>(arg)); }
};
void foo()
{
int dummy;
int …Run Code Online (Sandbox Code Playgroud)