By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Explicit declaration

P: n/a
Hello,

I'm trying to port some code from Windows to Mac OSX with gcc.

Assume we have a class list which works like this:

template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
}

Now I have a derived class which extends this one:

template<class T>
class ServerList
{
void Func();
}

template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
....
}
}

Gcc doesn't accept "iterator" whereas microsoft compiler does. I'm honestly
not sure which is correct or if it's "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
So I tried to fix it by manually explicitly declaring it like this:

typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector;

Is this the correct way?

Thanks in advance.
-- John
Jul 23 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
John Smith wrote:
I'm trying to port some code from Windows to Mac OSX with gcc.

Assume we have a class list which works like this:

template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
} ;

Now I have a derived class which extends this one:

template<class T>
class ServerList
{
void Func();
} ;

Didn't you just say you _derived_ a class? Shouldn't it then be

template<class T> class ServerList : public List<T>

???

template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
...
}
}

Gcc doesn't accept "iterator" whereas microsoft compiler does. I'm honestly
not sure which is correct or if it's "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
'iterator' is a dependent name in 'ServerList<T>' (provided it *is* in
fact derived from 'List<T>'). You have to explicitly bring it into the
'ServerList<T>'s scope or fully qualify it.
So I tried to fix it by manually explicitly declaring it like this:

typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector;

Is this the correct way?


Seems fine.

A side note: do you really want such a misnomer as "List" that is actually
a "vector"? I am currently maintaining a program full of such misnomers
and it is a PITA.

V
Jul 23 '05 #2

P: n/a
John Smith wrote:
Hello,

I'm trying to port some code from Windows to Mac OSX with gcc.

Assume we have a class list which works like this:

template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
} ; need semicolon

Now I have a derived class which extends this one:

template<class T> : public List<T>
// I suspect you meant to add this
class ServerList
{
void Func();
} ; // need simicolon

template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
...
}
}

Gcc doesn't accept "iterator" whereas microsoft compiler does. I'm honestly
not sure which is correct or if it's "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
So I tried to fix it by manually explicitly declaring it like this:

typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector; vector<typename ServerList<T>::iterator> ItVector;

Is this the correct way?


yes

There is a defect report DR224 that covers this and there is a
discussion initiated by Scott Meyers on comp.std.c++ a few days ago.

Jul 23 '05 #3

P: n/a
> Didn't you just say you _derived_ a class? Shouldn't it then be

template<class T> class ServerList : public List<T>

???
Yes it should be. Sorry... I just wrote some simple pseudo code so I
wouldn't have to copy the real code which is a tad more complex to read.
A side note: do you really want such a misnomer as "List" that is actually
a "vector"? I am currently maintaining a program full of such misnomers
and it is a PITA.


No I don't want and I use better names already. Again this was just to
illustrate the problem.

Thanks for your answer.

-- John
Jul 23 '05 #4

P: n/a
> > template<class T>
: public List<T>
// I suspect you meant to add this
class ServerList
{
void Func();
} ; // need simicolon


Correct and as I wrote in the other thread it wasn't more then just pseudo
code I wrote.
There is a defect report DR224 that covers this and there is a
discussion initiated by Scott Meyers on comp.std.c++ a few days ago.


So in short, are microsoft compilers not handling the implicit statement
correctly?

-- John
Jul 23 '05 #5

P: n/a
John Smith wrote:
Hello,

I'm trying to port some code from Windows to Mac OSX with gcc.

Assume we have a class list which works like this:

template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
}

Now I have a derived class which extends this one:

template<class T>
class ServerList
{
void Func();
}

template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
...
}
}

Gcc doesn't accept "iterator" whereas microsoft compiler does. I'm honestly
not sure which is correct or if it's "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
So I tried to fix it by manually explicitly declaring it like this:

typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector;

Is this the correct way?

Thanks in advance.
-- John


Just one remark (wasn't quite obvious from your incomplete code sample):
IIRC you're not supposed to extend STL containers by using public
inheritance. It's problematic, because they don't define virtual
destructors. This includes std::list.
But you could consider using private inheritance to model "is
implemented in terms of" semantics. Or containment.

--
Regards,
Matthias
Jul 23 '05 #6

P: n/a
> Just one remark (wasn't quite obvious from your incomplete code sample):
IIRC you're not supposed to extend STL containers by using public
inheritance. It's problematic, because they don't define virtual
destructors. This includes std::list.
But you could consider using private inheritance to model "is
implemented in terms of" semantics. Or containment.


I didn't do that either but thanks for telling me to watch out in future.
In my code I use vector, list, map etc. from the base classes and just give
a public way to access them like:

MyClass::iterator it;

for (it = MyObj.begin(); it != MyObj.end(); it++)
....

instead of:

list<blah>::iterator;

for (it = MyObj.m_List.begin(); it != MyObj.m_List.end(); it++)
....

The first one looks a little easier to read.
-- John

Jul 23 '05 #7

P: n/a
Matthias wrote:
John Smith wrote: ....
Just one remark (wasn't quite obvious from your incomplete code sample):
IIRC you're not supposed to extend STL containers by using public
inheritance. It's problematic, because they don't define virtual
destructors. This includes std::list.
Hogwash.

This whole idea that classes that don't not have a virtual destructor so
you can't inherit has been discussed here many times and I believe the
consesnus is that it's silly FUD.
But you could consider using private inheritance to model "is
implemented in terms of" semantics. Or containment.

Jul 23 '05 #8

P: n/a
Gianni Mariani wrote:
This whole idea that classes that don't not have a virtual destructor so
you can't inherit has been discussed here many times and I believe the
consesnus is that it's silly FUD.


What an insightful answer. All books I have read that deal with C++ tell
another story. I am curious to hear yours.

--
Regards,
Matthias
Jul 23 '05 #9

P: n/a
Matthias wrote:
Gianni Mariani wrote:
This whole idea that classes that don't not have a virtual destructor
so you can't inherit has been discussed here many times and I believe
the consesnus is that it's silly FUD.

What an insightful answer. All books I have read that deal with C++ tell
another story. I am curious to hear yours.


I don't think Gianni should repeat all the discussions and conclusions
that can be looked up in the newsgroup archives. Please use Google to
search for them.

Also, you apparently read all the wrong books. Get better ones.
Jul 23 '05 #10

P: n/a
Victor Bazarov wrote:
Also, you apparently read all the wrong books. Get better ones.


o_O

I don't consider Effective C++ to be that bad. It's actually pretty good
IMHO. But maybe that's just a matter of taste :)

I was referring to Item 14 by the way.

There are ways to circumvent this problem, it's just that it won't work
out of the box. I think that's reason enough to tell the OP (in case he
didn't know).

--
Regards,
Matthias
Jul 23 '05 #11

P: n/a
Victor Bazarov wrote:
Please use Google to
search for them.


Hmm, I tried, but couldn't find it. Is there a website where this
newsgroup discussion is logged? Can you point me to it?

I only found some articles which basically say the same as Meyers or
even say you shouldn't inherit from STL containers at all, and an STL
guide which HAD an example of an inherited vector type, but which didn't
take that specific problem into account.

That didn't help a lot.

--
Regards,
Matthias
Jul 23 '05 #12

P: n/a
Matthias wrote:
Victor Bazarov wrote:
Please use Google to
search for them.

Hmm, I tried, but couldn't find it. Is there a website where this
newsgroup discussion is logged? Can you point me to it?


There's beta edition:

http://groups-beta.google.com/group/comp.lang.c++
[...]


V
Jul 23 '05 #13

P: n/a
Gianni Mariani wrote:
Hogwash.

This whole idea that classes that don't not have a virtual destructor so
you can't inherit has been discussed here many times and I believe the
consesnus is that it's silly FUD.


I have read several discussions now from the google-link Victor gave me,
including:
http://groups-beta.google.com/group/...95b057de217e86
http://groups-beta.google.com/group/...96c99a231c5a0d
http://groups-beta.google.com/group/...e3af1515aa8f2f
http://groups-beta.google.com/group/...65c7b3bfc9b882
http://groups-beta.google.com/group/...82b29f4078e34d

I didn't read the last one to the end (it was like 30 pages long), but I
can tell:
The vast majority of posters agreed that inheriting from STL containers
*is* problematic, for said reasons. So much about silly fud. It's maybe
a matter of preferences or style, but it's not that your point of view
to that subject would be set in stone.
I do understand your reasoning though that -- if used properly -- one
can *avoid* running into said problems. Of course you can. It's just
argumentation along the lines of "You can also avoid errors by not doing
them."

Whatever. I don't want to turn this into a flame war. Maybe it was just
your tone which irritated me :-/

--
Regards,
Matthias
Jul 23 '05 #14

P: n/a
Matthias wrote:
Gianni Mariani wrote:

Whatever. I don't want to turn this into a flame war. Maybe it was just
your tone which irritated me :-/


Can you defend your position based on facts ?

Do you mean to say that you should never inherit from a class that has
no virtual destructor ?

I see plenty of production code that works just fine where the base
class does not have a virtual destructor.

Jul 23 '05 #15

P: n/a
Gianni Mariani wrote:
Do you mean to say that you should never inherit from a class that has
no virtual destructor ?


No! Sometimes you're even supposed to. It's just that there are the
risks, and one should be aware of them.
If you intend to use your container you inherited from an STL container
just like you would do it with a normal STL container, there is a good
chance that yo will run into trouble (slicing).
Of course there are exceptions where it is perfectly fine (I think
unary_function and binary_function were mentioned several times).

However, *usually* a non-virtual destructor indicates that this class is
not supposed to be used as a base class.
The STL has exceptions, because it was developed with having in mind to
produce the least overhead possible (you don't inherit from STL classes
THAT often right?), so this is one example where this may be misleading.

--
Regards,
Matthias
Jul 23 '05 #16

P: n/a
Matthias wrote:
Gianni Mariani wrote:
Do you mean to say that you should never [...] ?


No! Sometimes [...]

However, *usually* [...]


Funny. Neither "sometimes" nor "usually" existed in your original
statement which began with "IIRC you're not supposed to extend.."

It's another case of "All generalizations are bad".
Jul 23 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.