473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 - pi**@dohd.org
Oct 30 '06 #1
2 1692
* 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?
Oct 30 '06 #2
* 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 - pi**@dohd.org
Oct 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rick | last post by:
Hi, We all know that Java has classes but how about basic storage objects like structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types etc. How about Java? Greetings, Rick
2
by: Marcin Kalicinski | last post by:
Hi, Why local structs/classes cannot be used as template parameters? void f(std::list &l) { struct Cmp { /*...*/ }; l.sort(Cmp()); // Error here }
6
by: Matthew Jakeman | last post by:
If i create a cruct like so : struct test{ char *var1 ; int var2 ; short var3 ; } and i want to allocate it memory, should i allocate enough to hold the max amount of data that will go...
8
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my...
8
by: Joe Van Dyk | last post by:
I have a vector of structs. Say I get a new struct that's supposed to replace one of the structs in the vector. I believe I could use replace_if() to do this, but I'm not sure what the syntax...
6
by: Christian Christmann | last post by:
Hi, a question on local structs: Usually, when a local variable is used without initialization, it might hold any value. According to C99, does this rule also apply to elements of local...
2
by: silversurfer | last post by:
Hello, I am a little unsure whether this method really makes sense. The goal is to add an element to a vector. This is the struct and method I am using: std::vector<Entry> models; struct...
5
by: ravi.sathyam | last post by:
Hey, So say I have a sockaddr_in struct stored in a packet which I receive from my udp socket.....and it is stored within a certain offset into this packet (which is basically a char array)....
13
by: Tom Baxter | last post by:
Hi everyone, Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new' and 'protected' are valid modifiers on struct declarations. First, how can 'protected' be valid on a struct,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.