473,563 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static_cast and globally scoped types.

The following program shows the problem:

#include <stdio.h>

namespace x
{
enum L { One = 1, Two = 2 };
namespace x
{
enum L { Three = 3, Four = 4 };
void foo(long mylong)
{
::x::L l;
//
// This fails to compile due to syntax error
//
l = static_cast<::x ::L>(mylong);
//
// This (correctly) fails to compile since it uses
// wrong enum.
//
l = static_cast<x:: L>(mylong);
}
}
}

int main()
{
long l = 1;
x::x::foo(l);
return 0;
}

When compiled with g++ (3.3.2) and Sun's CC (5.5 Patch 113817-03), both
report a syntax error when using a globally scoped type:

% CC foo.cpp
"foo.cpp", line 19: Error: "<" expected instead of "<:".
"foo.cpp", line 19: Error: Type name expected instead of "<:".
"foo.cpp", line 19: Error: Expected an expression.
"foo.cpp", line 24: Error: Cannot assign x::x::L to x::L.

% g++ foo.cpp
foo.cpp: In function `void x::x::foo(long int)':
foo.cpp:19: error: parse error before `[' token
foo.cpp:24: error: cannot convert `x::x::L' to `x::L' in assignment

I had assumed that globally scoped types could be used in static_cast<>,
specifically to resolve scope issues. Is this a bug in the compiler? If
not, what would be the best workaround?

Otto

--
Otto Lind Kabira Technologies (http://www.kabira.com)
ot**@olcs.com 12193 285th street, Lindstrom, MN 55045
Jul 22 '05 #1
4 1881
On 05 Mar 2004 19:43:14 GMT, ot**@olcs.com (Otto Lind) wrote:
The following program shows the problem:

#include <stdio.h>

namespace x
{
enum L { One = 1, Two = 2 };
namespace x
{
enum L { Three = 3, Four = 4 };
void foo(long mylong)
{
::x::L l;
//
// This fails to compile due to syntax error
//
l = static_cast<::x ::L>(mylong);
Looks like <: is a diagraph equivalent of '[', believe it or not (I didn't
know that...). Put a space after the <.
//
// This (correctly) fails to compile since it uses
// wrong enum.
//
l = static_cast<x:: L>(mylong);

right.
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Otto Lind wrote:
l = static_cast<::x ::L>(mylong); When compiled with g++ (3.3.2) and Sun's CC (5.5 Patch 113817-03), both
report a syntax error when using a globally scoped type:

"foo.cpp", line 19: Error: "<" expected instead of "<:".


Your problem is not the same as the problem you think it is; `<:' is the
digraph for `[' (in C, at least, and I would think that this is the same
in C++), so you do, indeed, have a syntax error.

Try this, instead:

l = static_cast< ::x::L >(mylong);

--
++acr@,ka"
Jul 22 '05 #3
On Fri, 05 Mar 2004 20:08:02 GMT, Leor Zolman <le**@bdsoft.co m> wrote:
Looks like <: is a diagraph equivalent of '[', believe it or not (I didn't
know that...). Put a space after the <.

And yes, I /do/ know how to spell "digraph". That was Freudian slip of the
fingers.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4

"Sam Dennis" <sa*@malfunctio n.screaming.net > a écrit dans le message de
news:sl******** ********@ID-227112.user.uni-berlin.de...
Otto Lind wrote:
l = static_cast<::x ::L>(mylong);

When compiled with g++ (3.3.2) and Sun's CC (5.5 Patch 113817-03), both
report a syntax error when using a globally scoped type:

"foo.cpp", line 19: Error: "<" expected instead of "<:".


Your problem is not the same as the problem you think it is; `<:' is the
digraph for `[' (in C, at least, and I would think that this is the same
in C++), so you do, indeed, have a syntax error.

Try this, instead:

l = static_cast< ::x::L >(mylong);

--


I have proposed in comp.std.c that WE FORGET this digraph/trigraph
nonsense...
I can't understand that C++ needs to be bug compatible with C.

How many "incredible " bugs we will endure?

In C this is less of a problem than in C++, where <: is much more common!

jacob
Jul 22 '05 #5

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

Similar topics

11
5125
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an...
26
2521
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void...
3
1907
by: shrishjain | last post by:
Hi All, Do people frequently use static_cast, const_cast etc in industry?.. I only saw them in books, and never in real code.. Shrish
19
3708
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the other.
2
12813
by: Dave Booker | last post by:
How are complex data types saved and restored when used as User-scoped Application Settings? For an example of what I'm trying to do that does not work, take the following: A project has a user-scoped Settings object called "TestStrings" which is of type "StringDictionary". The following code is run: if (Settings.Default.TestStrings ==...
11
5642
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using a single interface: void setInfo(double); // store info double info(); // retrieve info and use
9
3162
by: Vincent RICHOMME | last post by:
Is there any reason to use static_cast instead of old C syntax ? Let's say I declare GLfloat test = static_cast<GLfloat>(x); or GLfloat test = (GLfloat) x;
5
3874
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; //...
3
367
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; // Compilation error , OK i= static_cast<int *>(d); // Compilation error. OK
0
7579
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...
0
7877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7943
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...
1
5479
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...
0
5204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
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...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
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

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.