qualified name VS unqualified name in class template.

Discussion in 'C++' started by Wayne Shu, Mar 10, 2007.

  1. Wayne Shu

    Wayne Shu Guest

    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.
    Wayne Shu, Mar 10, 2007
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Mar 10, 2007
    #2
    1. Advertising

  3. Wayne Shu

    Wayne Shu Guest

    On 3ÔÂ10ÈÕ, ÏÂÎç9ʱ49·Ö, "Alf P. Steinbach" <> wrote:
    > * 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<CountedType> const&) {
    ++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<char> s1, s2;
    MyString<wchar_t> ws;

    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?- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
    Wayne Shu, Mar 10, 2007
    #3
  4. * 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<CountedType> const&) {
    > ++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?
    Alf P. Steinbach, Mar 10, 2007
    #4
  5. Wayne Shu

    Guest

    On Mar 10, 11:21 am, "Alf P. Steinbach" <> wrote:
    > * 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<CountedType> const&) {
    > > ++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, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C. M. Sperberg-McQueen
    Replies:
    0
    Views:
    797
    C. M. Sperberg-McQueen
    Jul 31, 2003
  2. qualified and unqualified types

    , Jul 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    319
    Suman
    Jul 8, 2005
  3. George2

    qualified name and unqualified name

    George2, Mar 7, 2008, in forum: C Programming
    Replies:
    0
    Views:
    380
    George2
    Mar 7, 2008
  4. George2

    unqualified name in template

    George2, Mar 9, 2008, in forum: C Programming
    Replies:
    0
    Views:
    285
    George2
    Mar 9, 2008
  5. Szabolcs Borsanyi

    Pointer to qualified poitner to qualified object

    Szabolcs Borsanyi, May 30, 2008, in forum: C Programming
    Replies:
    13
    Views:
    553
    Keith Thompson
    Jun 8, 2008
Loading...

Share This Page