lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This kludge seems to work:

namespace {
  namespace foo {
    extern int i;
  }
}

int foo::i = 12;



On Tue, Dec 6, 2011 at 3:02 PM, David Given <dg@cowlark.com> wrote:
On 06/12/11 19:24, Wim Couwenberg wrote:
[...]
> In c++ "const T obj;" is a definition (with internal linkage) while in
> c it is only a tentative definition (with external linkage).
> Moreover, in c++ const data *must* be initialized, either by an
> explicitly declared constructor or an initializer.  So in c++ "const
> TValue obj;" is indeed an error since TValue declares no constructor
> (obviously) and the definition of obj does not specify an initializer,
> such as ={}.

I have, in fact, just been looking into this...

The C++ idiomatic alternative to tentative definitions is to use unnamed
namespaces. So:

 static int i;
 ...
 static int i = 4;

...becomes:

 namespace
 {
   extern int i;
   ...
   int i = 4;
 }

Due to magic, i is visible throughout the rest of the file but nowhere else.

Unfortunately, I don't think this is possible:

 namespace
 {
   extern int i;
 }
 ...
 namespace
 {
   int i = 4;
 }

Backwards compatibility? They've heard of it.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ "Never attribute to malice what can be adequately explained by
│ stupidity." --- Nick Diamos (Hanlon's Razor)




--