 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tobias Wollgam Guest
|
Posted: Thu Jun 17, 2004 6:46 am Post subject: set::find const Problem |
|
|
Hallo,
ich folgendes Problem:
template<class type,class trait = x>
class A
{
public:
bool has(const std::set<A& s,const A<type,trait>* a) const
{
return s.find(a) != s.end();
}
private:
std::set<A _siblings;
};
....
Wird vom gcc angemeckert mit:
error: passing `const std::set<A
IntTrait>*, std::less<><int, IntTrait>*>,
std::allocator<A >' as `this' argument of `
typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare,
_Alloc>::const_iterator std::set<_Key, _Compare, _Alloc>::find(const
_Key&)
[with _Key = A<int, IntTrait>*, _Compare =
std::less<A, _Alloc =
std::allocator<A]' discards qualifiers
Warum? find erwartet doch eine const Referenz auf den Key und ist selbst
const deklariert.
TIA,
Tobias
--
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 |
|
 |
Helmut Zeisel Guest
|
Posted: Thu Jun 17, 2004 1:40 pm Post subject: Re: set::find const Problem |
|
|
Tobias Wollgam wrote:
Versuch mal
template<class type,class trait = x>
class A
{
public:
typedef std::set<A tSet;
bool has(const tSet& s, const tSet::value_type a) const
{
return s.find(a) != s.end();
}
private:
tSet _siblings;
};
Was passiert jetzt?
Helmut
--
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 |
|
 |
Torsten Robitzki Guest
|
Posted: Thu Jun 17, 2004 4:30 pm Post subject: Re: set::find const Problem |
|
|
Tobias Wollgam wrote:
| Quote: | Hallo,
ich folgendes Problem:
template<class type,class trait = x
class A
{
public:
bool has(const std::set& s,const A<type,trait>* a) const
{
return s.find(a) != s.end();
}
private:
std::set<A _siblings;
};
...
Wird vom gcc angemeckert mit:
error: passing `const std::set<A
IntTrait>*, std::less<><int, IntTrait>*>,
std::allocator<A >' as `this' argument of `
typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare,
_Alloc>::const_iterator std::set<_Key, _Compare, _Alloc>::find(const
_Key&)
[with _Key = A<int, IntTrait>*, _Compare =
std::less<A, _Alloc =
std::allocator<A]' discards qualifiers
Warum? find erwartet doch eine const Referenz auf den Key und ist selbst
const deklariert.
|
Hast A::operator<() einen const quafifier? Ein bischen mehr code wäre
hilfreich.
mfg Torsten
--
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 |
|
 |
helmut zeisel Guest
|
Posted: Thu Jun 17, 2004 5:00 pm Post subject: Re: set::find const Problem |
|
|
Torsten Robitzki wrote:
| Quote: | Ein bischen mehr code wäre
hilfreich.
|
Meine Glaskugel hat es mir verraten:
#include <set>
#include <algorithm>
template<class type>
class A
{
public:
bool has(const std::set<A& s,const A<type>* a) const
{
return s.find(a) != s.end();
}
private:
std::set<A _siblings;
};
int main()
{
A<int> a;
std::set<A s;
a.has(s, &a);
};
So kannst Du den Fehler reproduzieren. Was er will ist aber
bool has(const std::set<A& s,A<type>* const a) const;
Man beachte das geaenderte "const". Damit frisst es der Compiler.
Helmut
--
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 |
|
 |
Tobias Wollgam Guest
|
Posted: Fri Jun 18, 2004 9:22 am Post subject: Re: set::find const Problem |
|
|
Stimmt. Dann läßt sich aber a ändern und das möchte ich nicht.
Im Moment hilft mir nur ein const_cast :-(
Tobias
--
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 |
|
 |
Falk Tannhäuser Guest
|
Posted: Fri Jun 18, 2004 9:33 am Post subject: Re: set::find const Problem |
|
|
Helmut Zeisel wrote:
bzw. (finde ich einfacher zu verstehen und konsistenter)
'typename tSet::value_type const a' geschrieben werden, was auf
'A<type,trait>* const' hinausläuft, womit man aber kein 'A<type,trait> const*'
übergeben könnte?
Lässt sich das Problem nicht auf folgendes reduzieren:
std::set<int*> const s(/* Initialisierung */);
int const n = 666;
//std::set<int*>::const_iterator it = s.find(&n); // <= Geht nicht!
// find nimmt 'int* const&' als Parameter, daher keine Übergabe von 'int const*'
std::set
if(it != s.end())
++**it; // Nicht erreichbar!
Obiger const_cast ist deswegen sicher, weil man 'int const*' gar nicht erst
in ein 'std::set<int*>' hineinpacken kann, (es sei denn, per const_cast).
'find' gibt daher zwangsläufig 's.end()' zurück, kann also nie einen const_iterator
liefern, der auf '&n' verweist. Dies ergibt sich aus der Semantik des Containers,
kann aber vom Compiler nicht erkannt werden - der guckt sich nur stur die Typen
an und folgt den Sprachregeln.
Dies ist also eine Gelegenheit, wo meines Erachtens const_cast mit gutem Gewissen
verwendet werden kann.
Zum Problem des OP: Ich würde 'has' so überladen:
(statt 'A<type,trait>' schreibe ich innerhalb der Templatedefinition einfach 'A')
bool has(std::set<A*> const& s, A* a) const
{
return s.find(a) != s.end();
}
bool has(std::set<A*> const& s, A const* a) const
{
return has(s, const_cast<A*>(a));
// Sicher, siehe oben
}
MfG
Falk
--
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 |
|
 |
Falk Tannhäuser Guest
|
Posted: Fri Jun 18, 2004 9:41 am Post subject: Re: set::find const Problem |
|
|
Torsten Robitzki wrote:
| Quote: |
Hast A::operator<() einen const quafifier? Ein bischen mehr code wäre
hilfreich.
|
Wird der gebraucht? So wie das set beim OP definiert ist, wird std::less
verwendet (ergibt nach 20.3.3/8 eine lineare Ordnung für Zeiger), ohne das
das gezeigte Objekt eine Rolle spielt.
MfG
Falk
--
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 |
|
 |
Helmut Zeisel Guest
|
Posted: Fri Jun 18, 2004 9:53 am Post subject: Re: set::find const Problem |
|
|
Falk Tannhäuser wrote:
Stimmt. "typename" fehlt.
| Quote: | bzw. (finde ich einfacher zu verstehen und konsistenter)
'typename tSet::value_type const a' geschrieben werden, was auf
'A<type,trait>* const' hinausläuft, womit man aber kein 'A<type,trait> const*'
übergeben könnte?
|
Ja, genau das vermute ich. Da Tobias wie aus anderen Postings
erkenntlich nocht nicht so richtig "const-sattelfest" ist, will er
vermutlich sowieso 'A<type,trait>* const' und nicht 'A<type,trait>
const*' haben. Dein const_cast Zugang eruebrigt sich dann. Ohne Antwort
von Tobias werden wir das aber nicht klaeren koennen.
Helmut
--
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 |
|
 |
Tobias Wollgam Guest
|
Posted: Fri Jun 18, 2004 10:42 am Post subject: Re: set::find const Problem |
|
|
Helmut Zeisel wrote:
| Quote: | bzw. (finde ich einfacher zu verstehen und konsistenter)
'typename tSet::value_type const a' geschrieben werden, was auf
'A<type,trait>* const' hinausläuft, womit man aber kein 'A<type,trait
const*' übergeben könnte?
Ja, genau das vermute ich. Da Tobias wie aus anderen Postings
erkenntlich nocht nicht so richtig "const-sattelfest" ist, will er
vermutlich sowieso 'A
const*' haben. Dein const_cast Zugang eruebrigt sich dann. Ohne Antwort
von Tobias werden wir das aber nicht klaeren koennen.
|
Natürlich ist dem nicht so. Was mit dem Pointer in der Funktion passiert ist
mir doch egal. Das referenzierte Objekt soll nicht modifiziert werden,
also:
'A
und damit komme ich um einen const_cast scheinbar leider nicht herum.
Tobias
--
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 |
|
 |
Helmut Zeisel Guest
|
Posted: Fri Jun 18, 2004 11:45 am Post subject: Re: set::find const Problem |
|
|
Tobias Wollgam wrote:
| Quote: | bool has(const std::set<A& s,A<type>* const a) const;
Man beachte das geaenderte "const". Damit frisst es der Compiler.
Stimmt. Dann läßt sich aber a ändern und das möchte ich nicht.
Im Moment hilft mir nur ein const_cast
|
Wieso verwendest Du kein std::set<const A ?
Helmut
--
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 |
|
 |
Tobias Wollgam Guest
|
Posted: Fri Jun 18, 2004 12:36 pm Post subject: Re: set::find const Problem |
|
|
Helmut Zeisel wrote:
| Quote: | Wieso verwendest Du kein std::set<const A ?
|
Dann sind wir bei meinem anderen Thread. C++ kann std::set<x*> nicht in
const std::set<const x*> konvertieren, was zur Folge hätte, daß ich dort
zunächst einen neuen Kontainer anlegen müßte und dann ist mir doch ein
const_cast lieber.
Es sei denn ich übersehe hier etwas.
Gruß,
Tobias
--
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 |
|
 |
Torsten Robitzki Guest
|
Posted: Fri Jun 18, 2004 1:43 pm Post subject: Re: set::find const Problem |
|
|
Falk Tannhäuser wrote:
| Quote: | Torsten Robitzki wrote:
Hast A::operator<() einen const quafifier? Ein bischen mehr code wäre
hilfreich.
Wird der gebraucht? So wie das set beim OP definiert ist, wird std::less
verwendet (ergibt nach 20.3.3/8 eine lineare Ordnung für Zeiger), ohne das
das gezeigte Objekt eine Rolle spielt.
|
Ne, natürlich nicht :-)
--
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 |
|
 |
helmut zeisel Guest
|
Posted: Fri Jun 18, 2004 3:45 pm Post subject: Re: set::find const Problem |
|
|
Tobias Wollgam wrote:
| Quote: | Helmut Zeisel wrote:
Wieso verwendest Du kein std::set<const A ?
Dann sind wir bei meinem anderen Thread. C++ kann std::set<x*> nicht in
const std::set<const x*> konvertieren,
|
Ich weiß; meine Frage ist, ob Du wirklich ein std::set<x*> benötigst
bzw. ob es nicht ausreicht, nur mit set::<const x*> zu arbeiten.
Helmut
--
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 |
|
 |
Markus Breuer Guest
|
Posted: Fri Jun 18, 2004 10:52 pm Post subject: Re: set::find const Problem |
|
|
Tobias Wollgam wrote:
IMHO ein ähnliches Problem, wie im anderen Thread:
const std::set<A bzw. der Argumenttyp des templates
A<type,trait> ist nicht kompatibel zum Typ const A<type,trait>.
Ein "const type" ist für den Compiler ein anderer Typ, als "type".
template<class type,class trait = x>
class A
{
public:
typedef A<type,trait>* value_type;
typedef std::set< value_type > container_type;
typedef container_type::const_iterator const_it;
bool has( const_it _begin, const_it _end, value_type _value ) const
{
const_it it = std::find( _begin, _end, _value );
return it != _end;
}
IMHO sollte weder "const set<...>" noch "set<const ...>" verwendet
werden. Stattdessen gehört hier der iterator bzw. const_iterator hin.
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 |
|
 |
|
|
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
|
|