Topic: Conversion ambiguity
Author: "Lucian Radu Teodorescu" <Luc.Teodorescu@gmail.com>
Date: Fri, 25 Aug 2006 10:00:22 CST Raw View
Hello
I've posted this message alson on comp.lang.c++.moderated, but it seems
that we have blocked there, as we need an C++ expert on overloading to
make things clear, and none responded there. We decided to seek here
for help.
You can find the original post (with some debates) here:
http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/d7a9a97c5276ec64
Basically we have the following code:
#include <iostream>
using namespace std;
struct Target;
struct Source;
struct Target
{
Target() {}
Target(const Source& other)
{
cout << "Target: conversion constructor" << endl;
}
};
struct Source
{
Source() {}
operator Target () // Note, no const here
{
cout << "Source: conversion operator" << endl;
return Target();
}
};
int main(int argc, char* argv[])
{
Target t;
Source s;
t = s;
return 0;
}
We failed to decide if this code is legal in C++. Different compiler
give different results. For example MSVC++ 8.0 fails to compile this
(in both cases where the conversion operator is non-const and const),
and GCC 3.4.3 will always compile the given code, no matter if the
conversion operator is const or not. (If the conversion operator is not
const, the conversion operator will get called, and if it is const, the
conversion constructor will be called).
The questions are:
1. Should the above code compile?
2. If yes, what is the result?
3. If the conversion function is const, should the code compile?
4. If the conversion function is const and the code compiles, what is
the result?
Thank you,
Lucian Radu Teodorescu
---
[ 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://www.comeaucomputing.com/csc/faq.html ]