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