473,399 Members | 2,159 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,399 software developers and data experts.

A problem with typedef

the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

class Screen
{
public:
typedef int DOUBLE;
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?

Dec 22 '06 #1
13 1823
On 22 Dec 2006 06:13:40 -0800, "mi*********@gmail.com"
<mi*********@gmail.comwrote in comp.lang.c++:
the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;
Why?
class Screen
{
public:
typedef int DOUBLE;
Why?
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?
What do you mean by "give an error"? A compiler is allowed to issue
any sort of messages it wants to, as long as it issues diagnostics
required by the standard. Does it produce an executable? If it does,
it is conforming to the standard in this respect. There is no
requirement that it do so quietly.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 22 '06 #2
kk
I think VC compiler should give an error, but it may not. Because you
have typedef DOUBLE in defferent scope, one in global and other one in
class.

mi*********@gmail.com wrote:
the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

class Screen
{
public:
typedef int DOUBLE;
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?
Dec 22 '06 #3
there is no why. I just read the C++ Primer 4th. In Page 448 ,there
are something about the typedef in global scope and in class scope.I
just have a try about the book's example code in the popular compilers.

BTW: if the typedef in class is written behind the "DOUBLE OK", both
compilers give OK.
there is no compile error.But if the typedef is written before "DOUBLE
OK", the mingw give a compile error.

And whatever behind or before , the Intel CPP compiler , VC7 and Comeau
all give OK!
"Jack Klein дµÀ£º
"
On 22 Dec 2006 06:13:40 -0800, "mi*********@gmail.com"
<mi*********@gmail.comwrote in comp.lang.c++:
the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

Why?
>
class Screen
{
public:
typedef int DOUBLE;

Why?
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?

What do you mean by "give an error"? A compiler is allowed to issue
any sort of messages it wants to, as long as it issues diagnostics
required by the standard. Does it produce an executable? If it does,
it is conforming to the standard in this respect. There is no
requirement that it do so quietly.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 22 '06 #4
mi*********@gmail.com wrote:
the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

class Screen
{
public:
typedef int DOUBLE;
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?
As far as I can tell, your code is correct, There are two typedefs of
DOUBLE, but that's OK. One is ::DOUBLE, and the other is Screen::DOUBLE.

Now as to why you'd want to call an int a DOUBLE, that's another matter.

For what it's worth, Comeau online likes it.

Dec 22 '06 #5
red floyd wrote:
>
Now as to why you'd want to call an int a DOUBLE, that's another matter.
To experiment with name scoping and typedefs.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 22 '06 #6
Pete Becker wrote:
red floyd wrote:
>>
Now as to why you'd want to call an int a DOUBLE, that's another matter.

To experiment with name scoping and typedefs.
I wasn't arguing with the name scoping part. I was commenting on the
naming conventions.

Dec 22 '06 #7
red floyd wrote:
Pete Becker wrote:
>red floyd wrote:
>>>
Now as to why you'd want to call an int a DOUBLE, that's another matter.

To experiment with name scoping and typedefs.

I wasn't arguing with the name scoping part. I was commenting on the
naming conventions.
Naming conventions aren't important when you're doing quick and dirty
experiments.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 22 '06 #8
Pete Becker wrote:
red floyd wrote:
>Pete Becker wrote:
>>red floyd wrote:

Now as to why you'd want to call an int a DOUBLE, that's another
matter.
To experiment with name scoping and typedefs.

I wasn't arguing with the name scoping part. I was commenting on the
naming conventions.

Naming conventions aren't important when you're doing quick and dirty
experiments.
Well, if we are in picky mode, then let me point out that
perhaps that's why red floyd wrote "that's another matter".

- J.
Dec 22 '06 #9
Jacek Dziedzic wrote:
Pete Becker wrote:
>red floyd wrote:
>>Pete Becker wrote:
red floyd wrote:
>
Now as to why you'd want to call an int a DOUBLE, that's another
matter.
>

To experiment with name scoping and typedefs.
I wasn't arguing with the name scoping part. I was commenting on the
naming conventions.

Naming conventions aren't important when you're doing quick and dirty
experiments.

Well, if we are in picky mode, then let me point out that
perhaps that's why red floyd wrote "that's another matter".
Thanks, Jacek. I guess I'll have to put smileys in next time.
Dec 22 '06 #10
Pete Becker wrote:
>I wasn't arguing with the name scoping part. I was commenting on the
naming conventions.
Naming conventions aren't important when you're doing quick and dirty
experiments.
They are important when doing experiments in public forums, to avoid this
messages.

--
Salu2
Dec 22 '06 #11
mi*********@gmail.com wrote:
the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

class Screen
{
public:
typedef int DOUBLE;
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?
Neither compiler is behaving incorrectly according to the C++ Standard.

Since the name DOUBLE evaluates first as a typedef for double in the
context of its declaration but evaluates as a typedef for int when
evaluated in the context of the completed Screen class - the program is
ill-formed. However - even though the program is ill-formed, the
Standard states that a diagnostic is not required for this type of
error. So whether the compiler informs you that the program is wrong,
is a decision left up to whoever wrote the compiler.

Greg

Dec 23 '06 #12
Greg wrote:
mi*********@gmail.com wrote:
>the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

class Screen
{
public:
typedef int DOUBLE;
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?

Neither compiler is behaving incorrectly according to the C++ Standard.

Since the name DOUBLE evaluates first as a typedef for double in the
context of its declaration but evaluates as a typedef for int when
evaluated in the context of the completed Screen class - the program is
ill-formed. However - even though the program is ill-formed, the
Standard states that a diagnostic is not required for this type of
error. So whether the compiler informs you that the program is wrong,
is a decision left up to whoever wrote the compiler.
That doesn't sound right, but I haven't looked it up. My recollection is
that this pass-and-a-half rule was intended to invalidate changing the
type after it was used. So if the typedef for DOUBLE inside Screen
occurred after the definition of OK it would violate the rule, but not
before. I think the confusion comes from "the context of the completed
Screen class". That's not the context where the class is defined (where
DOUBLE is a synonym for double), but the context at the end of the
definition of Screen (where DOUBLE is a synonym for int). Since all the
uses of DOUBLE inside Screen saw it as int, it's okay.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 23 '06 #13
Pete Becker wrote:
Greg wrote:
mi*********@gmail.com wrote:
the code is as follows:
#include <iostream>
using namespace std;

typedef double DOUBLE;

class Screen
{
public:
typedef int DOUBLE;
DOUBLE OK;

};

int main()
{
return 0;
}

the mingw(with the gcc 3.4.2) give an error:
changes meaning of `DOUBLE' from `typedef double
but the vc7 gives OK. so which one is right?
Neither compiler is behaving incorrectly according to the C++ Standard.

Since the name DOUBLE evaluates first as a typedef for double in the
context of its declaration but evaluates as a typedef for int when
evaluated in the context of the completed Screen class - the program is
ill-formed. However - even though the program is ill-formed, the
Standard states that a diagnostic is not required for this type of
error. So whether the compiler informs you that the program is wrong,
is a decision left up to whoever wrote the compiler.

That doesn't sound right, but I haven't looked it up. My recollection is
that this pass-and-a-half rule was intended to invalidate changing the
type after it was used. So if the typedef for DOUBLE inside Screen
occurred after the definition of OK it would violate the rule, but not
before. I think the confusion comes from "the context of the completed
Screen class". That's not the context where the class is defined (where
DOUBLE is a synonym for double), but the context at the end of the
definition of Screen (where DOUBLE is a synonym for int). Since all the
uses of DOUBLE inside Screen saw it as int, it's okay.
Yes, I was thinking of a "typedef DOUBLE DOUBLE;" kind of declaration
inside the class - which while it looks harmless, is in fact an error.
The actual program in this case, while questionable in style, does
seems to be legal.

As an editorial aside, I do find the C++ language, especially its name
lookup rules (and rules for template type deduction) somewhat
complicated. At some point, one has to wonder whether compilers will be
the only ones left who can completely understand the C++ language. What
is needed I believe is a C++ compiler that can post answers to C++
questions on USENET forums, when it's not busy compiling source files,
that is.

Greg

Dec 23 '06 #14

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

Similar topics

6
by: mar00ned | last post by:
Hi, I have a written a custom allocator for STL, on the lines of default allocator as follows : template <class T> class pool_allocator { public: typedef size_t size_type;
0
by: Steven T. Hatton | last post by:
I copied what I believe is a complete representation of <iosfwd> from ISO/IEC 14882:2003(E). My understanding of the rules covering typedefs, templates and forward declarations is not very solid. ...
6
by: Erica | last post by:
Hi, I am currently working on a Pascal-to-C translation, and I am getting an error that I can't seem to debug. I have a globals.h file, and here is a snippet from it: -- typedef char...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
10
by: StephQ | last post by:
I found that old post: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/3a2562c9a5f8998/15519204726d01e8?lnk=gst&q=vector+no+surprise&rnum=2#15519204726d01e8 I just erased the...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
8
by: aneuryzma | last post by:
Hello, I'm merging an OpenCV app with an Ogre3d app. I'm on a mac, I'm using xCode. When I add #include "openCVApp.h" I got the following error:
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
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
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,...
0
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...
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
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...
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.