about struct keyword

Discussion in 'C++' started by lp-boy@yandex.ru, Dec 7, 2005.

  1. Guest

    Hello!

    Is the following code legal?

    template<class T>
    struct holder{};

    struct A
    {
    holder<struct B*> h; //!!!
    //but holder<B*> h; error: B was not declared
    };

    int main()
    {
    A a;
    }

    If yes, why? Thanks in advance.
     
    , Dec 7, 2005
    #1
    1. Advertising

  2. writes:

    > Hello!
    >
    > Is the following code legal?
    >
    > template<class T>
    > struct holder{};
    >
    > struct A
    > {
    > holder<struct B*> h; //!!!
    > //but holder<B*> h; error: B was not declared
    > };
    >
    > int main()
    > {
    > A a;
    > }
    >
    > If yes, why? Thanks in advance.


    In order to use a type, its size must be known. In case of pointers a
    forward declaration is all that is needed, since pointer size does not
    depend on the internals of the type. All pointers to user defined types
    have the same size.

    In your code you say holder<struct B*> h;
    The struct word makes this a forward declaration for B at the latest
    possible moment.
    When you just say holder<B*> B isn't declared yet, so it can't be used.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 7, 2005
    #2
    1. Advertising

  3. Marcus Kwok Guest

    wrote:
    > Hello!
    >
    > Is the following code legal?
    >
    > template<class T>
    > struct holder{};
    >
    > struct A
    > {
    > holder<struct B*> h; //!!!
    > //but holder<B*> h; error: B was not declared
    > };
    >
    > int main()
    > {
    > A a;
    > }
    >
    > If yes, why? Thanks in advance.


    The way I interpret it, the line

    holder<struct B*> h;

    is equivalent to forward-declaring B as a struct.


    template <class T>
    struct holder { };

    struct B;

    struct A {
    holder<B*> h;
    };

    int main()
    {
    A a;
    }


    works for me (VS .NET 2003).

    --
    Marcus Kwok
     
    Marcus Kwok, Dec 7, 2005
    #3
  4. Greg Guest

    Marcus Kwok wrote:
    > wrote:
    > > Hello!
    > >
    > > Is the following code legal?
    > >
    > > template<class T>
    > > struct holder{};
    > >
    > > struct A
    > > {
    > > holder<struct B*> h; //!!!
    > > //but holder<B*> h; error: B was not declared
    > > };
    > >
    > > int main()
    > > {
    > > A a;
    > > }
    > >
    > > If yes, why? Thanks in advance.

    >
    > The way I interpret it, the line
    >
    > holder<struct B*> h;
    >
    > is equivalent to forward-declaring B as a struct.
    >
    >
    > template <class T>
    > struct holder { };
    >
    > struct B;
    >
    > struct A {
    > holder<B*> h;
    > };
    >
    > int main()
    > {
    > A a;
    > }


    The two programs are similar but not the same. The struct B declaration
    appears first within struct "A" therefore it is a forward declaration
    for an inner struct of A, named B (or ::A::B fully qualified). Of
    course there are no inner structs defined in struct A, let alone one
    named B.

    But no matter, since the holder class template is being instantiated
    with a pointer to B, the fact that there is no such type is not an
    error.

    Greg
     
    Greg, Dec 7, 2005
    #4
  5. Marcus Kwok Guest

    Greg <> wrote:
    > Marcus Kwok wrote:
    >> wrote:
    >> > Hello!
    >> >
    >> > Is the following code legal?
    >> >
    >> > template<class T>
    >> > struct holder{};
    >> >
    >> > struct A
    >> > {
    >> > holder<struct B*> h; //!!!
    >> > //but holder<B*> h; error: B was not declared
    >> > };
    >> >
    >> > int main()
    >> > {
    >> > A a;
    >> > }
    >> >
    >> > If yes, why? Thanks in advance.

    >>
    >> The way I interpret it, the line
    >>
    >> holder<struct B*> h;
    >>
    >> is equivalent to forward-declaring B as a struct.
    >>
    >>
    >> template <class T>
    >> struct holder { };
    >>
    >> struct B;
    >>
    >> struct A {
    >> holder<B*> h;
    >> };
    >>
    >> int main()
    >> {
    >> A a;
    >> }

    >
    > The two programs are similar but not the same. The struct B declaration
    > appears first within struct "A" therefore it is a forward declaration
    > for an inner struct of A, named B (or ::A::B fully qualified). Of
    > course there are no inner structs defined in struct A, let alone one
    > named B.


    OK, then what I really meant <g> was:

    template <class T>
    struct holder { };

    struct A {
    struct B;
    holder<B*> h;
    };

    int main()
    {
    A a;
    }

    --
    Marcus Kwok
     
    Marcus Kwok, Dec 7, 2005
    #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 Fogelklou
    Replies:
    36
    Views:
    1,439
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    6
    Views:
    486
    Peter Otten
    May 10, 2007
  3. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    376
  4. Rolf Krüger
    Replies:
    2
    Views:
    640
    Ian Collins
    Mar 12, 2008
  5. Giles Bowkett
    Replies:
    9
    Views:
    151
    Robert Klemme
    Jan 10, 2008
Loading...

Share This Page