Explicit declaration

J

John Smith

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
 
V

Victor Bazarov

John said:
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 said:
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
 
G

Gianni Mariani

John said:
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>
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 said:
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.
 
J

John Smith

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
 
J

John Smith

template said:
; // 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
 
M

Matthias

John said:
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.
 
J

John Smith

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
 
G

Gianni Mariani

Matthias said:
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.
 
M

Matthias

Gianni said:
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.
 
V

Victor Bazarov

Matthias said:
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.
 
M

Matthias

Victor said:
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).
 
M

Matthias

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.
 
M

Matthias

Gianni said:
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..._doneTitle=Back+to+Search&&d#9f95b057de217e86
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#3b96c99a231c5a0d
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#00e3af1515aa8f2f
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#2e65c7b3bfc9b882
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#ca82b29f4078e34d

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 :-/
 
G

Gianni Mariani

Matthias said:
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.
 
M

Matthias

Gianni said:
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.
 
V

Victor Bazarov

Matthias said:
Gianni said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top