LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2007, 07:40 AM   #1
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
C++ templated operators


The following code compiles under GNU C++:

#include <boost/shared_ptr.hpp>

class A {};
class B : public A {};

template<typename T>
T operator +(boost::shared_ptr<B> b) {
return (T&)(b);
}

int main() {
using namespace boost;
shared_ptr<B> b(new B);

shared_ptr<A> a;
//a = +b;
}


If you un-comment the last line, however, it stops compiling.

Is there a way to overload a unary operator (+ is a valid unary operator) within a template, and pass in the template parameters when the operator is used?

I've tried the obvious a = +<A*>B; but that just gives a syntax error.

(What I actually wanted to do was to pass in a functor to be run over every object in in a list, and use unary + to generate a total of whatever the functor returned; I thought this test case would be simpler code).

Anyone know? Or is this simply a valid template that generates invalid code?

Last edited by rjlee; 08-04-2007 at 05:18 AM.
 
Old 08-03-2007, 09:10 AM   #2
sjr
Member
 
Registered: Oct 2002
Location: Maryland
Distribution: SUSE 10.1 and 10.2
Posts: 44

Rep: Reputation: 15
What version of GCC do you have? It compiles for me; GCC reports v 4.1.0.
 
Old 08-03-2007, 09:13 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it compiles OK on cygwin and
SunOS primadtpdev 5.8 Generic_108528-20 sun4u sparc SUNW,Ultra-250


????

gcc version 3.4.6

Last edited by bigearsbilly; 08-03-2007 at 09:15 AM.
 
Old 08-03-2007, 03:06 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Original Poster
Rep: Reputation: 76
Sorry; the original post did compile. I thought I had reduced it to a proper test case as it didn't compile under GCC-2.96, but in 3.x series compiles it worked okay.

I have put a better use-case in an edit to the original post. This claims that there is no matching operator for + in +b in 4.1.2 (Ubuntu).

Is it possible to pass a template parameter explicitly to an operator?
 
Old 08-03-2007, 06:18 PM   #5
sjr
Member
 
Registered: Oct 2002
Location: Maryland
Distribution: SUSE 10.1 and 10.2
Posts: 44

Rep: Reputation: 15
Maybe you have to dereference b and then take its address; its type is shared_ptr<B>, whereas the + operator that you defined takes a "B &". I don't know about shared_ptr, so I don't know how you dereference it, but if they overload operator*, then you might have to do something like this:

Code:
a = +(&(*b));
or maybe just "a = +(*b);" will work. Or you can redefine operator+ to accept a shared_ptr<B> and return a shared_ptr<A>.
 
Old 08-04-2007, 05:17 AM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Original Poster
Rep: Reputation: 76
Sorry; stupid mistake.

The operator should have taken a shared_ptr; I have corrected my example again.

If you are intirested, the shared_ptr class is documented at http://www.boost.org/libs/smart_ptr/shared_ptr.htm

And my example does now compile.

I do still have a case that doesn't, but it's quite a large block of code and I think I've got a workaround anyway. If I can reduce it to a manageable test case when I have more time then I'll re-post.

Sorry for wasting your time, and thanks for your help,
 
  


Reply

Tags
c++, template



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Templated Class and Borland C++ Compiler enemorales Programming 2 09-25-2010 02:30 PM
c++ templated iterators problem gearoid_murphy Programming 6 01-10-2007 07:40 AM
pointers to templated c++ functions gearoid_murphy Programming 11 08-11-2006 01:12 PM
C++: Specialized templated member functions? spursrule Programming 1 07-01-2006 07:16 PM
trouble making a [simple] templated node-based stack nadroj Programming 4 02-14-2006 05:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration