I have checked out the definition of std::ios::app in /usr/include/c++/4.6/bits/ios_base.h and found that std::ios::app is defined as a const static variable:
typedef _Ios_Openmode openmode;
/// Seek to end before each write.
static const openmode app = _S_app;
in which _Ios_Openmode is defined in the same header file as
enum _Ios_Openmode
{
_S_app = 1L << 0,
_S_ate = 1L << 1,
_S_bin = 1L << 2,
_S_in = 1L << 3,
_S_out = 1L << 4,
_S_trunc = 1L << 5,
_S_ios_openmode_end = 1L << 16
};
It's well known that static variable has internal linkage and every translation unit has its own copy of this static variable, which means static variables in different translation unit should have different addresses. However, I have used two separate programs to print address of std::ios::app and found that the printed address are the same:
source file test1.cpp
#include <iostream>
int main() {
std::cout << "address: " << &std::ios::app << std::endl;
return 0;
}
result
address: 0x804a0cc
source file test2.cpp is the same with test1.cpp and the result is the same:
address: 0x804a0cc
This really confused me, shouldn't static variables in different translation units have different addresses?
Update: as is pointed out in the comments, std::ios::app is a static data member rather than a static const variable; static data member has external linkage and addresses of static data member in different translation unit should be the same. The second point is that my method for validating this fact is wrong: different program does not mean different translation unit.