C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Conversion ambiguity

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Lucian Radu Teodorescu
Guest





PostPosted: Fri Aug 25, 2006 4:00 pm    Post subject: Conversion ambiguity Reply with quote



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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.