K
kevin
Hello!
So I was reading O'Reilly's C++ in a Nutshell when I came accross
something interesting:
The class definition for basic_ostream contains numerous overloaded
operator<< functions:
template <class charT, class traits = char_traits<charT> >
class basic_ostream : virtual public basic_ios<charT,traits>
{
public:
....
basic_ostream<charT,traits>& operator<< (bool n);
basic_ostream<charT,traits>& operator<< (short n);
basic_ostream<charT,traits>& operator<< (int n);
....
etc.
}
which makes sense since these are used for statements like
cout << 45 << 34 << endl;
But the functions to output strings and characters are implemented as
function templates elsewhere and *not* as member function of
basic_ostream :
template<class charT, class traits>
basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>&
out, char c);
template<class traits>
basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>&
out, char c);
Could anyone shed some light on why the STL would implement these
functions as function templates and not as memebr functions of the
basic_ostream class? (I am probably missing something bigger here,
since I'm only a fairly intermediate C++ programmer and I don't know
the STL in depth).
thanks
-kevin
So I was reading O'Reilly's C++ in a Nutshell when I came accross
something interesting:
The class definition for basic_ostream contains numerous overloaded
operator<< functions:
template <class charT, class traits = char_traits<charT> >
class basic_ostream : virtual public basic_ios<charT,traits>
{
public:
....
basic_ostream<charT,traits>& operator<< (bool n);
basic_ostream<charT,traits>& operator<< (short n);
basic_ostream<charT,traits>& operator<< (int n);
....
etc.
}
which makes sense since these are used for statements like
cout << 45 << 34 << endl;
But the functions to output strings and characters are implemented as
function templates elsewhere and *not* as member function of
basic_ostream :
template<class charT, class traits>
basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>&
out, char c);
template<class traits>
basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>&
out, char c);
Could anyone shed some light on why the STL would implement these
functions as function templates and not as memebr functions of the
basic_ostream class? (I am probably missing something bigger here,
since I'm only a fairly intermediate C++ programmer and I don't know
the STL in depth).
thanks
-kevin