Re: Why koenig lookup?
On Aug 11, 8:10 pm, Diego Martins <jose.di...@gmail.com> wrote:
On Aug 11, 8:04 am, "Alf P. Steinbach" <al...@start.no> wrote:
* Alf P. Steinbach:
I now think I/we? have been blaming the wrong guy, ADL.
The whole thing about global namespace operator<< overload
problem, being just a case of name hiding.
what is the best solution for the operator<< example presented
above? I runned into the same problem once and I putted my
operator<< inside std:: as a quick non-std solution :-(
You mean overloading operator<< for something like
std::vector<double>? The best solution is not to do it, at all.
What happens if a collegue is also using std::vector<double>,
and also wants to overload<< for it?
What you want is a type with known semantics, or rather, with a
known standard display format. In mathematics, for example,
you'd almost certainly have a Vector class, with various
additional operators not supported by std::vector<double>.
Unless it was a Polynome class. In exceptional cases where you
really do have a raw std::vector, you can create a small
decorator class which does the trick:
template< typename T >
class AsVector // mathematical vector...
{
public:
explicit AsVector( std::vector< T > const& v )
: myV( v )
{
}
friend std::ostream&
operator<<( std::ostream& dest,
AsVector const& source ) ;
private:
std::vector< T > const&
myV ;
} ;
template< typename T >
inline AsVector< T >
asVector( std::vector< T > const& v )
{
return AsVector< T >( v ) ;
}
AsPolynome would be similar, except that the code for the
operator<< would be very different.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34