ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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>.
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,
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.