using declarations with nested typedefs

Discussion in 'C++' started by Dave, Dec 5, 2003.

  1. Dave

    Dave Guest

    Is the using declaration shown in the program below valid? My compiler
    accepts it, but I'm not sure if it should...

    class foo_t
    {
    public:
    typedef int X;
    };

    int main()
    {
    using foo_t::x;
    X a;
    }
     
    Dave, Dec 5, 2003
    #1
    1. Advertising

  2. Dave

    Dave Guest

    "Dave" <> wrote in message
    news:...
    > Is the using declaration shown in the program below valid? My compiler
    > accepts it, but I'm not sure if it should...
    >
    > class foo_t
    > {
    > public:
    > typedef int X;
    > };
    >
    > int main()
    > {
    > using foo_t::x;
    > X a;
    > }
    >
    >

    Sorry, it should've been: using foo_t::X;

    I typed in the example by hand...
     
    Dave, Dec 5, 2003
    #2
    1. Advertising

  3. "Dave" <> wrote...
    >
    > "Dave" <> wrote in message
    > news:...
    > > Is the using declaration shown in the program below valid? My compiler
    > > accepts it, but I'm not sure if it should...
    > >
    > > class foo_t
    > > {
    > > public:
    > > typedef int X;
    > > };
    > >
    > > int main()
    > > {
    > > using foo_t::x;
    > > X a;
    > > }
    > >
    > >

    > Sorry, it should've been: using foo_t::X;


    Answering your original question, yes, it's valid. 'X' is a synonym
    for 'int' in 'foo_t' declarative region. You may bring that name
    into any other declarative region, where foo_t::X is accessible.

    I guess since your compiler accepts it, you don't have a problem,
    but may I ask, what prompted your question in the first place?

    Victor
     
    Victor Bazarov, Dec 5, 2003
    #3
  4. Dave

    Dave Guest

    "Victor Bazarov" <> wrote in message
    news:678Ab.33903$_M.147470@attbi_s54...
    > "Dave" <> wrote...
    > >
    > > "Dave" <> wrote in message
    > > news:...
    > > > Is the using declaration shown in the program below valid? My

    compiler
    > > > accepts it, but I'm not sure if it should...
    > > >
    > > > class foo_t
    > > > {
    > > > public:
    > > > typedef int X;
    > > > };
    > > >
    > > > int main()
    > > > {
    > > > using foo_t::x;
    > > > X a;
    > > > }
    > > >
    > > >

    > > Sorry, it should've been: using foo_t::X;

    >
    > Answering your original question, yes, it's valid. 'X' is a synonym
    > for 'int' in 'foo_t' declarative region. You may bring that name
    > into any other declarative region, where foo_t::X is accessible.
    >
    > I guess since your compiler accepts it, you don't have a problem,
    > but may I ask, what prompted your question in the first place?
    >
    > Victor


    Well, my compiler (VC++ 7.1) accepts it, but Comeau (the online test-drive
    version) does not...

    Comeau (as well as my compiler) will, however, accept this:

    typedef foo_t::X X;

    You know, looking at the C++ grammar as defined in the Standard, it appears
    that, syntactically, a using declaration may indeed be used to access a
    class scope. Of course, this jives with your statement that it is valid.
    Unless there's a semantic reason this is invalid that we've both missed,
    perhaps a minor bug has just been found in Comeau??????

    I'd love to hear your further thoughts as well as any other thoughts anyone
    has...
     
    Dave, Dec 5, 2003
    #4
  5. Dave wrote:
    > Is the using declaration shown in the program below valid? My compiler
    > accepts it, but I'm not sure if it should...
    >
    > class foo_t
    > {
    > public:
    > typedef int X;
    > };
    >
    > int main()
    > {
    > using foo_t::x;
    > X a;
    > }


    No, it is not valid. 7.3.3/6 states that a using declaration for a class
    member shall be a member declaration. Nested 'typedef's are class
    members, according to 9.2/1.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 5, 2003
    #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. advantage of using typedefs

    , Jan 28, 2006, in forum: C Programming
    Replies:
    30
    Views:
    1,581
    Keith Thompson
    Feb 1, 2006
  2. pyramus
    Replies:
    3
    Views:
    510
    Victor Bazarov
    Nov 29, 2005
  3. Noah Roberts
    Replies:
    7
    Views:
    682
    Bo Persson
    May 3, 2006
  4. Chris Fairles
    Replies:
    2
    Views:
    343
    Gennaro Prota
    Jun 26, 2007
  5. Nested declarations

    , Sep 15, 2007, in forum: C Programming
    Replies:
    1
    Views:
    276
    Army1987
    Sep 15, 2007
Loading...

Share This Page