473,513 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2053
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
1960
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
3378
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. This does not happen, but undeclare variables lead to some odd results. Error somewhere or has IIS changfed its behavior? "Testbed" Win XP Pro SP2
1
5638
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 at the bottom. Hope you can help me understand the "explicit" keyword and its usage. Specifically, Is "explicit" keyword only associated with...
1
1973
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following: ****************************************************************** template<typename T> void from_string(const char Str, T &Obj); template<> void from_string(const char Str, long &); // template<>...
6
8973
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 statement /transferkey.asp, line 2
1
2295
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. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for...
1
2132
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 occur if the charset declaration in the metatag is 8859-1 but this causes spurious characters such as capital A with a circle over it to appear before...
2
7676
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
7173
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? class Trial { public: Trial() {
3
1334
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 to either Microsoft sql or Mysql server. Infact i was suprised to see people with different programming problems. That is how i became a member of...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.