STL and local structs: Is this valid ?

M

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++:

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

Alf P. Steinbach

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

Martijn van Buul

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top