Meaning of template construction

Discussion in 'C++' started by =?ISO-8859-1?Q?Xavier_D=E9coret?=, Jul 9, 2004.

  1. I am trying to understand the meaning of this construction, found in the
    CGAL library:

    typedef typename Vb::template Rebind_TDS<TDS2>::Other Vb2;

    Can anyone explain?


    PS: I tried to reproduce this construction to see if it was what I
    understood. Below is the code. Unfortunately, it crashes the compiler
    (gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)).
     
    =?ISO-8859-1?Q?Xavier_D=E9coret?=, Jul 9, 2004
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Xavier_D=E9coret?=

    Sharad Kala Guest

    "Xavier Décoret" <> wrote in message
    news:cclmpu$ghp$...
    > I am trying to understand the meaning of this construction, found in the
    > CGAL library:
    >
    > typedef typename Vb::template Rebind_TDS<TDS2>::Other Vb2;


    Let me give it a try.
    - Vb is a class template.
    - Vb has a nested class template Rebind_TDS
    - Rebind_TDS contains a type Other.

    So the above statement means that Vb2 is a type alias for the Other type in
    Rebind_TDS, which is instantiated with TDS2 in the Vb class template.

    -Sharad
     
    Sharad Kala, Jul 9, 2004
    #2
    1. Advertising

  3. "Xavier Décoret" <> wrote in message
    news:cclmpu$ghp$...
    > I am trying to understand the meaning of this construction, found in the
    > CGAL library:
    >
    > typedef typename Vb::template Rebind_TDS<TDS2>::Other Vb2;
    >
    > Can anyone explain?
    >


    Take out your favourite C++ book and look at the chapter on allocators. The
    std::allocator class uses a rebind/other mechanism exactly like the one you
    are looking at.

    The other point to make it that typename and template are just keywords to
    help the compiler disambiguate the code. Try writing it like this

    typedef Vb::Rebind_TDS<TDS2>::Other Vb2;

    That is not legal C++, but it might make it easier to understand.

    >
    > PS: I tried to reproduce this construction to see if it was what I
    > understood. Below is the code. Unfortunately, it crashes the compiler
    > (gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)).


    Where is the code?

    john
     
    John Harrison, Jul 9, 2004
    #3
  4. John Harrison wrote:

    > Where is the code?
    >
    > john
    >
    >

    Thanks for the explanation. This is what I was suspected. Here is the
    code (forgot it in my post).


    class Object
    {
    class Drawer
    {
    public:
    template <int dim>
    class Projector
    {
    class Basis
    {
    float e[dim][3];
    };
    };
    };
    };


    typedef typename Object::template Drawer<2>::Basis Vb2;

    int
    main()
    {
    }
     
    =?ISO-8859-1?Q?Xavier_D=E9coret?=, Jul 9, 2004
    #4
  5. "Xavier Décoret" <> wrote in message
    news:ccltas$nng$...
    > John Harrison wrote:
    >
    > > Where is the code?
    > >
    > > john
    > >
    > >

    > Thanks for the explanation. This is what I was suspected. Here is the
    > code (forgot it in my post).
    >
    >
    > class Object
    > {
    > class Drawer
    > {
    > public:
    > template <int dim>
    > class Projector
    > {
    > class Basis
    > {
    > float e[dim][3];
    > };
    > };
    > };
    > };
    >
    >
    > typedef typename Object::template Drawer<2>::Basis Vb2;
    >
    > int
    > main()
    > {
    > }


    There a few things wrong with that code.

    1) It's Drawer that need to be the template
    2) Not sure what Projector is doing
    3) Drawer is private, should be public
    4) You don't need to say typename because you aren't defining Vb2 inside a
    template. The ambiguity that typename resolves only applies when compiling
    templates.
    5) I'm less sure of this but I think you can drop the template keyword as
    well for similar reasons.

    This compiles on gcc 3.3.1

    class Object
    {
    public:
    template <int dim>
    class Drawer
    {
    public:
    class Basis
    {
    float e[dim][3];
    };
    };
    };


    typedef Object::Drawer<2>::Basis Vb2;

    int main()
    {
    Vb2 x;
    }
     
    John Harrison, Jul 9, 2004
    #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. Chris Theis
    Replies:
    2
    Views:
    503
    Chris Theis
    Jul 24, 2003
  2. Replies:
    5
    Views:
    389
    Nathan Addy
    Sep 17, 2005
  3. Ook
    Replies:
    10
    Views:
    581
  4. Replies:
    4
    Views:
    818
  5. Angus
    Replies:
    8
    Views:
    466
    Francesco S. Carta
    Sep 2, 2010
Loading...

Share This Page