c ++中的静态成员函数是否以多个翻译单元复制? [英] Are static member functions in c++ copied in multiple translation units?

查看:157
本文介绍了c ++中的静态成员函数是否以多个翻译单元复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序中有一个帮助类,它有许多静态函数在我的程序的不同类中使用。例如



helper.h

 类帮助器{
public:
static void fn1()
{/ *在头本身中定义* /}

/ * fn2定义在src文件中helper.cpp * /
static void fn2();
}

助手只有静态成员函数。因此,没有其他模块创建助手的对象。辅助函数用于其他模块,如:



A.cpp

  #includehelper.h
A :: foo(){
helper :: fn1
helper :: fn2();
}

B.cpp

  #includehelper.h
B :: foo(){
helper :: fn1 ();
helper :: fn2();
}

编译器是否在 A.cpp B.cpp ?我读了一些较早的帖子,我从编译器将创建的回复收集。但是当我打印 fn1 fn2 的地址为 printf(fn1的地址%p \\\
,& helper :: fn1);
printf(fn1的地址是%p \\\
,& helper :: fn1) ; 从A.cpp B.cpp ,我得到相同的地址。我现在很困惑。有人可以澄清,如果我错过了什么。



我担心辅助函数的多个副本的原因(如果它发生)是我们正在努力减少我们的可执行文件大小,并希望优化它。

解决方案

在类体中定义的函数隐式标记为 inline 。如果你获取函数的地址,编译器也会创建函数的正式副本(每个编译单元),但是链接器只会选择其中一个副本包含在可执行文件中,因此只有一个地址。 p>

但是,内联过程可以生成函数的许多副本,甚至超过编译单元的数量。通常,通过消除参数传递和函数调用开销以及用于公共子表达式消除的机会等,可以通过增加的优化来补偿复制代码的增加的大小。尽管内联通常被认为是尺寸和速度之间的权衡,



在类中声明的然后在单个编译单元中实现的函数,在可执行文件中只有一个副本。


I have a helper class in my program which has many static functions used in different classes of my program. E.g.

helper.h:

Class helper {
 public: 
   static void fn1 () 
   { /* defined in header itself */ }

   /* fn2 defined in src file helper.cpp */
   static void fn2(); 
}

Helper has only static member functions. So, no objects of helper are created by other modules. Helper functions are used in other modules like:

A.cpp

#include "helper.h"
A::foo() {
  helper::fn1(); 
  helper::fn2();
}

B.cpp

#include "helper.h"
B::foo() {
  helper::fn1();
  helper::fn2(); 
}

Does the compiler create separate copies of helper functions in A.cpp and B.cpp? I read some earlier posts and I gathered from the replies that compiler will create so. But when I print the address of fn1 and fn2 as printf("Address of fn1 is %p\n", &helper::fn1); and printf("Address of fn1 is %p\n", &helper::fn1); from both A.cpp and B.cpp, I get the same address. I'm confused now. Can someone clarify, If I'm missing something.

The reason I'm worried about multiple copies of helper functions (if it happens) is we are trying to reduce our executable size and wanted to optimize it.

解决方案

Functions defined inside the class body are implicitly marked inline. If you take the address of the function, the compiler will also create a regular copy of the function (per compilation unit), but the linker will pick just one of these copies to include in the executable, so there's only one address.

However, the inlining process could make many copies of the function, even more than the number of compilation units. Often the increased size of duplicating the code is offset by the increased optimization possible by eliminating argument passing and function call overhead, as well as opportunities for common subexpression elimination, etc. Although inlining is often considered a tradeoff between size and speed, the size increase is often negligible or even negative.

The function that's just declared in the class and then implemented in a single compilation unit, definitely has just one copy in the executable.

这篇关于c ++中的静态成员函数是否以多个翻译单元复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆