473,847 Members | 1,440 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 1895
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
5164
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 online site that can explain it to me?
26
2568
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 foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
3
1932
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
3775
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
12837
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 == null) Settings.Default.TestStrings = new StringDictionary();...
11
5685
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
3184
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
3923
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; // <--- this
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
9732
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
10987
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10709
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9486
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
7885
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
7058
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5723
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
5914
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4125
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.