Struct with internal linkage
Hi all (long time no see)
I've been wondering about something for a while:
Is it possible to declare a struct/class, in a cpp file, designed for local use, but with internal linkage?
The usecase is that every once in a while, I want to wrap "startXXX+endXXX" function pairs in a simple RAII struct. I just declare the struct in my cpp and use it once.
However, if I do this, (AFAIK), the compiler will generate an entry in the link table, which means I could potentially have link conflicts if I declare the same struct twice in two different cpp files.
Unless I'm mistaken, since the struct is declared in the same cpp that it is used, I wouldn't need external linkage. Is there a way to avoid it?
Re: Struct with internal linkage
you can use an anonymous namespace. Linkage is still external but symbol names will be unique across compilation units ( which tantamounts to be internal as no linker conflicts can occur and the linker will take care of stripping unused symbols later anyway ... ).
Re: Struct with internal linkage
Re: Struct with internal linkage
that's what 'static' is for (when used declaratively at global scope)
Code:
static struct foo
{
int bar;
};
in the above case, the foo structure will only be visible in the compilation unit (typically 1 .cpp file) only. It will not be exported, it won't be listed in the .obj or in the .lib (it will be in there with debug-builds for debuggign purposes of course).
static has other uses (and other meaning) in other contexts.
Re: Struct with internal linkage
static as a linkage specification has been deprecated in favor of anonymous namespaces. Moreover, it works only with functions and variables, not types. Indeed, I think your code snippet is not legal ( although VC2010 seems accepting it with just a warning about it having no effect when no variable name is specified ). AFAIK, the only reason to use static this way in place of anonymous namespaces ( ignoring C compatibility ) is to ease the inspection/manipulation of the generated linker symbols ...
Re: Struct with internal linkage
Quote:
Originally Posted by
OReubens
that's what 'static' is for (when used declaratively at global scope)
Code:
static struct foo
{
int bar;
};
in the above case, the foo structure will only be visible in the compilation unit (typically 1 .cpp file) only. It will not be exported, it won't be listed in the .obj or in the .lib (it will be in there with debug-builds for debuggign purposes of course).
static has other uses (and other meaning) in other contexts.
That'd be my "go to" solution, if it was legal C++.
GCC gives:
error: a storage class can only be specified for objects and functions
VStudio compiles, but emits:
warning C4091: 'static ' : ignored on left of 'foo' when no variable is declared
Re: Struct with internal linkage
erp...
typo on my end here. I meant:
Code:
struct foo
{
int bar;
};
static foo Foo;
code outside the compilation unit can't see the "struct foo" anyway because it's just a struct definition (and those aren't exported in any way anyway). declaring Foo as a static foo is what makes the declaration (the variable) "local" to the compilation unit.