STL and local structs: Is this valid ?

Discussion in 'C++' started by Martijn van Buul, Oct 30, 2006.

  1. Hi,

    Today, I stumbled over the following:

    I had a struct, whose scope only made sense within a single function, and I
    wanted to use std::swap() on it, like the following example:

    #include <algorithm>

    void somefunction()
    {
    struct foobar
    {
    int bar;
    int wobble;
    } a,b;

    /* [...] */

    std::swap(a,b);
    }

    This worked just fine with MSVC (Visual Studio 2005, wouldn't that make it
    msvc 8?), but failed to work with g++:

    .../test.cc: In function `void somefunction()':
    .../test.cc:11: error: no matching function for call to `swap(somefunction()::foobar&, somefunction()::foobar&)'

    If I move the declaration to outside the definition of somefunction(), like
    this:

    #include <algorithm>
    namespace {
    struct foobar
    {
    int bar;
    int wobble
    };
    };

    void somefunction()
    {
    foobar a,b;

    std::swap(a,b);
    }

    things work out right for both g++ (4.1.2) as msvc. I can live with the
    promotion from local definition to unnamed namespace, so I'm obviously not
    looking for a will-you-fix-my-code? workaround. But, as far as I'm concerned,
    my first code example is valid, but obviously g++ disagrees, and I'm man
    enough to admit that I'm most likely wrong then. Apperently my idea about
    template functions is wrong, and I'd appreciate it if someone could set it
    straight.

    What I'd really like to know is *why* g++ is right. I tried to find an answer
    on the intarweb, but failed. You may shoot me if google gives a usable answer
    on a simple query...

    --
    Martijn van Buul -
     
    Martijn van Buul, Oct 30, 2006
    #1
    1. Advertising

  2. * Martijn van Buul:
    > Hi,
    >
    > Today, I stumbled over the following:
    >
    > I had a struct, whose scope only made sense within a single function, and I
    > wanted to use std::swap() on it, like the following example:
    >
    > #include <algorithm>
    >
    > void somefunction()
    > {
    > struct foobar
    > {
    > int bar;
    > int wobble;
    > } a,b;
    >
    > /* [...] */
    >
    > std::swap(a,b);
    > }
    >
    > This worked just fine with MSVC (Visual Studio 2005, wouldn't that make it
    > msvc 8?), but failed to work with g++:


    g++ is right and MSVC is wrong, per the current (but not necessarily per
    the future) standard.

    Per the current standard a local class has no linkage: it's not internal
    linkage and not external linkage, it's no linkage.

    Why that is I don't know (perhaps just same kind of historical accident
    that means you can't declare variables 'inline' directly, but must use
    template workaround if you want that), but given that for whatever
    reason local classes don't have linkage there are certain restrictions
    on them, including that they can't be used as a template parameters.

    C++ local classes are IMO currently just as much kludges as C# structs
    (a kind of value class).

    However, they're good for defining local helper functions.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 30, 2006
    #2
    1. Advertising

  3. * Alf P. Steinbach:
    > * Martijn van Buul:


    [snip]

    >> This worked just fine with MSVC (Visual Studio 2005, wouldn't that make it
    >> msvc 8?), but failed to work with g++:

    >
    > g++ is right and MSVC is wrong, per the current (but not necessarily per
    > the future) standard.


    Thanks :)

    > Per the current standard a local class has no linkage: it's not internal
    > linkage and not external linkage, it's no linkage.


    Ah, that's where I went wrong; I assumed the linkage was the same between
    both of my examples, and that only the scope changed.

    > Why that is I don't know (perhaps just same kind of historical accident
    > that means you can't declare variables 'inline' directly, but must use
    > template workaround if you want that), but given that for whatever
    > reason local classes don't have linkage there are certain restrictions
    > on them, including that they can't be used as a template parameters.


    Thank you for the explanation. Muchly appreciated.

    --
    Martijn van Buul -
     
    Martijn van Buul, Oct 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    646
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    561
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    647
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,411
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    479
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page