Reputation: 1580
#include <iostream>
int main()
{
std::cout << 1.0;
return 0;
}
I use command g++ -E main.cpp
for getting this compile unit after preprocessor run.
After preprocessing it contains about 20k lines.
And it contains this part of code with definition of operator<<(double __f)
.
namespace std __attribute__ ((__visibility__ ("default")))
{
# 55 "/usr/include/c++/4.7/ostream" 3
template<typename _CharT, typename _Traits>
class basic_ostream : virtual public basic_ios<_CharT, _Traits>
{
public:
typedef _CharT char_type;
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
typedef _Traits traits_type;
typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
typedef basic_ios<_CharT, _Traits> __ios_type;
typedef basic_ostream<_CharT, _Traits> __ostream_type;
typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
__num_put_type;
typedef ctype<_CharT> __ctype_type;
# 81 "/usr/include/c++/4.7/ostream" 3
explicit
basic_ostream(__streambuf_type* __sb)
{ this->init(__sb); }
//.........................................................
__ostream_type&
operator<<(double __f)
{ return _M_insert(__f); }
//.........................................................
};
}
Next, run
g++ -c main.cpp
and nm main.o
000000000000005a t _GLOBAL__sub_I_main
000000000000001d t _Z41__static_initialization_and_destruction_0ii
U _ZNSolsEd
U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev
U _ZSt4cout
0000000000000000 r _ZStL19piecewise_construct
0000000000000000 b _ZStL8__ioinit
U __cxa_atexit
U __dso_handle
0000000000000000 T main
_ZNSolsEd
is mangled name for operator<<(double)
and that line has "U" The symbol is undefined
. But definition for this operator is in compiled source. What is reason for that?
Upvotes: 2
Views: 304
Reputation: 7925
This line:
extern template class basic_ostream<char>;
tells the compiler that the function is already available elsewhere and it should not emit any code for it (except possibly for inlining), like inline
does in C99.
Upvotes: 1