473,473 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

qualified name VS unqualified name in class template.

Hey, guys.

There is a problem.
e.g.

template <typename T>
class foo
{
public:
foo();
~foo();
private:
static size_t bar;
};

template <typename T>
size_t foo<T>::bar = 0;

template <typename T>
foo<T>::foo()
{
++bar; // (1)
}

template <typename T>
foo<T>::~foo()
{
--bar; // (2)
}

Note the place (1), (2), it can be replaced with
++foo<T>::bar;
--foo<T>::bar;

The qualified name and unqualified name are all OK,
and most c++ book use the qualified name preferable.
I know that it is something related with the name lookup.
but I don't know the details.

Does it have somebody explain it for me??
thanks.
Regards.

Mar 10 '07 #1
4 2414
* Wayne Shu:
>
There is a problem.
Is there?

e.g.

template <typename T>
class foo
{
public:
foo();
~foo();
private:
static size_t bar;
};

template <typename T>
size_t foo<T>::bar = 0;

template <typename T>
foo<T>::foo()
{
++bar; // (1)
}

template <typename T>
foo<T>::~foo()
{
--bar; // (2)
}

Note the place (1), (2), it can be replaced with
++foo<T>::bar;
--foo<T>::bar;

The qualified name and unqualified name are all OK,
and most c++ book use the qualified name preferable.
I know that it is something related with the name lookup.
but I don't know the details.

Does it have somebody explain it for me??
It may have been just a basic instinct or style on the part of the
authors, it may be that you've misunderstood the context, that the
books' examples haven't been exactly equivalent to the code you present.
You don't say which books or which examples that befuddle you.
Without that information further discussion would be just as much pure
speculation as this short reply. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 10 '07 #2
On 3ÔÂ10ÈÕ, ÏÂÎç9ʱ49·Ö, "Alf P. Steinbach" <a...@start.nowrote:
* Wayne Shu:
There is a problem.

Is there?


e.g.
template <typename T>
class foo
{
public:
foo();
~foo();
private:
static size_t bar;
};
template <typename T>
size_t foo<T>::bar = 0;
template <typename T>
foo<T>::foo()
{
++bar; // (1)
}
template <typename T>
foo<T>::~foo()
{
--bar; // (2)
}
Note the place (1), (2), it can be replaced with
++foo<T>::bar;
--foo<T>::bar;
The qualified name and unqualified name are all OK,
and most c++ book use the qualified name preferable.
I know that it is something related with the name lookup.
but I don't know the details.
Does it have somebody explain it for me??

It may have been just a basic instinct or style on the part of the
authors, it may be that you've misunderstood the context, that the
books' examples haven't been exactly equivalent to the code you present.
The book is "C++ Template The Complete Guide",
and the example is the section 16.3 "The Curiously Recurring Template
Pattern"
The complete code is

// inherit/objectcounter.hpp
template <typename CountedType>
class ObjectCounter {
private:
static size_t count; // number of existing objects

protected:
// default constructor
ObjectCounter() {
++ObjectCounter<CountedType>::count;
}

// copy constructor
ObjectCounter (ObjectCounter<CountedTypeconst&) {
++ObjectCounter<CountedType>::count;
}

// destructor
~ObjectCounter() {
--ObjectCounter<CountedType>::count;
}

public:
// return number of existing objects:
static size_t live() {
return ObjectCounter<CountedType>::count;
}
};

// initialize counter with zero
template <typename CountedType>
size_t ObjectCounter<CountedType>::count = 0;

// inherit/testcounter.cpp
#include "objectcounter.hpp"
#include <iostream>

template <typename CharT>
class MyString : public ObjectCounter<MyString<CharT {
//...
};

int main()
{
MyString<chars1, s2;
MyString<wchar_tws;

std::cout << "number of MyString<char>: "
<< MyString<char>::live() << std::endl;
std::cout << "number of MyString<wchar_t>: "
<< ws.live() << std::endl;
}

You don't say which books or which examples that befuddle you.
Without that information further discussion would be just as much pure
speculation as this short reply. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

Mar 10 '07 #3
* Wayne Shu:
>
The book is "C++ Template The Complete Guide",
and the example is the section 16.3 "The Curiously Recurring Template
Pattern"
The complete code is

// inherit/objectcounter.hpp
template <typename CountedType>
class ObjectCounter {
private:
static size_t count; // number of existing objects

protected:
// default constructor
ObjectCounter() {
++ObjectCounter<CountedType>::count;
}

// copy constructor
ObjectCounter (ObjectCounter<CountedTypeconst&) {
++ObjectCounter<CountedType>::count;
}

// destructor
~ObjectCounter() {
--ObjectCounter<CountedType>::count;
}

public:
// return number of existing objects:
static size_t live() {
return ObjectCounter<CountedType>::count;
}
};
[snip]

Well, David Vandevoorde & Nicolai Josuttis are very compentent folks,
but I see no technical need for the qualification in this case.

If you post this to [comp.lang.c++.moderated] perhaps Daveed will
explain the reason(s).

I'd guess it's just a coding convention to avoid the problems that
unqualified names in template code can cause in other contexts, or to be
very explicit (very clear what 'count' means), which is generally good,
in the same vein that "unnecessary" parentheses may be used for clarity.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 10 '07 #4
On Mar 10, 11:21 am, "Alf P. Steinbach" <a...@start.nowrote:
* Wayne Shu:


The book is "C++ Template The Complete Guide",
and the example is the section 16.3 "The Curiously Recurring Template
Pattern"
The complete code is
// inherit/objectcounter.hpp
template <typename CountedType>
class ObjectCounter {
private:
static size_t count; // number of existing objects
protected:
// default constructor
ObjectCounter() {
++ObjectCounter<CountedType>::count;
}
// copy constructor
ObjectCounter (ObjectCounter<CountedTypeconst&) {
++ObjectCounter<CountedType>::count;
}
// destructor
~ObjectCounter() {
--ObjectCounter<CountedType>::count;
}
public:
// return number of existing objects:
static size_t live() {
return ObjectCounter<CountedType>::count;
}
};

[snip]

Well, DavidVandevoorde& Nicolai Josuttis are very compentent folks,
but I see no technical need for the qualification in this case.

If you post this to [comp.lang.c++.moderated] perhapsDaveedwill
explain the reason(s).

(I can reply here too ;-)

I'd guess it's just a coding convention to avoid the problems that
unqualified names in template code can cause in other contexts, or to be
very explicit (very clear what 'count' means), which is generally good,
in the same vein that "unnecessary" parentheses may be used for clarity.

Yes, it was just written that way to emphasize the fact that we're
referring to something stored in the base class itself (the class
template is later used as a base class instantiated over the derived
class, and the aim was to help the reader not look for "count" in
places that are too far away). A note in the text would have been a
good idea.

Also, as I think you suggest, in templates it's often a good idea to
make your names as explicit as reasonable to avoid changes in binding
caused by harmless-looking changes. In this particular instance, that
may not be a strong argument though.

Daveed

Mar 12 '07 #5

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

Similar topics

0
by: C. M. Sperberg-McQueen | last post by:
wooks (wookiz@hotmail.com) wrote: > <?xml version='1.0'?> > <userlogin xmlns="urn:faster:userlogin" > xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> > <login>mick</login> > ...
4
by: pervinder | last post by:
Hi, I get an error on line #3 while compiling the program segment on aix (xlc - va6) While this warning is not seen with gcc "test.cpp" line 200.18: 1540-0152 (W) A template dependent name that...
4
by: Jim Garrison | last post by:
I know how to use the name() function to access the name of the current node. How do I get the 'fully qualified' name, consisting of the path from the root to the current node? I.e. <a> <b>...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
1
by: Jeff Molby | last post by:
Sorry for the crossposting guys. I figured this one might be a little tricky, so I'm calling upon the C# brains out there to help out this poor VB developer. I know enough C# to translate any code...
5
by: CindyRob | last post by:
Using .NET framework 1.1 SP1, .NET framework SDK 1.1 SP1, Visual Studio .NET 2003, hotfixes 892202 and 823639. I create a proxy class using wsdl.exe, and in the serialized XML request, I see...
0
by: Jim Avera | last post by:
On AIX, a template class containing a nested class which needs to be a friend provokes a warning: (W) A template dependent name that is a type must be qualified with "typename". and method...
10
by: famat | last post by:
Hi all, I have C++ source code that compiles on windows-MS VS2005 without any problem. I am trying to compile it in Linux gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) and I ran into the following...
3
by: axel_johard | last post by:
Hi, need some help with creating a WSDL file. I will try to summarize the problem like this: I need to create a wsdl that accepts a response that has no namespace- prefix in the first line in...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.