 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Abrahams Guest
|
Posted: Thu Jun 26, 2003 2:20 pm Post subject: Re: Detecting the presence of an operator |
|
|
[email]ersmith (AT) ucsd (DOT) edu[/email] (Erik Smith) writes:
| Quote: | There's an easy solution to this one: You've changed the call in
sizeof
from "tester(t + u)". If you change it back to that, it should work.
It
has to match the overloaded operator.
That was a posting mistake: the above error is, in fact, for tester(t
+ u). It also occurs for other operators such as ==. Corrected code:
struct operator_not_present {};
struct proxy {
proxy(...);
};
operator_not_present operator+(const proxy &,const proxy &);
char tester(...);
char (&tester(const operator_not_present &))[2];
template<class T,class U=T> struct has_plus_op {
static T t;
static U u;
static const bool value=sizeof(test(t + u))==1;
};
class A {};
class B {};
std::cout << has_plus_op
The compiler error is : illegal operands 'A' + 'A'
|
A bunch of us on the C++ committee came up with a pretty good
solution for this at the Curacao meeting.
http://lists.boost.org/MailArchives/boost/msg29007.php
Shows an implementation. I think Mat might've left me out of the
credits ;-)
Enjoy,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Terje Slettebų Guest
|
Posted: Sun Jun 29, 2003 2:05 pm Post subject: Re: Detecting the presence of an operator |
|
|
"David Abrahams" <dave (AT) boost-consulting (DOT) com> wrote
| Quote: | ersmith (AT) ucsd (DOT) edu (Erik Smith) writes:
There's an easy solution to this one: You've changed the call in
sizeof
from "tester(t + u)". If you change it back to that, it should
work.
It
has to match the overloaded operator.
That was a posting mistake: the above error is, in fact, for
tester(t
+ u). It also occurs for other operators such as ==. Corrected
code:
struct operator_not_present {};
struct proxy {
proxy(...);
};
operator_not_present operator+(const proxy &,const proxy &);
char tester(...);
char (&tester(const operator_not_present &))[2];
template<class T,class U=T> struct has_plus_op {
static T t;
static U u;
static const bool value=sizeof(test(t + u))==1;
};
class A {};
class B {};
std::cout << has_plus_op
The compiler error is : illegal operands 'A' + 'A'
|
If one corrects "test" to "tester" in this code (or vice versa), it
compiles cleanly on Intel C++ 7.1, at least. By the way, this may be yet
another difference between the tested and the posted code (besides
"test"/"tester"), but it's a strange error message, for code that tries
to add A + B. You should post the exact code you've tested, to avoid
errors in transcribing it, like this.
| Quote: | A bunch of us on the C++ committee came up with a pretty good
solution for this at the Curacao meeting.
|
Yes, that solution was the basis of the above one, as mentioned in the
first posting in this thread
(http://www.google.com/groups?selm=GZ0Ba.8969%24KF1.135340%40amstwist00)
:
"This is inspired by "IsAddable" by Dietmar Kuehl, Jaakko Jarvi, Jeremy
Siek and Mat Marcus."
The code was also on one of Herb Sutter's slides at the ACCU conference,
which is where I got it from (the slides used "IsAddable" instead of
"is_addable").
However, as I said in that posting, my version is different in that it
allows implicit conversions on the operands. This may be useful for
generic concept checking, where the important question is whether or not
you can add two values. The original is_addable would give true for int
+ int, but false for int + double. The version above will give true in
both cases.
It seems that way. The same credits were on Herb Sutter's slides.
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|