Using declaration for a namespace name ?

Discussion in 'C++' started by Timothy Madden, Jun 18, 2012.

  1. Hello

    I have a header file, generated with flex++, that declares a class
    phpFlexLexer in the global namespace.

    I would like to include this header in the yy namespaces, like this:

    namespace yy
    {
    #include "FlexLexer.h"
    }

    so the class would than be yy::phpFlexLexer, but than the standard
    headers included by FlexLexer.h would declare symbols in namespace
    yy::std
    this way. Is there a way to write a using-declaration that brings the
    global name std into namespace yy ? So that if I later #include
    <iostream>
    from namespace yy, than any attempts to populate yy::std would really
    populate the global namespace std ?

    Or maybe it is just ok to populate yy::std and let things go this way ?

    Thank you,
    Timothy Madden
     
    Timothy Madden, Jun 18, 2012
    #1
    1. Advertising

  2. Timothy Madden

    Luca Risolia Guest

    On 18/06/2012 17:35, Timothy Madden wrote:
    > Hello
    >
    > I have a header file, generated with flex++, that declares a class
    > phpFlexLexer in the global namespace.
    >
    > I would like to include this header in the yy namespaces, like this:
    >
    > namespace yy
    > {
    > #include "FlexLexer.h"
    > }
    >
    > so the class would than be yy::phpFlexLexer, but than the standard
    > headers included by FlexLexer.h would declare symbols in namespace
    > yy::std
    > this way. Is there a way to write a using-declaration that brings the
    > global name std into namespace yy ? So that if I later #include
    > <iostream>


    Cannot you modify FlexLexer.h, after it has been generated, for e.g.

    // FlexLexer.h

    #include <iostream>

    namespace yy {
    using namespace std;
    // any FlexLexer-related declaration using symbols in std::
    }
     
    Luca Risolia, Jun 18, 2012
    #2
    1. Advertising

  3. On Mon, 18 Jun 2012 18:15:33 +0200, Luca Risolia wrote:

    > On 18/06/2012 17:35, Timothy Madden wrote:
    >> Hello
    >>
    >> I have a header file, generated with flex++, that declares a class
    >> phpFlexLexer in the global namespace.
    >>
    >> I would like to include this header in the yy namespaces, like this:
    >>
    >> namespace yy {
    >> #include "FlexLexer.h"
    >> }
    >>
    >> so the class would than be yy::phpFlexLexer, but than the standard
    >> headers included by FlexLexer.h would declare symbols in namespace
    >> yy::std this way. Is there a way to write a using-declaration that
    >> brings the global name std into namespace yy ? So that if I later
    >> #include <iostream>

    >
    > Cannot you modify FlexLexer.h, after it has been generated, for e.g.
    >
    > // FlexLexer.h
    >
    > #include <iostream>
    >
    > namespace yy {
    > using namespace std;
    > // any FlexLexer-related declaration using symbols in std::
    > }


    If I modify the file after it has been generated, than I would have to
    modify it after each and every time it is generated.

    My question is how to bring the name `std`, into my yy namespace, and
    only that name. That using-declaration you showed me would bring in the
    entire content of the std namespace (which is not what I want).

    What I would like is to do something like this:

    namespace std
    {
    }

    namespace yy
    {
    using ::std;
    #include <iostream>
    }

    That way, the included <iostream> would populate namespace ::std,
    although it is included from namespace yy;

    My question is if the using ::std; statement is correct in C++ and would
    work as expected, or if there is a better alternative to it.

    Thank you,
    Timothy Madden
     
    Timothy Madden, Jun 18, 2012
    #3
  4. On 6/18/2012 11:35 AM, Timothy Madden wrote:
    > Hello
    >
    > I have a header file, generated with flex++, that declares a class
    > phpFlexLexer in the global namespace.
    >
    > I would like to include this header in the yy namespaces, like this:
    >
    > namespace yy
    > {
    > #include "FlexLexer.h"
    > }


    That's A Bad Idea(tm). Why don't you include that header from within
    another class defined in a function inside a macro expansion?

    > so the class would than be yy::phpFlexLexer, but than the standard


    then

    > headers included by FlexLexer.h would declare symbols in namespace
    > yy::std


    Would they? Perhaps it's a bad implementation. Why can't they declare
    the symbos in ::std? Of course, normal library implementers rarely
    write their headers to overcome inclusion inside another namespace...
    because nobody writes it that way!

    > this way. Is there a way to write a using-declaration that brings the
    > global name std into namespace yy ? So that if I later #include
    > <iostream>
    > from namespace yy, than any attempts to populate yy::std would really
    > populate the global namespace std ?


    I don't know of any way. Maybe a namespace alias?...

    namespace yy {
    namespace std = ::std;
    ...
    }


    > Or maybe it is just ok to populate yy::std and let things go this way ?


    So, you're saying that you haven't even tried?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 18, 2012
    #4
  5. On 6/18/2012 1:18 PM, Timothy Madden wrote:
    > On Mon, 18 Jun 2012 18:15:33 +0200, Luca Risolia wrote:
    >
    >> On 18/06/2012 17:35, Timothy Madden wrote:
    >>> Hello
    >>>
    >>> I have a header file, generated with flex++, that declares a class
    >>> phpFlexLexer in the global namespace.
    >>>
    >>> I would like to include this header in the yy namespaces, like this:
    >>>
    >>> namespace yy {
    >>> #include "FlexLexer.h"
    >>> }
    >>>
    >>> so the class would than be yy::phpFlexLexer, but than the standard
    >>> headers included by FlexLexer.h would declare symbols in namespace
    >>> yy::std this way. Is there a way to write a using-declaration that
    >>> brings the global name std into namespace yy ? So that if I later
    >>> #include<iostream>

    >>
    >> Cannot you modify FlexLexer.h, after it has been generated, for e.g.
    >>
    >> // FlexLexer.h
    >>
    >> #include<iostream>
    >>
    >> namespace yy {
    >> using namespace std;
    >> // any FlexLexer-related declaration using symbols in std::
    >> }

    >
    > If I modify the file after it has been generated, than I would have to
    > modify it after each and every time it is generated.


    Right. If you let some tool generate your headers, what stops you from
    writing your own tool that, after the other tools is done generating the
    header, makes a slight modification to it?

    > My question is how to bring the name `std`, into my yy namespace, and
    > only that name. That using-declaration you showed me would bring in the
    > entire content of the std namespace (which is not what I want).
    >
    > What I would like is to do something like this:
    >
    > namespace std
    > {
    > }
    >
    > namespace yy
    > {
    > using ::std;


    I'd try

    namespace std = ::std;

    > #include<iostream>
    > }
    >
    > That way, the included<iostream> would populate namespace ::std,
    > although it is included from namespace yy;
    >
    > My question is if the using ::std; statement is correct in C++


    No, since 'std' is not an entity name, it's a namespace.

    > and would
    > work as expected, or if there is a better alternative to it.


    Perhaps a better alternative is to add your modification into the
    generation of the header toolchain.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 18, 2012
    #5
  6. On 06/18/2012 09:39 PM, Victor Bazarov wrote:
    > On 6/18/2012 11:35 AM, Timothy Madden wrote:
    >> Hello
    >>
    >> I have a header file, generated with flex++, that declares a class
    >> phpFlexLexer in the global namespace.
    >>
    >> I would like to include this header in the yy namespaces, like this:
    >>
    >> namespace yy
    >> {
    >> #include "FlexLexer.h"
    >> }

    >
    > That's A Bad Idea(tm). Why don't you include that header from within
    > another class defined in a function inside a macro expansion?
    >
    >> so the class would than be yy::phpFlexLexer, but than the standard

    >
    > then
    >
    >> headers included by FlexLexer.h would declare symbols in namespace
    >> yy::std

    >
    > Would they? Perhaps it's a bad implementation. Why can't they declare
    > the symbos in ::std? Of course, normal library implementers rarely write
    > their headers to overcome inclusion inside another namespace... because
    > nobody writes it that way!
    >
    >> this way. Is there a way to write a using-declaration that brings the
    >> global name std into namespace yy ? So that if I later #include
    >> <iostream>
    >> from namespace yy, than any attempts to populate yy::std would really
    >> populate the global namespace std ?

    >
    > I don't know of any way. Maybe a namespace alias?...
    >
    > namespace yy {
    > namespace std = ::std;
    > ...
    > }
    >
    >
    >> Or maybe it is just ok to populate yy::std and let things go this way ?

    >
    > So, you're saying that you haven't even tried?
    >
    > V


    I know my #include inside a namespace is a bad idea, but flex has no
    namespaces for its C++ interface, so the bad idea is all I can try to
    get it.

    Well I tried the namespace alias, and the compiler accepted the
    statement, but I still get errors later in the included system headers
    this way. So I resorted to making sure that I include any headers needed
    by FlexLexer.h before I open yy namespace. That way, when FlexLexer.h
    includes them, their include guards would simply skip all their content.

    And yes, I have not even tried until now, because I am just learning
    flex and bison, and believe me, trying to integrate them with their C++
    interface, for the first time, is not fun!

    Thank you,
    Timothy Madden
     
    Timothy Madden, Jun 22, 2012
    #6
  7. Timothy Madden

    Rui Maciel Guest

    Timothy Madden wrote:

    > I know my #include inside a namespace is a bad idea, but flex has no
    > namespaces for its C++ interface, so the bad idea is all I can try to
    > get it.


    Why not file a bug report describing your problem? This appears to be an
    important issue, which might affect a considerable number of users, and may
    not be that hard to implement.


    Rui Maciel
     
    Rui Maciel, Jun 22, 2012
    #7
  8. On 06/22/2012 09:39 PM, Rui Maciel wrote:
    > Timothy Madden wrote:
    >
    >> I know my #include inside a namespace is a bad idea, but flex has no
    >> namespaces for its C++ interface, so the bad idea is all I can try to
    >> get it.

    >
    > Why not file a bug report describing your problem? This appears to be an
    > important issue, which might affect a considerable number of users, and may
    > not be that hard to implement.


    You mean a report for flex to support C++ in a proper way ? I posted a
    question on gmane.comp.lex.flex.general, only to find out flex is no
    longer being developed (only some minimal maintenance).

    Trying to ask on gmain.comp.parsers.bison.general for an alternative to
    flex I got no response, so I think there is none.

    Anyway flex seem to lack even support for Unicode, which in my opinion
    is much worse than lack of C++ namespaces.

    http://thread.gmane.org/gmane.comp.lex.flex.general/2994
    http://thread.gmane.org/gmane.comp.lex.flex.general

    http://news.gmane.org/gmane.comp.parsers.bison.general

    Thank you,
    Timothy Madden
     
    Timothy Madden, Jul 3, 2012
    #8
  9. On 7/3/2012 8:34 AM, Timothy Madden wrote:
    > [..] I posted a
    > question on gmane.comp.lex.flex.general, only to find out flex is no
    > longer being developed (only some minimal maintenance).
    >
    > Trying to ask on gmain.comp.parsers.bison.general for an alternative to
    > flex I got no response, so I think there is none.
    >
    > Anyway flex seem to lack even support for Unicode, which in my opinion
    > is much worse than lack of C++ namespaces.
    >
    > http://thread.gmane.org/gmane.comp.lex.flex.general/2994
    > http://thread.gmane.org/gmane.comp.lex.flex.general
    >
    > http://news.gmane.org/gmane.comp.parsers.bison.general


    Seems like you're better off putting aside tools that don't work and
    aren't supported/developed any longer, and turn your sights to the tools
    that do and are. I am certain that if there is a need in the tools like
    flex or bison, there are replacements. Good luck!

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 3, 2012
    #9
  10. Timothy Madden

    Jorgen Grahn Guest

    On Tue, 2012-07-03, Victor Bazarov wrote:
    > On 7/3/2012 8:34 AM, Timothy Madden wrote:
    >> [..] I posted a
    >> question on gmane.comp.lex.flex.general, only to find out flex is no
    >> longer being developed (only some minimal maintenance).
    >>
    >> Trying to ask on gmain.comp.parsers.bison.general for an alternative to
    >> flex I got no response, so I think there is none.
    >>
    >> Anyway flex seem to lack even support for Unicode, which in my opinion
    >> is much worse than lack of C++ namespaces.
    >>
    >> http://thread.gmane.org/gmane.comp.lex.flex.general/2994
    >> http://thread.gmane.org/gmane.comp.lex.flex.general
    >>
    >> http://news.gmane.org/gmane.comp.parsers.bison.general

    >
    > Seems like you're better off putting aside tools that don't work and
    > aren't supported/developed any longer, and turn your sights to the tools
    > that do and are.


    Just to clarify: noone in the referenced thread said flex isn't
    maintained, just that it does "not currently have active developers".
    Not surprising: apart from non-ASCII support there are probably not
    too many interesting features left to implement after ~25 years.

    (And to reiterate what I wrote here earlier: I wouldn't touch its
    ancient experimental C++ code generator with a ten-foot pole.)

    > I am certain that if there is a need in the tools like
    > flex or bison, there are replacements. Good luck!


    If flex is good enough for enough people, that might be a problem.

    Also, since the heyday of lexers in the 1980s, CPU power had become
    very cheap and powerful regex libraries and scripting languages have
    become common. This is just speculation, but that may be another force
    working against it.

    But as Victor writes: good luck!

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jul 15, 2012
    #10
  11. Timothy Madden

    Jeff Flinn Guest

    On 7/15/2012 5:45 PM, Jorgen Grahn wrote:
    > On Tue, 2012-07-03, Victor Bazarov wrote:
    >> On 7/3/2012 8:34 AM, Timothy Madden wrote:
    >>> [..] I posted a
    >>> question on gmane.comp.lex.flex.general, only to find out flex is no
    >>> longer being developed (only some minimal maintenance).


    ....

    > (And to reiterate what I wrote here earlier: I wouldn't touch its
    > ancient experimental C++ code generator with a ten-foot pole.)
    >
    >> I am certain that if there is a need in the tools like
    >> flex or bison, there are replacements. Good luck!

    >
    > If flex is good enough for enough people, that might be a problem.
    >
    > Also, since the heyday of lexers in the 1980s, CPU power had become
    > very cheap and powerful regex libraries and scripting languages have
    > become common. This is just speculation, but that may be another force
    > working against it.


    Or use boost spirit qi to directly access ebnf/peg parsing technology
    directly from C++.

    Jeff
     
    Jeff Flinn, Jul 16, 2012
    #11
    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. Replies:
    0
    Views:
    5,168
  2. Jacek Dziedzic
    Replies:
    2
    Views:
    5,816
    John Carson
    Apr 29, 2004
  3. Jason Heyes
    Replies:
    1
    Views:
    472
    Woebegone
    Nov 19, 2004
  4. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,436
    Howard
    Nov 29, 2004
  5. Niels Dekker - no reply address

    Using-declaration or using-directive inside unnamed-namespace?

    Niels Dekker - no reply address, Apr 27, 2010, in forum: C++
    Replies:
    1
    Views:
    646
    Niels Dekker - no reply address
    Apr 27, 2010
Loading...

Share This Page