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 - pi**@dohd.org