|
|
Constructor overload does fail
Hi!
Problem: The code is working fine on windows with visual studio 9 but
it is NOT working on most unix compilers. They are
yelling
about multiple constructors with same meaning
(example: second ctor can not overload the first)
What's the problem?
How can I fix the problem?
kindly
Thomas
APPENDIX:
/// type for container of sorted nodes.
typedef std::set<Key*, Compare> KeySorted;
One class is providing constructors this way:
class Iterator
{
public:
/// initializes the iterator to work with a std::set iterator.
Iterator(KeySorted::iterator iterator);
/// initializes the iterator to work with a std::set const
iterator.
Iterator(KeySorted::const_iterator iterator);
// ...
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
2/4/2009 12:33:17 AM |
|
On Feb 3, 11:33 pm, Thomas Lehmann <t.lehm...@rtsgroup.net> wrote:
> Hi!
>
> Problem: The code is working fine on windows with visual studio 9 but
> it is NOT working on most unix compilers. They are
> yelling
> about multiple constructors with same meaning
> (example: second ctor can not overload the first)
>
> What's the problem?
> How can I fix the problem?
>
> kindly
> Thomas
Probably the standard library on systems it fails uses
typedef const iterator const_iterator;
whereas on other systems it does not. Since you cannot overload a
function based on the constness of an argument, your code is therefore
illegal.
It's hard to suggest a specific workaround without knowing how you use
those iterators, because const_iterator is not necessarily convertible
from iterator, so if you are making a wrapper class, you would have to
have more than one member variable or something. If you could explain
the purpose of your iterator - or even better, provide a sample - then
we could help suggest alternative designs.
Sean
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Sean
|
2/4/2009 6:34:37 AM
|
|
On Feb 4, 6:33 am, Thomas Lehmann <t.lehm...@rtsgroup.net> wrote:
> Problem: The code is working fine on windows with visual studio 9 but
> it is NOT working on most unix compilers. They are
> yelling
> about multiple constructors with same meaning
> (example: second ctor can not overload the first)
>
> What's the problem?
> How can I fix the problem?
> /// type for container of sorted nodes.
> typedef std::set<Key*, Compare> KeySorted;
>
> One class is providing constructors this way:
>
> class Iterator
> {
> public:
> /// initializes the iterator to work with a std::set iterator.
> Iterator(KeySorted::iterator iterator);
> /// initializes the iterator to work with a std::set const
> iterator.
> Iterator(KeySorted::const_iterator iterator);
You have hit on of the murkier corners of C++. There is a discussion
of the problem and its resolution here:
http://gcc.gnu.org/onlinedocs/libstdc++/ext/lwg-defects.html#103
which can be summarised as:
"It is unspecified whether or not iterator and const_iterator [for
std::set] are the same type."
In your VC implemention I guess they are different types, and so can
be overloaded. For the UNIX compilers they are the same, and so can't
be overloaded. The following is from my g++ <set> header:
typedef typename _Rep_type::const_iterator iterator;
typedef typename _Rep_type::const_iterator const_iterator;
As to a solution, if you really, really need this functionality I
suppose you could use a std::map (which doesn't have this problem) and
fake out the map's values.
Neil Butterworth
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Neil
|
2/4/2009 6:38:11 AM
|
|
Sean Hunt wrote:
[const_iterator is the same type as iterator which causes overloads to fail]
> Probably the standard library on systems it fails uses
>
> typedef const iterator const_iterator;
That would be utterly useless, as such an iterator could not be used to
iterate. This is like a "const pointer", as opposed to
a "pointer-to-const".
> whereas on other systems it does not. Since you cannot overload a
> function based on the constness of an argument, your code is therefore
> illegal.
I'm pretty sure that there are two overloads of operator[] in class vector
that only differ by whether the vector is const. I wouldn't bet that you
can not overload a function on whether the argument is constant.
> It's hard to suggest a specific workaround without knowing how you use
> those iterators, because const_iterator is not necessarily convertible
> from iterator, so if you are making a wrapper class, you would have to
> have more than one member variable or something. If you could explain
> the purpose of your iterator - or even better, provide a sample - then
> we could help suggest alternative designs.
Which "const_iterator" can not be created from the according "iterator"? The
way in the opposite direction is what usually fails, but not adding
the 'const'.
Actually, concerning the OP's problem, the answer could be rather simple:
use const_iterator exclusively. Since the normal iterator of a set<> still
doesn't allow you to modify any elements, you can as well always use the
non-const one.
Uli
--
Sator Laser GmbH
Gesch�ftsf�hrer: Thorsten F�cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ulrich
|
2/5/2009 8:05:09 AM
|
|
|
3 Replies
133 Views
(page loaded in 0.078 seconds)
|
|
|
|
|
|
|
|
|