Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL and local structs: Is this valid ? (http://www.velocityreviews.com/forums/t458032-stl-and-local-structs-is-this-valid.html)

Martijn van Buul 10-30-2006 07:47 PM

STL and local structs: Is this valid ?
 
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 - pino@dohd.org

Alf P. Steinbach 10-30-2006 07:55 PM

Re: STL and local structs: Is this valid ?
 
* 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?

Martijn van Buul 10-30-2006 08:09 PM

Re: STL and local structs: Is this valid ?
 
* 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 - pino@dohd.org


All times are GMT. The time now is 09:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.