473,672 Members | 2,693 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>::iter ator 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::ite rator (from List).
So I tried to fix it by manually explicitly declaring it like this:

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

Is this the correct way?

Thanks in advance.
-- John
Jul 23 '05 #1
16 2069
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>::iter ator 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::ite rator (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<ServerLi st<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>::iter ator 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::ite rator (from List).
So I tried to fix it by manually explicitly declaring it like this:

typename ServerList<T>:: iterator it;
vector<ServerLi st<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>::iter ator 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::ite rator (from List).
So I tried to fix it by manually explicitly declaring it like this:

typename ServerList<T>:: iterator it;
vector<ServerLi st<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::iterat or it;

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

instead of:

list<blah>::ite rator;

for (it = MyObj.m_List.be gin(); it != MyObj.m_List.en d(); 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1966
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
3386
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
5649
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 constructor in C++? What's "implied conversion of types"?
1
1982
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<> void from_string(const char Str, unsigned lon &); //
6
9000
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
2313
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 call to 'D<int>::D(D<int>&)'
1
2139
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 the "£" symbol when newsfeeds are paced on the 8859-1 page. Not very professional! These was a...
2
7691
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
7197
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
1337
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 this good family. Now can anybody help? i am a beginner in VB 6, i enjoy writing codes but i do...
0
8504
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8643
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7476
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4242
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2094
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1839
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.