473,722 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum within function, is this standard?

I'm using an enum that's declared within a function (since I only need it
within that function.)

I can't find anything about this in "The C++ Programming Language" by
Stroustroup and I don't have the standard.

Is this legal?

// test.cpp
#include <iostream>

int main() {
enum {zero,one,two};
std::cout << zero << " " << one << " " << two << std::endl;
return 0;
}

g++ seems to think so:

$ g++ -ansi -pedantic -Wall test.cpp

Gives no output, which means no error or warnings.
--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 19 '05 #1
5 22390
"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message
news:pa******** *************** *****@spam.for. me.invalid...
| Is this legal?
|
| // test.cpp
| #include <iostream>
|
| int main() {
| enum {zero,one,two};
| std::cout << zero << " " << one << " " << two << std::endl;
| return 0;
| }

Yes, this is a totally legal anonymous enum declaration.

Structs and classes may be declared within a function
as well (and may also be anonymous).
The only limitation with types that are declared within
a function (rather than at namespace or class scope)
is that they cannot be used as template parameters.

hth - Ivan
--
http://ivan.vecerina.com


Jul 19 '05 #2
Ivan Vecerina wrote:
Structs and classes may be declared within a function
as well (and may also be anonymous).
The only limitation with types that are declared within
a function (rather than at namespace or class scope)
is that they cannot be used as template parameters.


so the following breaks? why?

template <typename T>
struct A
{
T t;
};

void f()
{
enum B { zero, one };
A<B> t;
}
-----[ Domenico Andreoli, aka cavok
--[ http://filibusta.crema.unimi.it/~cavok/gpgkey.asc
---[ 3A0F 2F80 F79C 678A 8936 4FEE 0677 9033 A20E BC50

Jul 19 '05 #3
Domenico Andreoli wrote in news:86Qsb.1128 09$vO5.4389182
@twister1.liber o.it:
Ivan Vecerina wrote:


[micro-snip]
The only limitation with types that are declared within
a function (rather than at namespace or class scope)
is that they cannot be used as template parameters.


so the following breaks? why?

template <typename T>
struct A
{
T t;
};

void f()
{
enum B { zero, one };
A<B> t;
}


The name B doesn't have external linkage. If you consider adding:

void g()
{
enum B { three, four };
A<B> t;
}

You can see the problem, you've actually tried to create 2 types
bothe withe the same id A< no-real-name-enum >.

IOW templates base there external-linkage on the external-linkage
of there arguments.

This is how the standard was/is writen. It could have been otherwise,
but would the benefits outway the cost (for implementers this is
man-hour's of programming, for us it's more waiting for fully
conforming compiler's).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
"Rob Williscroft" <rt*@freenet.RE MOVE.co.uk> wrote in message
news:Xn******** *************** ***********@195 .129.110.204...
Domenico Andreoli wrote in news:86Qsb.1128 09$vO5.4389182
@twister1.liber o.it:
so the following breaks? why?

template <typename T>
struct A
{
T t;
};

void f()
{
enum B { zero, one };
A<B> t;
}
The name B doesn't have external linkage. If you consider adding:

Exact. IOW templates base there external-linkage on the external-linkage
of there arguments. Which from a typical implementation' s perspective means that
the name of the parameter type is mangled into (used as part of)
the name of template instanciation.
This is how the standard was/is writen. It could have been otherwise,
but would the benefits outway the cost (for implementers this is
man-hour's of programming, for us it's more waiting for fully
conforming compiler's).

Maybe, but this is debatable. Since the compiler has support for
anonymous namespaces, it knows how to generate unique names
already.

Many find this limitation is especially annoying when writing
functors and predicates:
void sortByAge(std:: vector<Item>& items)
{
struct AgeCompare {
bool operator()(Item const& a, Item const&b)
{ return a.age<b.age; }
};
std::sort(items .begin(),items. end(),AgeCompar e()); //error
}
Because AgeCompare has no external linkage, it has to be
moved out of the function. Ugly.

I'm pretty sure that dropping this limitation has been proposed
for the next C++ standard...

Regards,
Ivan
--
http://ivan.vecerina.com


Jul 19 '05 #5
Ivan Vecerina wrote in news:bp******** *@newshispeed.c h:
"Rob Williscroft" <rt*@freenet.RE MOVE.co.uk> wrote in message
news:Xn******** *************** ***********@195 .129.110.204...
[snip]
The name B doesn't have external linkage. If you consider adding:

Exact.
IOW templates base there external-linkage on the external-linkage
of there arguments.

Which from a typical implementation' s perspective means that
the name of the parameter type is mangled into (used as part of)
the name of template instanciation.
This is how the standard was/is writen. It could have been otherwise,
but would the benefits outway the cost (for implementers this is
man-hour's of programming, for us it's more waiting for fully
conforming compiler's).

Maybe, but this is debatable. Since the compiler has support for
anonymous namespaces, it knows how to generate unique names
already.

Yes, the above was an attempt at a (partial) explanation of why it
isn't currently in the standard. Not a suggestion that it isn't worth
putting in the next.

In fact gcc can already do this (at least with local classes). Also
it should be easier than for anonymous namespaces as there is already a
unique name (the function's) to base the external-linkage-name on.
Many find this limitation is especially annoying when writing
functors and predicates:
void sortByAge(std:: vector<Item>& items)
{
struct AgeCompare {
bool operator()(Item const& a, Item const&b)
{ return a.age<b.age; }
};
std::sort(items .begin(),items. end(),AgeCompar e()); //error
}
Because AgeCompare has no external linkage, it has to be
moved out of the function. Ugly.

I'm pretty sure that dropping this limitation has been proposed
for the next C++ standard...


I have a vague recollection of reading something about this, but I
can't remember what is covered, 1) local classes, 2) local enum's,
3) nameless classes (struct {} x;) and 4) nameless enum's. It's
probably just 1 and 2, but 3 and 4 should be doable too, but does
anybody care enough to write up the proposal and justify it to the
committee ?

Some people would like to write (vc6 users currently can):

some_template< "string-id" >;

Maybe well get that too.

All of these thing's would be nice (and useful), but there not top
of my wish list (Move semantics and unlimited template paramiters are).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #6

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

Similar topics

2
2606
by: Shugong Wang | last post by:
getch() function is not a standard C/C++ function. Thought gcc provieds ncurses.h and a getch() function is realized, I still want to know how to realize a getch() function in standard c or c++.
4
1817
by: imme929 | last post by:
I got things working until I tried adding this enum to a structure... Public Enum Keyboard EnglishUS EnglishUK Spanish German Italian French
13
2078
by: Anthony de Almeida Lopes | last post by:
Hello, I am wondering why it is not possible to have a function-like macro like the following: #define __nothread(name) do { \ #ifdef _PTHREAD_H \ #warning "name is not a thread safe function" \ } while (0)
2
3446
by: mj | last post by:
Hi, I recently have found it necessary to move from fortran to c++ for scientific programming... I'm working on a program that needs to resize a 2d vector of vectors within a function... This variable "tri" is an input arg to my function using the syntax: function(vector<vector<int> >& tri) The problem occurs when the tri 'matrix' is resized to triple or
3
1673
by: not_a_commie | last post by:
Does MS have an official C# variable/function/codingstyle standard that is available on the internet? What standard do they use for their internal coding? Mind posting a link? Thanks.
4
12095
by: cupa | last post by:
Hi, is it posible to execute script from file within function (sql or pl/sql or ...)
6
45002
by: sethukr | last post by:
can we define enum within a structure?? for example, typedef enum {red,blue}color; struct s1{ color c; }obj;
2
2255
by: gert | last post by:
obj=function() { this.attribute=1 this.method=function() { img=document.createElement('img') img.onclick=function { this.attribute=2 // i need the obj this not the img this :) }
8
3907
by: thatcollegeguy | last post by:
http://smarterfootball.com/exits/theHTML.html I am not sure what is wrong w/ this code. The main issue is that the table that is in the initial html will empty its td but the table that I load using php and jquery through a mysql database will not empty the td elements. There is a table underneath the button in the initial html. if you click on a box in the table, it will empty. the same does not happen for the table that is generated by...
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.