Topic: Class static linkage?


Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/01
Raw View
On 30 Oct 99 00:51:18 GMT, David R Tribble <david@tribble.com> wrote:

>Steve Clamage wrote:
>[about class names with static linkage]
>> The point is that names in the anonymous namespace cannot collide
>> with names in the anonymous namespace of another translation unit.
>
>Are you saying that the following is invalid?:
>
>    // 1.cpp
>    namespace /*anon*/
>    {
>        class Foo { ... }
>    }
>
>    // 2.cpp
>    namespace /*anon*/
>    {
>        class Foo { ... };
>    }
>

No it's perfectly valid. You have two different classes named Foo, but
they are in different namespaces, and it is impossible to refer to
either Foo outside of the translation unit where it is defined.

>IOW, what do you mean by "colliding" names

By "colliding" I mean duplication of names that create ambiguous
references, or result in violating the One-Definition Rule. If the two
versions of class Foo were not both in anonymous namespaces, it would
be possible for the names to collide via using-declarations or
using-directives.

Names in different anonymous namespaces cannot collide.

If you use an anonymous namespace in different headers and include
them in the same translation unit, they become the same anonymous
namespace in that translation unit, but different namespaces in
different translation units. Using the anonymous namespace in a header
is therefore likely to create a maintenace nightmare, assuming you can
ever get your program to behave as you intend.

---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <david@tribble.com>
Date: 1999/10/30
Raw View
Steve Clamage wrote:
[about class names with static linkage]
> The point is that names in the anonymous namespace cannot collide
> with names in the anonymous namespace of another translation unit.

Are you saying that the following is invalid?:

    // 1.cpp

    namespace /*anon*/
    {
        class Foo
        {
            ...
        };
    }


    // 2.cpp

    namespace /*anon*/
    {
        class Foo
        {
            ...
        };
    }

IOW, what do you mean by "colliding" names within the anonymous
namespace?  Is declaring names file-static (using the 'static'
specifier) safer than using the anonymous namespace?

-- David R. Tribble, david@tribble.com, http://www.david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: rado42 <rado42@my-deja.com>
Date: 1999/10/25
Raw View
In article <7uoing$m90$1@engnews1.eng.sun.com>,
  clamage@eng.sun.com (Steve Clamage) wrote:
> rado42 <rado42@my-deja.com> writes:
>
> >Class members always have external linkage.
>
> Right.
>
> >Is there any way to have static linkage for class members?
>
> No.
>
> >For instance, if anything is enclosed in anonimouus
> >namespace, it is not visible in other files. Does it not
> >suggest static linkage?
>
> No. You can use the addresses of functions and objects in the
> anonymous namespace as template parameters. Such arguments
> must have external linkage.
>
> Also, it's not correct to say the names are not visible.
> The names have external linkage, but there is no way to
> write the qualified name. The point is that names in the
> anonymous namespace cannot collide with names in the anonymous
> namespace of another translation unit.
>
> --
> Steve Clamage, stephen.clamage@sun.com

Thank you. How about inline member functions?
Are they not static by definition (even if actually
outlined by the compiler)?

--
rado
http://members.tripod.com/~radosoft


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/27
Raw View
rado42 <rado42@my-deja.com> writes:

>Thank you. How about inline member functions?
>Are they not static by definition (even if actually
>outlined by the compiler)?

Originally in C++, a non-member inline function had internal
linkage (as if declared static). The rule in the C++ standard
is that "inline" has no effect on linkage. An inline function
has external linkage unless you explicitly declare it static.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/22
Raw View
rado42 <rado42@my-deja.com> writes:

>Class members always have external linkage.

Right.

>Is there any way to have static linkage for class members?

No.

>For instance, if anything is enclosed in anonimouus
>namespace, it is not visible in other files. Does it not
>suggest static linkage?

No. You can use the addresses of functions and objects in the
anonymous namespace as template parameters. Such arguments
must have external linkage.

Also, it's not correct to say the names are not visible.
The names have external linkage, but there is no way to
write the qualified name. The point is that names in the
anonymous namespace cannot collide with names in the anonymous
namespace of another translation unit.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: rado42 <rado42@my-deja.com>
Date: 1999/10/21
Raw View
Hi
Class members always have external linkage.

Is there any way to have static linkage for class members?

For instance, if anything is enclosed in anonimouus
namespace, it is not visible in other files. Does it not
suggest static linkage?

Thanks


--
rado
http://members.tripod.com/~radosoft


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]