C++ 中有多少份自动生成的赋值运算符(MS Visual Studio 2008)?
如果我有一个 POD(普通旧数据)C++ 类 Foo
,普遍的观点是不需要为其定义复制构造函数或赋值运算符,因为 C++ 会自动执行此操作。
我的问题是,如果 Foo.h
包含在多个 .cpp
文件中,并且在每个 文件中调用
文件,VS2008 会在生成的 Foo
赋值运算符.cpp.obj
文件中生成默认赋值运算符的多个副本吗? (我从事一个非常大的项目,我正在尝试减少构建过程中生成的二进制文件的大小。)
If I have a POD (Plain Old Data) C++ class Foo
, the common view is that one does not need to define a copy constructor nor an assignment operator for it, because C++ will do it automatically.
My question is, if Foo.h
is included in multiple .cpp
files and the Foo
assignment operator is invoked in each of these .cpp
files, will VS2008 generate multiple copies of the default assignment operator in the resulting .obj
files? (I work on a very large project and I'm trying to reduce the size of binary files generated during the build.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该运算符将被发送到所有这些 .obj 文件中(就像任何其他内联函数一样)。但是,除了其中一个函数之外的所有函数都将被链接器丢弃(如果启用了优化)。
The operator will be emitted into all of those .obj files (just like any other inline function). However, all but one of those functions will be discarded by the linker (if you have optimizations enabled).
它取决于编译器以及用于调用编译器的选项。
编译器生成的函数被认为是声明为内联的,但是
这对于生成的代码意味着什么完全取决于
编译器。
大多数编译器都可以选择不进行任何内联操作(因此您可能会
只获得一份)。然而,这是否会使代码更小,
取决于;如果生成的构造函数非常简单,则生成它
内联可能需要比调用所需的代码更少的空间
非内联副本。
大多数编译器还具有优化空间的选项,而不是
执行时间(例如,对于 VC++,
/O1 /Os
;或者对于 g++,-Os
)。 ID从使用这些开始。
It depends on the compiler, and the options used to invoke the compiler.
Compiler generated functions are considered to be declared
inline
, butwhat that means in terms of generated code is totally up to the
compiler.
Most compilers have an option to make nothing inline (so you'll probably
only get one copy). Whether this will make the code smaller, however,
depends; if the generated constructor is very simple, generating it
inline may require less space than the code necessary to call a
non-inline copy.
Most compilers also have options to optimize for space, rather than
execution time (
/O1 /Os
for VC++, for example; or-Os
for g++). I'dstart by using these.