CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

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

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Struct with internal linkage

    Derp.

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

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

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

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Struct with internal linkage

    Quote Originally Posted by OReubens View Post
    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.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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
  •  





Click Here to Expand Forum to Full Width

Featured