leading underscore

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Axter

    leading underscore

    Section 17.4.3.1.2 states that each name that begins with an underscore
    is reserved to the implementation for use as a name in the global
    namespace.

    Exactly what defines the implementation?

    Is it only narrowly defined by the compiler's internal name handling?
    Or does it also include the library that comes with the compiler?
    Does it include the OS library as part of the implementation?

    I'm developing some portable leak tracking code, and for most functions
    I can't use namespaces in the header because the code has to be able to
    compile to both C and C++ source files.

    I'm trying to determine what would be the best naming convention to
    use, and I'm considering something like the following:
    _leaktracker61_ free
    _leaktracker61_ realloc
    _leaktracker61_ DumpAllLeaks
    _leaktracker61_ fopen
    _leaktracker61_ fclose

    When compiled in DEBUG mode, these functions are going to be in the
    global namespace, so I'm trying to limit name collision.

    I'm wondering if I can consider my code as an extension to the
    implementation, and there by use the leading underscore to avoid name
    collision.

  • Ron Natalie

    #2
    Re: leading underscore

    Axter wrote:
    [color=blue]
    > Exactly what defines the implementation?
    >
    > Is it only narrowly defined by the compiler's internal name handling?
    > Or does it also include the library that comes with the compiler?
    > Does it include the OS library as part of the implementation?[/color]

    Yes, yes, and yes. The implementation is pretty much everything
    other than code YOU are writing.

    [color=blue]
    > I'm wondering if I can consider my code as an extension to the
    > implementation, and there by use the leading underscore to avoid name
    > collision.[/color]

    That wouldn't be portable.

    Your best bet is to NOT use the underscore and hope your
    long distinctive names don't collide with any in the application
    under test.

    Comment

    • Victor Bazarov

      #3
      Re: leading underscore

      Axter wrote:[color=blue]
      > Section 17.4.3.1.2 states that each name that begins with an underscore
      > is reserved to the implementation for use as a name in the global
      > namespace.
      >
      > Exactly what defines the implementation?[/color]

      You're the one who is reading the Standard. Doesn't it define the
      'implementation '?
      [color=blue]
      > Is it only narrowly defined by the compiler's internal name handling?
      > Or does it also include the library that comes with the compiler?[/color]

      What's the title of Clause 17 (from which 17.4.3.1.2 comes)?
      [color=blue]
      > Does it include the OS library as part of the implementation?[/color]

      Doesn't it already say about that in the beginning of Clause 17?
      [color=blue]
      > I'm developing some portable leak tracking code, and for most functions
      > I can't use namespaces in the header because the code has to be able to
      > compile to both C and C++ source files.
      >
      > I'm trying to determine what would be the best naming convention to
      > use, and I'm considering something like the following:
      > _leaktracker61_ free
      > _leaktracker61_ realloc
      > _leaktracker61_ DumpAllLeaks
      > _leaktracker61_ fopen
      > _leaktracker61_ fclose[/color]

      What is the reason for you to have the leading underscores? I mean, if
      you simply omit them, what's going to happen?
      [color=blue]
      > When compiled in DEBUG mode, these functions are going to be in the
      > global namespace, so I'm trying to limit name collision.
      >
      > I'm wondering if I can consider my code as an extension to the
      > implementation, and there by use the leading underscore to avoid name
      > collision.[/color]

      It is not an extension to the implementation _unless_ you work for the
      compiler vendor. Do you?

      V

      Comment

      • niklasb@microsoft.com

        #4
        Re: leading underscore

        Axter wrote:[color=blue]
        > Section 17.4.3.1.2 states that each name that begins with an underscore
        > is reserved to the implementation for use as a name in the global
        > namespace.[/color]

        My understanding is that a leading underscore is OK as long as it's not
        followed by an uppercase letter. To quote from the section you cited:

        "Each name that contains a double underscore (_ _) or begins with
        an underscore followed by an uppercase letter (2.11) is reserved to
        the implementation for any use."
        [color=blue]
        > I'm trying to determine what would be the best naming convention to
        > use, and I'm considering something like the following:
        > _leaktracker61_ free
        > _leaktracker61_ realloc
        > _leaktracker61_ DumpAllLeaks
        > _leaktracker61_ fopen
        > _leaktracker61_ fclose[/color]

        These should be OK, not because you're an "implementation " but
        because the names are not reserved according to the above rules.

        Comment

        • Ron Natalie

          #5
          Re: leading underscore

          niklasb@microso ft.com wrote:[color=blue]
          > Axter wrote:[color=green]
          >> Section 17.4.3.1.2 states that each name that begins with an underscore
          >> is reserved to the implementation for use as a name in the global
          >> namespace.[/color]
          >
          > My understanding is that a leading underscore is OK as long as it's not
          > followed by an uppercase letter. To quote from the section you cited:
          >[/color]

          Your understanding is wrong. Axter correctly quotes 17.4.3.1 of the
          standard. All use of leading underscore is prohibited in the global
          namespace.

          Comment

          • Axter

            #6
            Re: leading underscore

            Victor Bazarov wrote:[color=blue]
            > Axter wrote:[color=green]
            > > Section 17.4.3.1.2 states that each name that begins with an underscore
            > > is reserved to the implementation for use as a name in the global
            > > namespace.
            > > Exactly what defines the implementation?[/color]
            > You're the one who is reading the Standard. Doesn't it define the
            > 'implementation '?[/color]

            If I new that, I wouldn't be asking.....
            That's like someone looking for their keys, and you asking them where
            was the last place they put it.
            [color=blue][color=green]
            > > Is it only narrowly defined by the compiler's internal name handling?
            > > Or does it also include the library that comes with the compiler?
            > > Does it include the OS library as part of the implementation?[/color]
            > Doesn't it already say about that in the beginning of Clause 17?[/color]

            Not that I can see.
            I'm posting this question to recieve information from someone who knows
            the answer.
            If you don't know the answer, or you don't want to give the answer
            away, then please don't bother giving me the look it up in the
            dictionary answer.

            Comment

            • Axter

              #7
              Re: leading underscore

              Ron Natalie wrote:[color=blue]
              > Axter wrote:
              >[color=green]
              > > Exactly what defines the implementation?
              > >
              > > Is it only narrowly defined by the compiler's internal name handling?
              > > Or does it also include the library that comes with the compiler?
              > > Does it include the OS library as part of the implementation?[/color]
              >
              > Yes, yes, and yes. The implementation is pretty much everything
              > other than code YOU are writing.
              >
              >[color=green]
              > > I'm wondering if I can consider my code as an extension to the
              > > implementation, and there by use the leading underscore to avoid name
              > > collision.[/color]
              >
              > That wouldn't be portable.
              >
              > Your best bet is to NOT use the underscore and hope your
              > long distinctive names don't collide with any in the application
              > under test.[/color]

              Thanks.
              That's what I think I will do.

              I was debating which would have the higher probability of having a name
              collision. IMHO, it would be less likely to have a name collision with
              the implementation then with the user's code.
              But I guess to make the code truely portable, it would be better to
              leave the leading underscore out.

              Comment

              • Julián Albo

                #8
                Re: leading underscore

                Axter wrote:
                [color=blue]
                > Section 17.4.3.1.2 states that each name that begins with an underscore
                > is reserved to the implementation for use as a name in the global
                > namespace.
                > Exactly what defines the implementation?[/color]

                This is defined by the implementation.

                --
                Salu2

                Comment

                • Jonathan Mcdougall

                  #9
                  Re: leading underscore

                  Julián Albo wrote:[color=blue]
                  > Axter wrote:
                  >[color=green]
                  > > Section 17.4.3.1.2 states that each name that begins with an underscore
                  > > is reserved to the implementation for use as a name in the global
                  > > namespace.
                  > > Exactly what defines the implementation?[/color]
                  >
                  > This is defined by the implementation.[/color]

                  What what is... oh... uh... <stack overflow>.


                  Jonathan

                  Comment

                  • Default User

                    #10
                    Re: leading underscore

                    Axter wrote:
                    [color=blue]
                    > Section 17.4.3.1.2 states that each name that begins with an
                    > underscore is reserved to the implementation for use as a name in the
                    > global namespace.
                    >
                    > Exactly what defines the implementation?[/color]


                    It means, "not you".




                    Brian

                    Comment

                    • niklasb@microsoft.com

                      #11
                      Re: leading underscore

                      Ron Natalie wrote:[color=blue]
                      > niklasb@microso ft.com wrote:[color=green]
                      > > Axter wrote:[color=darkred]
                      > >> Section 17.4.3.1.2 states that each name that begins with an underscore
                      > >> is reserved to the implementation for use as a name in the global
                      > >> namespace.[/color]
                      > >
                      > > My understanding is that a leading underscore is OK as long as it's not
                      > > followed by an uppercase letter. To quote from the section you cited:
                      > >[/color]
                      >
                      > Your understanding is wrong. Axter correctly quotes 17.4.3.1 of the
                      > standard. All use of leading underscore is prohibited in the global
                      > namespace.[/color]

                      My bad. I didn't read on to the next paragraph after the one I quoted,
                      which deals with the global namespace.

                      I've never run into this since I've never had the urge to create a name
                      with a leading underscore at namespace scope. :-)

                      Comment

                      Working...