Boost logo

Boost Users :

Subject: Re: [Boost-users] Annoying compiler warnings in asio/error.hpp
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2010-07-14 07:13:47


> I think it is not the same as defining a global static variable.
Global
> static variable will have internal linkage and therefore be visible
inside
> the compilation unit where defined only. Between the compilation units
these
> variables will be different.

No the fancy trick works. I've seen it used in XSD-tree's code which is
completly a header only. They put all their globals in a template class
with a single parameter char. MyClass<char>::variable has to be the
same in all compilation scopes. The standard says so. So the linker
has to eliminate duplicate symbols.

> IMO C++ like approach would be to introduce an
> anonymous namespace in header files and define global variables there.

> Defining static vars in the template class will result in a static
variable
> per class template instantiation. Which is not exactly the same, as
intended
> here.

Anonymous namespaces in headers are bad; as are declaring things static
in headers. It makes the compiler generate seperate objects per
compilation scope, which isn't what you want.

The compiler warning is right. If I include this header in 20
compilation scopes I shouldn't have 20 copies of these constants (just
refs) in my binary.

At least the stuff in boost/system/error_code.hpp could easily be moved
to a cxx. Boost::system is not a header only library.

Chris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net