-
March 15th, 2013, 09:00 AM
#1
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?
Is your question related to IO?
Read this C++ FAQ 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.
-
March 15th, 2013, 09:30 AM
#2
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 ... ).
-
March 15th, 2013, 09:48 AM
#3
Re: Struct with internal linkage
Is your question related to IO?
Read this C++ FAQ 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.
-
March 18th, 2013, 08:12 AM
#4
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.
-
March 18th, 2013, 09:25 AM
#5
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 ...
-
March 18th, 2013, 09:28 AM
#6
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 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.
-
March 21st, 2013, 12:03 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|