473,241 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Explicit declaration

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
16 2044
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
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
> 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
> > 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
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
> 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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Luis Solís | last post by:
Hi It is possible to declare some variables as int, long... ? , or something like visual basic option explicit. In some situations could be usefull. Thanks
7
by: Mats | last post by:
Option Explicit does not work anymore.(?) If you put <%option explicit%> at the top of your pages (direktly after the language declaration) you should get an error for each undeclared variable....
1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
6
by: John Kotuby | last post by:
Hi all, I am simply trying to include the Option Explicit declaration at the top of an ASP page and am getting an error: Error Type: Microsoft VBScript compilation (0x800A0400) Expected...
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
1
by: Jimmbo | last post by:
I get an option explicit error saying it does not appear as the first line whenever I use it with utf-8 charset encoding on my Sun Cobalt server running Chillisoft on Linux. The error does not...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
3
by: Jja | last post by:
Hello, I am very new to this site. before i start may i first introduce myself i am 'Jhalil, a beginner Vb6 programmer. i stumbled on this site on my quest for a code in vb that can connect me...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.