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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-03-2007, 07:40 AM
|
#1
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Rep:
|
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.
|
|
|
|
08-03-2007, 09:10 AM
|
#2
|
|
Member
Registered: Oct 2002
Location: Maryland
Distribution: SUSE 10.1 and 10.2
Posts: 44
Rep:
|
What version of GCC do you have? It compiles for me; GCC reports v 4.1.0.
|
|
|
|
08-03-2007, 09:13 AM
|
#3
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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.
|
|
|
|
08-03-2007, 03:06 PM
|
#4
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Original Poster
Rep:
|
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?
|
|
|
|
08-03-2007, 06:18 PM
|
#5
|
|
Member
Registered: Oct 2002
Location: Maryland
Distribution: SUSE 10.1 and 10.2
Posts: 44
Rep:
|
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:
or maybe just "a = +(*b);" will work. Or you can redefine operator+ to accept a shared_ptr<B> and return a shared_ptr<A>.
|
|
|
|
08-04-2007, 05:17 AM
|
#6
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Original Poster
Rep:
|
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,
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:09 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|