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 

Problem with template and automatic conversion

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Peter Gerell
Guest





PostPosted: Fri Mar 05, 2004 2:37 pm    Post subject: Problem with template and automatic conversion Reply with quote



Hi, I have the following code:

-----------------
template <typename T>
class C {
public:
C(T t) {}
};

template<typename T>
void operator+(const C<T>& lhs , const C<T>& rhs) {
}

void test() {
C<int> c(3);
c + c;
c + 4; // Line 14
4 + c; // Line 15
}
------------------------

I get the following errors even though there is a public non-explicit
constructor in C<T>.
I would appreciate if someone could explain why this doesn't work as I
expect.

"ComeauTest.c", line 14: error: no operator "+" matches these operands
operand types are: C<int> + int
c + 4;
^

"ComeauTest.c", line 15: error: no operator "+" matches these operands
operand types are: int + C<int>
4 + c;
^


If I add the following two functions one of the errors go away, but one
still remains.
----------------
template<typename T>
void operator+(const C<T>& lhs , T& rhs) {
}
template<typename T>
void operator+(const T& lhs , const C<T>& rhs) {
}
----------------
"ComeauTest.c", line 21: error: no operator "+" matches these operands
operand types are: C<int> + int
c + 4;
^

Thanks,
Peter



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Peter Gerell
Guest





PostPosted: Sat Mar 06, 2004 12:10 am    Post subject: Re: Problem with template and automatic conversion Reply with quote



Quote:
If I add the following two functions one of the errors go away, but one
still remains.
----------------
template<typename T
void operator+(const C }
template<typename T
void operator+(const T& lhs , const C }
Hi again,

I noticed the mistake here myself as soon as the message appeared on the
news server.
I missed a "const" in the first function.
template<typename T>
void operator+(const C<T>& lhs , const T& rhs) {
}

However, the original problem still exist. Why do I have to write these two
functions in the first place?

Thanks,
Peter



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephen C. Dewhurst
Guest





PostPosted: Sat Mar 06, 2004 12:11 am    Post subject: Re: Problem with template and automatic conversion Reply with quote



On 5 Mar 2004 09:37:02 -0500, "Peter Gerell" <news1 (AT) gerell (DOT) se> wrote:

Quote:
Hi, I have the following code:

-----------------
template <typename T
class C {
public:
C(T t) {}
};

template void operator+(const C }

The short answer is that you don't get user-defined conversions for
function templates.

The somewhat longer answer is that Dan Saks presented a good solution
for this problem at The C++ Seminar in a presentation called "Making
New Friends." I don't think the paper has been published elsewhere,
but basically you want to do something like this:

template <typename T>
class C{
public:
C(T);
friend void operator +(const C &lhs, const C &rhs) {}
};

The friend function is not a template, so the compiler will apply
user-definded conversions to the actual arguments, and the C class
template will generate the appropriate version of (the non-member,
non-template) operator + for each instantiation. Cute, huh?

Steve

Steve Dewhurst
www.semantics.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.