473,770 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conflicting behavior of MS and Borland

I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo

While in case of VC7.0

In const foo
In non-const foo

I could not find much in TC++PL
--
Imanpreet Singh Arora
Jul 22 '05 #1
10 1484
Minti wrote:
I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo
gcc 3.4.0 and VC7.1 also says this.

While in case of VC7.0

In const foo
In non-const foo
I suspect it's a bug in VC7.0.

I could not find much in TC++PL


Jul 22 '05 #2
Gianni Mariani wrote:
Minti wrote:
I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo

gcc 3.4.0 and VC7.1 also says this.


Not the VC7.1 I have. It still says the same as VC7.0. What version of
the compiler do you have? Mine says

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Do I need to update it somehow?

While in case of VC7.0

In const foo
In non-const foo

I suspect it's a bug in VC7.0.

Jul 22 '05 #3
Victor Bazarov wrote:
Not the VC7.1 I have. It still says the same as VC7.0. What version of
the compiler do you have? Mine says

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Do I need to update it somehow?


My compiler says:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3052 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

I.e. it's a bit older than yours. If I turn Microsoft's extension off
(/Za), I get the expected:

In const foo
In const foo

Just my $0.05

Br,
Peter
Jul 22 '05 #4
On Thu, 20 May 2004 12:45:06 -0400, Victor Bazarov
<v.********@com Acast.net> wrote:
Gianni Mariani wrote:
Minti wrote:
I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo

gcc 3.4.0 and VC7.1 also says this.


Not the VC7.1 I have. It still says the same as VC7.0. What version of
the compiler do you have? Mine says

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Do I need to update it somehow?

While in case of VC7.0

In const foo
In non-const foo

I suspect it's a bug in VC7.0.


I suspect it is a compatibility "feature" of 7.0 (don't ask me what it is
supposed to be compatible /with/, since VC6 does it "right" by default...)

Using both MSVC 7.0 and 7.1 with /Za, I get both const. Without /Za, I get
the one non-const.

A revealing diagnostic comes out when you compile it with Comeau, though:

test.cpp(10): warning: type qualifier is meaningless on cast type
foo( (const int ) x );

This makes sense, as casts always produce a temporary that is not
assignable, I think. So clearly we have a case of the parameter needing to
be a reference-to-const so that it can bind to a temporary.

None of this explains why VC7.x does it the other way by default. MSVC
works in mysterious ways when it comes to its default compatibility mode...
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5
Leor Zolman <le**@bdsoft.co m> wrote in message news:<ho******* *************** **********@4ax. com>...
On Thu, 20 May 2004 12:45:06 -0400, Victor Bazarov
<v.********@com Acast.net> wrote:
Gianni Mariani wrote:
Minti wrote:

I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo
gcc 3.4.0 and VC7.1 also says this.
Not the VC7.1 I have. It still says the same as VC7.0. What version of
the compiler do you have? Mine says

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Do I need to update it somehow?


While in case of VC7.0

In const foo
In non-const foo
I suspect it's a bug in VC7.0.


I suspect it is a compatibility "feature" of 7.0 (don't ask me what it is
supposed to be compatible /with/, since VC6 does it "right" by default...)

Using both MSVC 7.0 and 7.1 with /Za, I get both const. Without /Za, I get
the one non-const.

A revealing diagnostic comes out when you compile it with Comeau, though:

test.cpp(10): warning: type qualifier is meaningless on cast type
foo( (const int ) x );

This makes sense, as casts always produce a temporary that is not
assignable, I think.


I really don't think that is indeed the case, if the cast can be done
at compile time
like in

const int x = 5.5;

then there is no need to produce a temporary.

I am not sure what you mean by "not assignable" do you mean that they
can't be an lvalue.

So clearly we have a case of the parameter needing to
be a reference-to-const so that it can bind to a temporary. None of this explains why VC7.x does it the other way by default. MSVC
works in mysterious ways when it comes to its default compatibility mode...
-leor


--
Imanpreet Singh Arora
Jul 22 '05 #6
On 20 May 2004 14:21:46 -0700, mi************@ yahoo.com (Minti) wrote:
A revealing diagnostic comes out when you compile it with Comeau, though:

test.cpp(10): warning: type qualifier is meaningless on cast type
foo( (const int ) x );

This makes sense, as casts always produce a temporary that is not
assignable, I think.
I really don't think that is indeed the case, if the cast can be done
at compile time
like in

const int x = 5.5;

I don't consider that a "cast"; that's just a conversion. By "cast", I mean
either
(type) expression
type (expression)
or new-style_cast<type >(expression)

and I'm saying you can't make the result of one of those be the left
operand of an assignment operator (or ++/--).
then there is no need to produce a temporary.
Right, because it isn't a "cast".

I am not sure what you mean by "not assignable" do you mean that they
can't be an lvalue.


That's probably better wording, or "not modifiable" perhaps. I began trying
to elaborate on this based on what the Standard says; I've been bouncing
around between 3.10/2, 3.10/6 and writing test programs, and finally
decided I just don't understand some of the subtleties enough to really
follow through with an analysis.

I do, however, have a pet theory as to what the default MSVC 7 behavior is
doing, even though I haven't a clue why Microsoft would find such behavior
to be useful. In the strange behavior, the call

foo ((const int) x);

results in the function call argument being bound to a reference to
non-const (the function parameter). Here's what section 3.10/6 has to say
about the result of casts:

"An expression which holds a temporary object resulting from a cast to
a nonreference type is an rvalue (this includes the explicit creation
of an object using functional notation (5.2.3))."

My suspicion is that the strange behavior is ignoring this rule, and
retaining all the lvalue-ness of the cast's operand. Thus it behaves as we
see when the operand is an lvalue, but not when it is a constant. Consider
this expansion of the original example:

#include <iostream>

void foo(int& x) {std::cout << "In non-const foo\n"; }
void foo(const int& x) { std::cout << "In const foo\n"; }

int main(void)
{
int x = 23;

foo(5);
foo(x);
foo( (const int ) x );
foo( (const int ) 5 );

return 0;
}

The output is:

In const foo
In non-const foo
In non-const foo
In const foo

The third one is the strange one, and can be explained by imagining that
the cast isn't actually producing a "new" temporary value, but rather is
passing the original operand x "as if" it had the desired type. The last
example shows that if you start with a constant (5), it doesn't magically
turn into an lvalue (which is a good thing.)

Another shot in the dark: perhaps Microsoft is interpreting 3.10/6's use of
the term "nonreferen ce" to mean that, when the argument can be bound to a
(non-const) reference in the function call, the result of the cast is no
longer a "nonreferen ce type" and thus exempt from the requirements of being
an rvalue. But if /Za is used, it battens down the hatches. I just have no
idea.

For what it's worth.
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7
Victor Bazarov wrote:
Gianni Mariani wrote:
Minti wrote:
I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo
gcc 3.4.0 and VC7.1 also says this.

Not the VC7.1 I have. It still says the same as VC7.0. What version of
the compiler do you have? Mine says

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Do I need to update it somehow?


Nope - It was my mistake ... Getting a bit punchy at 9:00AM !

While in case of VC7.0

In const foo
In non-const foo


I suspect it's a bug in VC7.0.


Jul 22 '05 #8
"Gianni Mariani" <gi*******@mari ani.ws> wrote...
Victor Bazarov wrote:
Gianni Mariani wrote:
Minti wrote:

I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo

gcc 3.4.0 and VC7.1 also says this.

Not the VC7.1 I have. It still says the same as VC7.0. What version of
the compiler do you have? Mine says

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Do I need to update it somehow?


Nope - It was my mistake ... Getting a bit punchy at 9:00AM !


Gianni, you were correct, the others suggested that "language extensions"
were turned on, and they were correct. I turned language extensions off
and got the behaviour you described. I wonder how long MSoft is going
to keep "extensions " _on_ by default...

Thanks to all for corrections.

V
Jul 22 '05 #9
Leor Zolman wrote:
On Thu, 20 May 2004 12:45:06 -0400, Victor Bazarov
<v.********@com Acast.net> wrote:

Gianni Mariani wrote:
Minti wrote:

I tried the following code on Borland C++ complier [ 5.5.1 ] and
Microsoft VC7.0 both seem to give conflicting results
void foo(const int& x) { std::cout << "In const foo\n"; }
void foo(int& x) {std::cout << "In non-const foo\n"; }

int main(void)
{
foo(5);
int x = 23;
foo( (const int ) x );
return 0;
}

In case of Borland compiler the result is

In const foo
In const foo
[Snip]

I suspect it is a compatibility "feature" of 7.0 (don't ask me what it is
supposed to be compatible /with/, since VC6 does it "right" by default...)

Using both MSVC 7.0 and 7.1 with /Za, I get both const. Without /Za, I get
the one non-const.

A revealing diagnostic comes out when you compile it with Comeau, though:

test.cpp(10): warning: type qualifier is meaningless on cast type
foo( (const int ) x );

This makes sense, as casts always produce a temporary that is not
assignable, I think. So clearly we have a case of the parameter needing to
be a reference-to-const so that it can bind to a temporary.

None of this explains why VC7.x does it the other way by default. MSVC
works in mysterious ways when it comes to its default compatibility mode...
-leor


I dont know if this sheds any light on why MS would have such a language
extension, but you can use a similar cast on an lvalue in VC7.1 with
language extensions on.

const int c = 4;
int n = 5;
volatile int v = 6;

(const int)n = 6; // Fine in VC 7.1 with extensions
(int)v = 3; // Fine in VC 7.1 with extensions

(int)c = 3; // illegal
(unsigned int)n = 7; // illegal
But still ... why?

- Jake Montgomery

Jul 22 '05 #10

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

Similar topics

0
2968
by: Kenneth Gomez | last post by:
Hello, I have tried many avenues (web search, borland website, libxml website) before deciding to post here. I'm trying to install libxml2 on windows ME to work with my Borland C++ 5 Compiler (command line). Read through the installation guides from xmlsoft.org.
17
4704
by: Ziggi | last post by:
Hi. I want to get a C++ IDE, but I dont know whether to go for Bill Gate's solution or Borland's. Could any kind folks detail the relative strength and weaknesses of both, and also tell me which you yourselves prefer. Thanks in advance. Ziggi
7
2624
by: Thiru | last post by:
I am writing an application that interacts with Oracle and Teradata. In order to create the executable, I need to link various Oracle and Teradata libraries. I found out that when I link the Oracle and Teradata libraries together, the Teradata API functions are not working properly. My assumption is that these libraries are sharing identical function names and parameter lists and hence the conflicts are causing the problem. Is my...
2
2685
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled across some wierd loop behavior. With the pasted code I receive the output that follows. I realize that the code is broken, because the inner loop fails to reset j for each iteration of the outer loop (the fix is commented out). I also know that...
22
2241
by: smartwolf agassi via DotNetMonster.com | last post by:
I'm a C# language learner. I want to know which IDE is better for C# programing, Borland C#Builder or VS.net 2003? -- Message posted via http://www.dotnetmonster.com
1
1724
by: yawnmoth | last post by:
I seem to be getting conflicting gethostbyname behavior on different servers. Before going into detail, here's the script I'm using: <? $address = $HTTP_SERVER_VARS; $rev = implode('.',array_reverse(explode('.', $address))); $lookup = "$rev.l1.spews.dnsbl.sorbs.net"; echo gethostbyname($lookup); echo '<br />';
4
3770
by: darrensjunkaccount | last post by:
Hi, I have just inherited some embedded software that was compiled with Borland 4.52. I need to either purchase that compiler, which Borland no longer appear to sell or alternatively source an upgrade/compatible compiler. Do Borland sell a newer version of the 4.52 compiler that will still allow me to compile a DOS executable? Regards, Darren
1
3781
by: dhruba.bandopadhyay | last post by:
Am wondering whether Borland C++ 4.5 or Borland Turbo C++ 1.01 supported C++ templates. If not, which other future version of BC++ or TC++ did? If BC++ 4.5 or TC++ 1.01 does support it, how much of the C++ language does it support.
7
1475
by: thamizh.veriyan | last post by:
Hi, I am new to this community. I have a doubt regarding trap representations. I read in IBM's website that something like this is legal: int main(){ int x=3; {
0
10230
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...
0
10058
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
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
9870
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...
1
7416
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
6678
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();...
1
3972
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
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
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.