Why is this not allowed anymore (anymore being with a more recent compiler)?

Discussion in 'C++' started by Eric Lilja, Nov 6, 2006.

  1. Eric Lilja

    Eric Lilja Guest

    So I tried to compile code that my boss wrote with a newer compiler
    than I was previously using (FYI, the code compiles fine on g++ 3.4.4
    but not on 4.1.1). I condensed it into this simple testprogram:

    #include <string>

    class Foo
    {
    private:
    static const std::string LETTERS;
    };

    using namespace std;

    namespace
    {
    const string Foo::LETTERS("abc");
    }

    int
    main()
    {
    Foo f;

    (void)f;
    }

    I must admit I'm not entirely sure what he want the anonymous (correct
    term?) namespace for, but the more recent compiler sure doesn't like
    it:
    $ g++ --version
    g++ (GCC) 4.1.1
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is
    NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
    PURPOSE.

    $ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
    foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
    enclosing 'Foo'

    I need you gurus on this one. :)

    / E
    Eric Lilja, Nov 6, 2006
    #1
    1. Advertising

  2. Eric Lilja

    VJ Guest

    Re: Why is this not allowed anymore (anymore being with a more recentcompiler)?

    Eric Lilja wrote:
    > So I tried to compile code that my boss wrote with a newer compiler
    > than I was previously using (FYI, the code compiles fine on g++ 3.4.4
    > but not on 4.1.1). I condensed it into this simple testprogram:
    >
    > #include <string>
    >
    > class Foo
    > {
    > private:
    > static const std::string LETTERS;
    > };
    >
    > using namespace std;
    >
    > namespace
    > {
    > const string Foo::LETTERS("abc");
    > }
    >
    > int
    > main()
    > {
    > Foo f;
    >
    > (void)f;
    > }
    >
    > I must admit I'm not entirely sure what he want the anonymous (correct
    > term?) namespace for, but the more recent compiler sure doesn't like


    anonymous namespace is instead of using static variables
    (variables/functions are only visible in the file where they are defined)

    > it:
    > $ g++ --version
    > g++ (GCC) 4.1.1
    > Copyright (C) 2006 Free Software Foundation, Inc.
    > This is free software; see the source for copying conditions. There is
    > NO
    > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
    > PURPOSE.
    >
    > $ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
    > foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
    > enclosing 'Foo'
    >
    > I need you gurus on this one. :)
    >
    > / E
    >


    Your program compiles for me without warnings and errors. My compiler is:
    gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
    VJ, Nov 6, 2006
    #2
    1. Advertising

  3. Re: Why is this not allowed anymore (anymore being with a more recentcompiler)?

    * Eric Lilja:
    > So I tried to compile code that my boss wrote with a newer compiler
    > than I was previously using (FYI, the code compiles fine on g++ 3.4.4
    > but not on 4.1.1). I condensed it into this simple testprogram:
    >
    > #include <string>
    >
    > class Foo
    > {
    > private:
    > static const std::string LETTERS;
    > };
    >
    > using namespace std;
    >
    > namespace
    > {
    > const string Foo::LETTERS("abc");
    > }
    >
    > int
    > main()
    > {
    > Foo f;
    >
    > (void)f;
    > }
    >
    > I must admit I'm not entirely sure what he want the anonymous (correct
    > term?) namespace for, but the more recent compiler sure doesn't like
    > it:
    > $ g++ --version
    > g++ (GCC) 4.1.1
    > Copyright (C) 2006 Free Software Foundation, Inc.
    > This is free software; see the source for copying conditions. There is
    > NO
    > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
    > PURPOSE.
    >
    > $ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
    > foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
    > enclosing 'Foo'
    >
    > I need you gurus on this one. :)


    §9.4.2/2 "The definition of a static data member shall appear in a
    namespace scope enclosing the member's class definition" (the same rule
    applies to member functions).

    In the above code, the static data member is defined in a namespace that
    does not enclose the class definition.

    The anonymous namespace is just silly and incorrect the way it's used
    above, but you could put the class definition inside. Also, in C and
    C++ preferentially reserve all uppercase names for macros (is your boss
    a former Java programmer?). Using all uppercase you (or your boss) have
    no reason to complain if some header file defines a macro LETTERS that
    interferes destructively with the code.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 6, 2006
    #3
  4. Eric Lilja

    Daniel T. Guest

    "Eric Lilja" <> wrote:

    > So I tried to compile code that my boss wrote with a newer compiler
    > than I was previously using (FYI, the code compiles fine on g++ 3.4.4
    > but not on 4.1.1). I condensed it into this simple testprogram:
    >
    > #include <string>
    >
    > class Foo
    > {
    > private:
    > static const std::string LETTERS;
    > };
    >
    > using namespace std;
    >
    > namespace
    > {
    > const string Foo::LETTERS("abc");
    > }
    >
    > int
    > main()
    > {
    > Foo f;
    >
    > (void)f;
    > }
    >
    > I must admit I'm not entirely sure what he want the anonymous (correct
    > term?) namespace for, but the more recent compiler sure doesn't like
    > it:
    > $ g++ --version
    > g++ (GCC) 4.1.1
    > Copyright (C) 2006 Free Software Foundation, Inc.
    > This is free software; see the source for copying conditions. There is
    > NO
    > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
    > PURPOSE.
    >
    > $ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
    > foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
    > enclosing 'Foo'
    >
    > I need you gurus on this one. :)


    It doesn't look like it should compile. LETTERS is declared as a member
    of Foo which is in global scope but never defined as such; instead it's
    defined in anonymous namespace scope.

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Nov 6, 2006
    #4
  5. Eric Lilja

    VJ Guest

    Re: Why is this not allowed anymore (anymore being with a more recentcompiler)?

    VJ wrote:
    > Eric Lilja wrote:
    >


    >> class Foo
    >> {
    >> private:
    >> static const std::string LETTERS;
    >> };
    >>
    >> using namespace std;
    >>
    >> namespace
    >> {
    >> const string Foo::LETTERS("abc");
    >> }


    Reading other posts, I just realized that he put the definition of the
    static variable inside anonymous namespace. If class Foo was declared in
    another namespace, this would not compile at all
    VJ, Nov 6, 2006
    #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. =?Utf-8?B?QWxleCBTLg==?=

    Code being Debugged is not most recent

    =?Utf-8?B?QWxleCBTLg==?=, Jan 31, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    282
    =?Utf-8?B?QWxleCBTLg==?=
    Feb 1, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,673
    Smokey Grindel
    Dec 2, 2006
  3. Rodolfo Lima
    Replies:
    0
    Views:
    276
    Rodolfo Lima
    Jun 28, 2010
  4. Jian Lin
    Replies:
    1
    Views:
    122
    Luis Lavena
    Dec 9, 2009
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    469
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page