Topic: implicit conversion and overload resolution


Author: huynh@anubisinc.com
Date: 1999/04/16
Raw View
I have the following code where:
- A is a template class with two inserters one for void* and one for T*.
This template is instanciated with T=char.
- X is a class with a conversion operator to char *.

I try to insert an X into a A. I expected the char* inserter to be called.
When this inserter is a member, it's fine.

#include <iostream>
using std::cout;

template<class T> class A {
public:
  void operator <<(void *x) { cout << "Wrong\n"; }
  void operator <<(T *x) { cout << "Right\n"; }
};

class X {
public:
  operator char *() { return "Hello"; };
};

// template<class T>
// void operator <<(A<T> &a, T *x) { cout << "Right\n"; }

int main() {
  X x;
  A<char> a;

  a << x;

  return 0;
}

When I declare it as a normal function, the other inserter is called instead.
Is this what the standard requires?


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]