Never seen this usage before

Discussion in 'C++' started by l h, Feb 8, 2010.

  1. l h

    l h Guest

    I'm looking over some C++ code and I came across a class definition
    that I don't understand the purpose of. If someone could tell me what
    kind of construct this is and its purpose, I'd appreciate it.

    class A
    {
    public:
    typedef A Adesc;
    typedef B::C<Adesc> B1;
    typedef B::D<Adesc> B2;
    };

    What is the intent of this class of typedefs and what is it used for?

    Thanks.

    Les
     
    l h, Feb 8, 2010
    #1
    1. Advertising

  2. l h

    Ian Collins Guest

    l h wrote:
    > I'm looking over some C++ code and I came across a class definition
    > that I don't understand the purpose of. If someone could tell me what
    > kind of construct this is and its purpose, I'd appreciate it.
    >
    > class A
    > {
    > public:
    > typedef A Adesc;
    > typedef B::C<Adesc> B1;
    > typedef B::D<Adesc> B2;
    > };
    >
    > What is the intent of this class of typedefs and what is it used for?


    They are shorthand. If you uses a template type often, typing the full
    type becomes tedious.

    --
    Ian Collins
     
    Ian Collins, Feb 9, 2010
    #2
    1. Advertising

  3. l h

    tonydee Guest

    On Feb 9, 8:44 am, l h <> wrote:
    > class A
    > {
    >   public:
    >     typedef A Adesc;
    >     typedef B::C<Adesc> B1;
    >     typedef B::D<Adesc> B2;
    >
    > };
    >
    > What is the intent of this class of typedefs and what is it used for?


    Typedefs provide alternative names, sometimes for convenience,
    sometimes to hide lower level implementation choices. Here, A wants
    to define types Adesc, B1 and B2 so people can use them as "A::Adesc,
    A::B1, A::B2, though they actually refer to A, B::C<A> and B::D<A>
    respectively. Clearly, C and D are templates in a scope (namespace or
    class/struct) "B". Such code is common, for instance - classes often
    typedef instantiations of STL containers (e.g. "std::vector<int>
    Container"), making it both easier to refer to the Container type and
    its iterators and easier to change to say a list later without needing
    so many changes elsewhere in the class and client code.

    Cheers,
    Tony
     
    tonydee, Feb 9, 2010
    #3
  4. l h

    l h Guest

    On Feb 8, 5:40 pm, tonydee <> wrote:
    > On Feb 9, 8:44 am, l h <> wrote:
    >
    > > class A
    > > {
    > >   public:
    > >     typedef A Adesc;
    > >     typedef B::C<Adesc> B1;
    > >     typedef B::D<Adesc> B2;

    >
    > > };

    >
    > > What is the intent of this class of typedefs and what is it used for?

    >
    > Typedefs provide alternative names, sometimes for convenience,
    > sometimes to hide lower level implementation choices.  Here, A wants
    > to define types Adesc, B1 and B2 so people can use them as "A::Adesc,
    > A::B1, A::B2, though they actually refer to A, B::C<A> and B::D<A>
    > respectively.  Clearly, C and D are templates in a scope (namespace or
    > class/struct) "B".  Such code is common, for instance - classes often
    > typedef instantiations of STL containers (e.g. "std::vector<int>
    > Container"), making it both easier to refer to the Container type and
    > its iterators and easier to change to say a list later without needing
    > so many changes elsewhere in the class and client code.
    >
    > Cheers,
    > Tony


    I understand the basics of typedefs. I've just never seen them nested
    within a class before, particularly since the class is just a
    collection of typedefs. So you're saying that this is using the outer
    class as a namespace for the typedefs?

    Les
     
    l h, Feb 9, 2010
    #4
  5. l h wrote:
    > I'm looking over some C++ code and I came across a class definition
    > that I don't understand the purpose of. If someone could tell me what
    > kind of construct this is and its purpose, I'd appreciate it.
    >
    > class A
    > {
    > public:
    > typedef A Adesc;
    > typedef B::C<Adesc> B1;
    > typedef B::D<Adesc> B2;
    > };
    >
    > What is the intent of this class of typedefs and what is it used for?


    To know exactly what *this* class is for, you need to ask the person who
    *wrote* the code. However, we could try *guessing* the purpose. Often
    in generic programming types are used to express some relationships and
    to control generation of code (like conditional expressions in
    run-time). Often the types need to conform to a certain specification,
    like in the standard library every standard container has 'data_type'
    defined and so one. It means that some generic algorithm is looking for
    'T::data_type', which will resolve to something *iff* the 'T' class has
    the proper typedef for 'data_type'. Want more? Get a decent book on
    template programming, like the book by Vandevoorde and Josuttis.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 9, 2010
    #5
  6. l h

    tonydee Guest

    On Feb 9, 10:44 am, l h <> wrote:
    > I understand the basics of typedefs. I've just never seen them nested
    > within a class before, particularly since the class is just a
    > collection of typedefs. So you're saying that this is using the outer
    > class as a namespace for the typedefs?


    Some people use classes as a scoping mechanism, preferring them over
    namespaces. Professor John Lakos (author of Large Scale C++ Design),
    encouraged this in the past (and may still), on the grounds that class
    scope can't be reopened - if you've found the class/struct, then you
    know exactly what's inside it, whereas namespaces can be put together
    from many pieces - making it harder to get an accurate idea of their
    content. On the other hand, this very ability means namespaces can
    unify a logical interface across the bounds of multiple "physical"
    files, which IMHO is more important. Another aspect is that classes
    force the use of the scope (i.e. A::B1 must be used), whereas
    namespaces can be "used" so "B1" is sufficient. Again, opinions vary
    as to whether this is good or bad. Anyway, don't be thrown by the
    class wrapping the typedefs... it's just being used like a namespace
    as you say....

    Cheers,
    Tony
     
    tonydee, Feb 9, 2010
    #6
  7. l h

    James Kanze Guest

    On 9 Feb, 02:33, tonydee <> wrote:
    > On Feb 9, 10:44 am, l h <> wrote:


    > > I understand the basics of typedefs. I've just never seen
    > > them nested within a class before, particularly since the
    > > class is just a collection of typedefs. So you're saying
    > > that this is using the outer class as a namespace for the
    > > typedefs?


    > Some people use classes as a scoping mechanism, preferring
    > them over namespaces. Professor John Lakos (author of Large
    > Scale C++ Design), encouraged this in the past (and may
    > still), on the grounds that class scope can't be reopened - if
    > you've found the class/struct, then you know exactly what's
    > inside it, whereas namespaces can be put together from many
    > pieces - making it harder to get an accurate idea of their
    > content. On the other hand, this very ability means
    > namespaces can unify a logical interface across the bounds of
    > multiple "physical" files, which IMHO is more important.
    > Another aspect is that classes force the use of the scope
    > (i.e. A::B1 must be used), whereas namespaces can be "used" so
    > "B1" is sufficient. Again, opinions vary as to whether this
    > is good or bad. Anyway, don't be thrown by the class wrapping
    > the typedefs... it's just being used like a namespace as you
    > say....


    I don't think that's a very frequent use today. The most common
    use is probably as a sort of traits class, as Victor described,
    configuring some template class. Another possibility would be
    to use it as a base class, so that the derived class would get
    all of the typedef's, immediately. This is often done when the
    derived class needs a lot of typedef's; e.g. when it's a
    container, or an iterator, or such.

    --
    James Kanze
     
    James Kanze, Feb 9, 2010
    #7
    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. Rob G
    Replies:
    2
    Views:
    2,274
    Rob G
    May 24, 2004
  2. David
    Replies:
    2
    Views:
    917
    David
    Jul 6, 2004
  3. Kev
    Replies:
    0
    Views:
    422
  4. Sharron

    Code I've never seen before

    Sharron, Aug 18, 2003, in forum: C++
    Replies:
    1
    Views:
    327
    Mike Wahler
    Aug 18, 2003
  5. blangela

    Never seen this before ...

    blangela, Apr 15, 2008, in forum: C++
    Replies:
    30
    Views:
    923
    Nick Keighley
    Apr 17, 2008
Loading...

Share This Page