* Is this template problem in ecgs?
@ 1998-06-23 22:55 Nicholas Lee
1998-06-24 4:06 ` Alexandre Oliva
0 siblings, 1 reply; 6+ messages in thread
From: Nicholas Lee @ 1998-06-23 22:55 UTC (permalink / raw)
To: egcs
---8<---
#include <iostream>
template<typename T>
inline
T&
min(const T& x, const T& y)
{
return (x<y)? x:y;
}
int
main()
{
int a = 1;
const int b = 10;
cout << "a = " << a << ", b = " << b << endl;
cout << "min(a,b) = " << min(a,b) << endl;
min(a,b) = 20;
cout << "a = " << a << ", b = " << b << endl;
cout << "min(a,b) = " << min(a,b) << endl;
min(a,b) = 30;
cout << "a = " << a << ", b = " << b << endl;
cout << "min(a,b) = " << min(a,b) << endl;
}
/*
10:18am> g++ -Wall test2.cc -otest2
10:18am>./test2
a = 1, b = 10
min(a,b) = 1
a = 20, b = 10
min(a,b) = 10
a = 20, b = 10
min(a,b) = 10
10:18am> gcc -v
gcc version egcs-2.90.29 980515 (egcs-1.0.3 release)
(SunOs 5.6)
=== gcc 2.8.1 version ==
10:33:30 advlab$ gcc -V 2.8.1 test2.cc -o test2 -lstdc++
10:33:38 advlab$ ./test2
a = 1, b = 10
min(a,b) = 1
a = 20, b = 10
min(a,b) = 10
a = 20, b = 10
min(a,b) = 10
==== SGI Version ==
Slightly modified about to
<#include <iostreams>
>#include <iostreams.h>
<template <typename T>
>template <class T>
===
10:21am> uname
IRIX64
10:19am> CC test2.cc -o test2sgi
"test2.cc", line 9: error(3514): initial value of reference has excess
const/volatile qualifiers
return (x<y)? x:y;
^
detected during instantiation of
"int & min(const int &, const int &)" at line 18
1 error detected in the compilation of "test2.cc".
*/
---8<----
--
"I reserve the right to contradict myself."
Nicholas Lee (Li Peng Ming) n.lee with math.auckland.ac in nz
Auckland University, New Zealand
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Is this template problem in ecgs?
1998-06-23 22:55 Is this template problem in ecgs? Nicholas Lee
@ 1998-06-24 4:06 ` Alexandre Oliva
1998-06-24 4:14 ` Nicholas Lee
0 siblings, 1 reply; 6+ messages in thread
From: Alexandre Oliva @ 1998-06-24 4:06 UTC (permalink / raw)
To: Nicholas Lee; +Cc: egcs
Nicholas Lee <nlee011@math.auckland.ac.nz> writes:
> template<typename T>
> inline T& min(const T& x, const T& y) { return (x<y)? x:y; }
Not a template problem, actually. It would also compile if you
replaced `template<typename T>' with `typedef int T;'. It seems that
gcc disregards CV-qualification if the return value is a ?:
expression, but I haven't investigated the problem very much.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Is this template problem in ecgs?
1998-06-24 4:06 ` Alexandre Oliva
@ 1998-06-24 4:14 ` Nicholas Lee
1998-06-24 10:08 ` Alexandre Oliva
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Nicholas Lee @ 1998-06-24 4:14 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: egcs
On 24 Jun 1998, Alexandre Oliva wrote:
> Nicholas Lee <nlee011@math.auckland.ac.nz> writes:
>
> > template<typename T>
> > inline T& min(const T& x, const T& y) { return (x<y)? x:y; }
>
> Not a template problem, actually. It would also compile if you
> replaced `template<typename T>' with `typedef int T;'. It seems that
> gcc disregards CV-qualification if the return value is a ?:
> expression, but I haven't investigated the problem very much.
What is the correct behaviour?
I would assume since x and y are declared as const T&, that the compiler
should issue a warning that there was a attempt to discard constness without
a const_cast.
--
"I reserve the right to contradict myself."
Nicholas Lee (Li Peng Ming) n.lee with math.auckland.ac in nz
Auckland University, New Zealand
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Is this template problem in ecgs?
1998-06-24 4:14 ` Nicholas Lee
@ 1998-06-24 10:08 ` Alexandre Oliva
1998-06-24 18:13 ` Joe Buck
[not found] ` <199806250024.RAA14322.cygnus.egcs@atrus.synopsys.com>
2 siblings, 0 replies; 6+ messages in thread
From: Alexandre Oliva @ 1998-06-24 10:08 UTC (permalink / raw)
To: Nicholas Lee; +Cc: egcs
Nicholas Lee <nlee011@math.auckland.ac.nz> writes:
> On 24 Jun 1998, Alexandre Oliva wrote:
>> Nicholas Lee <nlee011@math.auckland.ac.nz> writes:
>>
>> > template<typename T>
>> > inline T& min(const T& x, const T& y) { return (x<y)? x:y; }
>>
>> It seems that gcc disregards CV-qualification if the return value
>> is a ?: expression
> I would assume since x and y are declared as const T&, that the compiler
> should issue a warning
You're right, I meant to say that, but I wasn't clear at all. gcc and
egcs are wrong, they violate const-safety without diagnostic.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Is this template problem in ecgs?
1998-06-24 4:14 ` Nicholas Lee
1998-06-24 10:08 ` Alexandre Oliva
@ 1998-06-24 18:13 ` Joe Buck
[not found] ` <199806250024.RAA14322.cygnus.egcs@atrus.synopsys.com>
2 siblings, 0 replies; 6+ messages in thread
From: Joe Buck @ 1998-06-24 18:13 UTC (permalink / raw)
To: Nicholas Lee; +Cc: oliva, egcs
> I would assume since x and y are declared as const T&, that the compiler
> should issue a warning that there was a attempt to discard constness without
> a const_cast.
const violations should, IMHO, be errors, not warnings. They certainly
are with every other C++ compiler I've used.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Is this template problem in ecgs?
[not found] ` <199806250024.RAA14322.cygnus.egcs@atrus.synopsys.com>
@ 1998-06-26 17:02 ` Nathan Myers
0 siblings, 0 replies; 6+ messages in thread
From: Nathan Myers @ 1998-06-26 17:02 UTC (permalink / raw)
To: egcs
Joe Buck wrote:
>
> > I would assume since x and y are declared as const T&, that the compiler
> > should issue a warning that there was a attempt to discard constness without
> > a const_cast.
>
> const violations should, IMHO, be errors, not warnings. They certainly
> are with every other C++ compiler I've used.
Agree, at least for C++.
Nathan Myers
ncm@cantrip.org
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~1998-06-26 17:02 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-23 22:55 Is this template problem in ecgs? Nicholas Lee
1998-06-24 4:06 ` Alexandre Oliva
1998-06-24 4:14 ` Nicholas Lee
1998-06-24 10:08 ` Alexandre Oliva
1998-06-24 18:13 ` Joe Buck
[not found] ` <199806250024.RAA14322.cygnus.egcs@atrus.synopsys.com>
1998-06-26 17:02 ` Nathan Myers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).