 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Markus Moll Guest
|
Posted: Mon Jul 03, 2006 7:34 pm Post subject: Verwirrung um operator>-template aus <utility> |
|
|
Hallo
Ich bin gerade auf ein Problem gestoßen, das ich mir gerade nicht erklären
kann und zu dem ich nichts im Netz finde:
--- 8X 8X 8X ---
#include <utility>
#include <functional>
using namespace std::rel_ops;
using namespace std;
struct A
{
A(int x) : x(x) {}
int x;
bool operator< (const A& a) const { return x < a.x; }
};
int main()
{
A a(42), b(23);
return greater<A>()(b,a);
}
--- 8X 8X 8X---
Das kompiliert hier leider nicht (g++ 3.4.6), der Fehler (mit Verweis auf
greater<A>) ist: "Fehler: no match für »operator>« in »__x > __y«"
Es kompiliert aber sofort, wenn ich "greater<A>()(b,a)" durch "b > a"
ersetze. Übersehe ich etwas beim name-lookup? Meiner Meinung nach müßte
template<typename T> operator> (const T&, const T&) doch auch im Aufruf von
greater<A>()(b,a) gefunden werden?
Gruß
Markus
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de |
|
| Back to top |
|
 |
Christoph Rabel Guest
|
Posted: Mon Jul 03, 2006 7:41 pm Post subject: Re: Verwirrung um operator>-template aus <utility> |
|
|
Markus Moll schrieb:
| Quote: |
bool operator< (const A& a) const { return x < a.x; }
^^
Das kompiliert hier leider nicht (g++ 3.4.6), der Fehler (mit Verweis auf
greater<A>) ist: "Fehler: no match für »operator>« in »__x > __y«"
^^ |
Falscher Operator. Du musst operator> implementieren...
mfg
Christoph
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Jul 03, 2006 9:44 pm Post subject: Re: Verwirrung um operator>-template aus <utility> |
|
|
Markus Moll <moll (AT) rbg (DOT) informatik.tu-darmstadt.de> writes:
| Quote: | Ich bin gerade auf ein Problem gestoßen, das ich mir gerade nicht erklären
kann und zu dem ich nichts im Netz finde:
--- 8X 8X 8X ---
#include <utility
#include <functional
using namespace std::rel_ops;
|
Diese Direktive importiert std::rel_ops::operator> in den globalen
Namespace.
| Quote: | using namespace std;
struct A
{
A(int x) : x(x) {}
int x;
bool operator< (const A& a) const { return x < a.x; }
};
int main()
{
A a(42), b(23);
return greater<A>()(b,a);
|
Die Instantiierung von std::greater mit A geschieht im namespace std,
und dorthin hast Du std::rel_ops::operator> nicht importiert (Du
kannst auch nicht in einem Programm mit definiertem
Verhalten). Deshalb wird dieser Operator dort nicht gefunden.
Wenn Du vor der Definition der Funktion main()
namespace std
{
using std::rel_ops::operator>;
}
schreibst (nein, das darf man nicht, aber man ist ja neugierig),
übersetzt gcc das Program anstandslos.
Wenn Du andererseits in main()
b>a
schreibst, befindet sich der Aufruf von > im globalen Namensraum,
wohin Du std::rel_ops::operator> importiert hast.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Tue Jul 04, 2006 9:11 am Post subject: Re: Verwirrung um operator>-template aus <utility> |
|
|
Markus Moll wrote:
| Quote: | Ich bin gerade auf ein Problem gestoßen, das ich mir gerade
nicht erklären kann und zu dem ich nichts im Netz finde:
--- 8X 8X 8X ---
#include <utility
#include <functional
using namespace std::rel_ops;
using namespace std;
struct A
{
A(int x) : x(x) {}
int x;
bool operator< (const A& a) const { return x < a.x; }
};
int main()
{
A a(42), b(23);
return greater<A>()(b,a);
}
--- 8X 8X 8X---
Das kompiliert hier leider nicht (g++ 3.4.6), der Fehler (mit
Verweis auf greater<A>) ist: "Fehler: no match für »operator>«
in »__x > __y«"
Es kompiliert aber sofort, wenn ich "greater<A>()(b,a)" durch
"b > a" ersetze. Übersehe ich etwas beim name-lookup? Meiner
Meinung nach müßte template<typename T> operator> (const T&,
const T&) doch auch im Aufruf von greater<A>()(b,a) gefunden
werden?
|
Thomas Maeder hat schon erklart, warum es nicht geht. Ich würde
aber sagen, dass ich »using namespace std::rel_ops« zu brutal
finde. Viel lieber definiere ich etwas wie:
template< typename T >
struct AdditionalComparisonOperators
{
friend bool operator>( T const& lhs, T const& rhs )
{
return rhs < lhs ;
}
// ...
} ;
und dans erbe davon:
struct A : public AdditionalComparisonOperators< A >
{
// ...
bool operator<( A const& other ) const { /* ... */ }
} ;
Damit kontrolliere ich bei jeder Klasse, ob sie die Operatore
bekommt oder nicht.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de |
|
| 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
|
|