using keyword in C++

Discussion in 'C++' started by MJ_India, Mar 10, 2011.

  1. MJ_India

    MJ_India Guest

    struct foo { static const int bar = 0; };
    int baz() {
    using foo::bar; // [1]
    return bar; // [2]
    }

    [1] is invalid in standard C++ because standard C++ allows _using_
    keyword only in 3 ways (For namespace, a member of namespace or in
    derived classes to avoid shadowing or to increase privateness of
    inherited member)

    To make statement [2] work without fully qualified name I need some
    workaround(a reference or define).

    My question is, what is the advantage of not allowing [1] as valid C++
    grammar?
    I sometimes think allowing [1] may be useful in some scenarios and
    cannot think of a reason why it was decided incorrect. Only advantage
    I see here is, it makes compiler implementation little easy (Does it
    really? may be.)
    But I don't think this is one of the possible answer for my question,
    considering C++ complexities. (generic programming etc)
    MJ_India, Mar 10, 2011
    #1
    1. Advertising

  2. On 3/10/2011 8:00 AM, MJ_India wrote:
    > struct foo { static const int bar = 0; };
    > int baz() {
    > using foo::bar; // [1]
    > return bar; // [2]
    > }
    >
    > [1] is invalid in standard C++ because standard C++ allows _using_
    > keyword only in 3 ways (For namespace, a member of namespace or in
    > derived classes to avoid shadowing or to increase privateness of
    > inherited member)
    >
    > To make statement [2] work without fully qualified name I need some
    > workaround(a reference or define).


    I would like to see a reason to prefer

    <some trick to bring foo::bar into scope>
    return bar;

    over

    return foo::bar;

    .. Please provide it.

    > My question is, what is the advantage of not allowing [1] as valid C++
    > grammar?


    The advantage is simplification of the language.

    > I sometimes think allowing [1] may be useful in some scenarios


    Name at least one, please.

    > and
    > cannot think of a reason why it was decided incorrect. Only advantage
    > I see here is, it makes compiler implementation little easy (Does it
    > really? may be.)
    > But I don't think this is one of the possible answer for my question,
    > considering C++ complexities. (generic programming etc)


    Huh?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 10, 2011
    #2
    1. Advertising

  3. MJ_India

    Joe Greer Guest

    MJ_India <> wrote in news:2a8c7f36-0cc3-4d35-
    :

    > struct foo { static const int bar = 0; };
    > int baz() {
    > using foo::bar; // [1]
    > return bar; // [2]
    > }
    >
    > [1] is invalid in standard C++ because standard C++ allows _using_
    > keyword only in 3 ways (For namespace, a member of namespace or in
    > derived classes to avoid shadowing or to increase privateness of
    > inherited member)
    >
    > To make statement [2] work without fully qualified name I need some
    > workaround(a reference or define).
    >
    > My question is, what is the advantage of not allowing [1] as valid C++
    > grammar?
    > I sometimes think allowing [1] may be useful in some scenarios and
    > cannot think of a reason why it was decided incorrect. Only advantage
    > I see here is, it makes compiler implementation little easy (Does it
    > really? may be.)
    > But I don't think this is one of the possible answer for my question,
    > considering C++ complexities. (generic programming etc)
    >


    I can't give any sort of official answer, but it seems to me that items
    that are declared in a struct/class should have a special association
    with that struct/class and shouldn't be manipulated outside of it without
    going through some pains. If you just want a global, you should put the
    variable in a namespace and then the using would work as you desire.
    That is, there is no reason for the above example that it couldn't be
    written as:

    namespace foo {
    const int bar = 0;
    }
    int baz() {
    using foo::bar;
    return bar;
    }

    and it would work just as you desire.

    joe
    Joe Greer, Mar 10, 2011
    #3
  4. MJ_India

    MJ_India Guest

    On Mar 10, 10:17 pm, Victor Bazarov <> wrote:
    > On 3/10/2011 8:00 AM, MJ_India wrote:
    >
    > > struct foo { static const int bar = 0; };
    > > int baz() {
    > >      using foo::bar;    // [1]
    > >      return bar;          // [2]
    > > }

    >
    > > [1] is invalid in standard C++ because standard C++ allows _using_
    > > keyword only in 3 ways (For namespace, a member of namespace or in
    > > derived classes to avoid shadowing or to increase privateness of
    > > inherited member)

    >
    > > To make statement [2] work without fully qualified name I need some
    > > workaround(a reference or define).

    >
    > I would like to see a reason to prefer
    >
    >      <some trick to bring foo::bar into scope>
    >      return bar;
    >
    > over
    >
    >      return foo::bar;
    >
    > .  Please provide it.
    >

    1. Okay, it is analogous to some_member over
    a_namespace_name::some_member. (Which is allowed)
    2. It can sometimes increase readability.
    > > My question is, what is the advantage of not allowing [1] as valid C++
    > > grammar?

    >
    > The advantage is simplification of the language.
    >
    > > I sometimes think allowing [1] may be useful in some scenarios

    >
    > Name at least one, please.

    There are 2 classes I can not change.
    class CPdf {
    public:
    static const performance_parameter_01 = 0;
    ...
    };

    class CDoc {
    public:
    static const performance_parameter_01 = 100;
    static const performance_parameter_02 = 200;
    ...
    };

    namespace default_parameter {
    static const performance_parameter_01 = 50;
    static const performance_parameter_02 = 250;
    }

    using namespace default_parameter;
    result viewer(CPdf &pdf) {
    using CPdf::performance_parameter_01;
    ...
    TunePerformance(performance_parameter_01, performance_parameter_02);
    ...
    }

    result viewer(CDoc &doc) {
    using CDoc::performance_parameter_01;
    using CDoc::performance_parameter_02;
    ...
    TunePerformance(performance_parameter_01, performance_parameter_02);
    ...
    }

    I don't remember exact scenario, but it happened long back.
    I was using some library and I felt if I can write some code like
    above, it was more readable.
    But I ended up writing many big named classes, sub classes, final
    members and many scope resolution operators because of the code layout
    of that module writer and lack of using class_name::member.
    >
    >  > and
    >
    > > cannot think of a reason why it was decided incorrect. Only advantage
    > > I see here is, it makes compiler implementation little easy (Does it
    > > really? may be.)
    > > But I don't think this is one of the possible answer for my question,
    > > considering C++ complexities. (generic programming etc)

    >
    > Huh?
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask
    MJ_India, Mar 10, 2011
    #4
  5. MJ_India

    MJ_India Guest

    On Mar 10, 10:31 pm, Joe Greer <> wrote:
    > MJ_India <> wrote in news:2a8c7f36-0cc3-4d35-
    > :
    >
    >
    >
    > > struct foo { static const int bar = 0; };
    > > int baz() {
    > >     using foo::bar;    // [1]
    > >     return bar;          // [2]
    > > }

    >
    > > [1] is invalid in standard C++ because standard C++ allows _using_
    > > keyword only in 3 ways (For namespace, a member of namespace or in
    > > derived classes to avoid shadowing or to increase privateness of
    > > inherited member)

    >
    > > To make statement [2] work without fully qualified name I need some
    > > workaround(a reference or define).

    >
    > > My question is, what is the advantage of not allowing [1] as valid C++
    > > grammar?
    > > I sometimes think allowing [1] may be useful in some scenarios and
    > > cannot think of a reason why it was decided incorrect. Only advantage
    > > I see here is, it makes compiler implementation little easy (Does it
    > > really? may be.)
    > > But I don't think this is one of the possible answer for my question,
    > > considering C++ complexities. (generic programming etc)

    >
    > I can't give any sort of official answer, but it seems to me that items
    > that are declared in a struct/class should have a special association
    > with that struct/class and shouldn't be manipulated outside of it without
    > going through some pains.  If you just want a global, you should putthe

    Thinking this (↑) way helped me to resolve my confusion. Thank you for
    the above paragraph.
    > variable in a namespace and then the using would work as you desire.  
    > That is, there is no reason for the above example that it couldn't be
    > written as:
    >
    > namespace foo {
    >    const int bar = 0;}
    >
    > int baz() {
    >    using foo::bar;
    >    return bar;
    >
    > }

    Actually I cooked a minimal example to demonstrate my question.
    After bar, foo may be continue to be a complex class containing all
    possible type of members.
    >
    > and it would work just as you desire.
    >
    > joe
    MJ_India, Mar 10, 2011
    #5
  6. On 3/10/2011 8:52 AM, MJ_India wrote:
    > On Mar 10, 10:17 pm, Victor Bazarov<> wrote:
    >> On 3/10/2011 8:00 AM, MJ_India wrote:
    >>
    >>> struct foo { static const int bar = 0; };
    >>> int baz() {
    >>> using foo::bar; // [1]
    >>> return bar; // [2]
    >>> }

    >>
    >>> [1] is invalid in standard C++ because standard C++ allows _using_
    >>> keyword only in 3 ways (For namespace, a member of namespace or in
    >>> derived classes to avoid shadowing or to increase privateness of
    >>> inherited member)

    >>
    >>> To make statement [2] work without fully qualified name I need some
    >>> workaround(a reference or define).

    >>
    >> I would like to see a reason to prefer
    >>
    >> <some trick to bring foo::bar into scope>
    >> return bar;
    >>
    >> over
    >>
    >> return foo::bar;
    >>
    >> . Please provide it.
    >>

    > 1. Okay, it is analogous to some_member over
    > a_namespace_name::some_member. (Which is allowed)
    > 2. It can sometimes increase readability.


    Use references. Like

    const int& bar = foo::bar;
    ..
    return bar;

    There, I just increased readability. Or did I? How is hiding the
    source of 'bar' symbol increasing readability? No, really. How?

    >>> My question is, what is the advantage of not allowing [1] as valid C++
    >>> grammar?

    >>
    >> The advantage is simplification of the language.
    >>
    >>> I sometimes think allowing [1] may be useful in some scenarios

    >>
    >> Name at least one, please.

    > There are 2 classes I can not change.
    > class CPdf {
    > public:
    > static const performance_parameter_01 = 0;
    > ...
    > };
    >
    > class CDoc {
    > public:
    > static const performance_parameter_01 = 100;
    > static const performance_parameter_02 = 200;
    > ...
    > };
    >
    > namespace default_parameter {
    > static const performance_parameter_01 = 50;
    > static const performance_parameter_02 = 250;
    > }
    >
    > using namespace default_parameter;
    > result viewer(CPdf&pdf) {
    > using CPdf::performance_parameter_01;
    > ...
    > TunePerformance(performance_parameter_01, performance_parameter_02);
    > ...
    > }
    >
    > result viewer(CDoc&doc) {
    > using CDoc::performance_parameter_01;
    > using CDoc::performance_parameter_02;
    > ...
    > TunePerformance(performance_parameter_01, performance_parameter_02);
    > ...
    > }


    Which part of this is yours and which part can't you change?

    > I don't remember exact scenario, but it happened long back.
    > I was using some library and I felt if I can write some code like
    > above, it was more readable.


    Well, readability in the eye of the beholder. Besides, there are other
    ways to make the code more readable, like giving meaningful names to
    variables.

    > But I ended up writing many big named classes, sub classes, final
    > members and many scope resolution operators because of the code layout
    > of that module writer and lack of using class_name::member.


    The code was apparently awful to begin with (from your readability
    standpoint). Adding a couple of scope resolution operators (qualifying
    some names to help compiler resolve them) didn't really hurt it, did it?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 10, 2011
    #6
  7. MJ_India

    RaZiel Guest

    On 10.03.2011 14:52, MJ_India wrote:
    > On Mar 10, 10:17 pm, Victor Bazarov<> wrote:
    >> On 3/10/2011 8:00 AM, MJ_India wrote:

    > There are 2 classes I can not change.
    > class CPdf {
    > public:
    > static const performance_parameter_01 = 0;
    > ...
    > };
    >
    > class CDoc {
    > public:
    > static const performance_parameter_01 = 100;
    > static const performance_parameter_02 = 200;
    > ...
    > };
    >
    > namespace default_parameter {
    > static const performance_parameter_01 = 50;
    > static const performance_parameter_02 = 250;
    > }
    >
    > using namespace default_parameter;
    > result viewer(CPdf&pdf) {
    > using CPdf::performance_parameter_01;
    > ...
    > TunePerformance(performance_parameter_01, performance_parameter_02);
    > ...
    > }
    >
    > result viewer(CDoc&doc) {
    > using CDoc::performance_parameter_01;
    > using CDoc::performance_parameter_02;
    > ...
    > TunePerformance(performance_parameter_01, performance_parameter_02);
    > ...
    > }
    >
    > I don't remember exact scenario, but it happened long back.
    > I was using some library and I felt if I can write some code like
    > above, it was more readable.
    > But I ended up writing many big named classes, sub classes, final
    > members and many scope resolution operators because of the code layout
    > of that module writer and lack of using class_name::member.


    I'm sure you don't want to make a copy, but why don't you use a const
    reference?

    int& const performance_parameter_01 = CDoc::performance_parameter_01;


    - RaZiel
    RaZiel, Mar 10, 2011
    #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. Herman Eldering

    Re: Why does using keyword require a new scope?

    Herman Eldering, Aug 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    965
    Herman Eldering
    Aug 24, 2003
  2. Jip from Paris
    Replies:
    0
    Views:
    1,895
    Jip from Paris
    Aug 25, 2003
  3. Thomas H
    Replies:
    6
    Views:
    3,157
    Thomas H
    Feb 25, 2005
  4. Replies:
    6
    Views:
    449
    Peter Otten
    May 10, 2007
  5. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    349
Loading...

Share This Page