If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Struct with internal linkage

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?

Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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

Derp.

Thanks.

Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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).

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

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

Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.