Topic: conversion operators in templates


Author: Boris Schaefer <sbo@psy.med.uni-muenchen.de>
Date: 1998/12/18
Raw View
Hi,

I just tried to compile the following and it failed on egcs 1.1b with
the following error message.

math.cc: In function `int main()':
math.cc:34: no matching function for call to `print_foo (bar<int> &)'

Now I wondered if the compiler is wrong or if this isn't in the
standard.  If the standard really doesn't allow/specify this, could
anybody please give me a reason on why this is so, since I think this
construct would be very useful.

#include <iostream>

template < typename T >
struct foo
{
  T i;
  foo(T t) : i(t) {}
};

template < typename T >
struct bar
{
  T i;
  bar(T t) : i(t) {}

  operator foo<T>() { return foo<T>(i); }
};

template < typename T >
void print_foo(foo<T> f)
{
  std::cout<<"print_foo: "<<f.i<<endl;
}

int main()
{
  foo<int> x(1);
  bar<int> y(2);

  foo<int> tmp = y;
  print_foo(y);

  return 0;
}
--
Boris Schaefer -- sbo@psy.med.uni-muenchen.de

Ambition, n:
 An overmastering desire to be vilified by enemies while
 living and made ridiculous by friends when dead.
  -- Ambrose Bierce


[ 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              ]