Topic: Is this well-formed?
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/04/27 Raw View
David R Tribble <dtribble@technologist.com> wrote
>>> Gee, wouldn't it be nice if C++ had a 'null' keyword?
Christopher Eltschka wrote:
>...
> However, it doesn't really need a keyword, the following
> should work on a conforming compiler:
>...
>
> class null
> {
> public:
> template<class T> operator T*() { return 0; }
> static null get() { return null(); }
> private:
> null();
> null(null const&);
> };
>
> #define null (null::get())
Nice little addition, that rvalue thing.
I know there's no chance of getting this widely accepted in any
"standard" sense, but wouldn't this be fairly easy to add to g++?
This would help establish "existing practice" for the next
go-around on std C++.
I saw somewhere that one version of the compiler used '__NULL__'
as a real built-in null constant (which the header files
conveniently #defined NULL to be).
I must also re-iterate my opinion on adding 'null' to C++, that
this should not in any way replace the use of 0, at least for
several years. Eventually, if enough people haved switched
from 0 to 'null', we can argue about getting rid of "0 as null";
but this won't happen for a while yet, if ever.
-- David R. Tribble, dtribble@technologist.com --
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/04/22 Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> void f(int) { cout << "foo" << endl; }
> void f(char*) { cout << "bar" << endl; }
[...]
> f(NULL); // prints foo again
Is that really guaranteed? I'd think
#define NULL 0L
would be a conforming definition of NULL. Then, calling f(int) would
require an integral conversion, and calling f(char*) would require a
null-pointer conversion, which both have conversion rank. In that
case, the call could be ambiguous. Under no circumstances, f(char*) is
called.
Regards,
Martin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Peter Dimov" <pdimov@techno-link.com>
Date: 1999/04/23 Raw View
Andrei Alexandrescu <andrewalex@hotmail.com> wrote in message
news:QQglev00422.199904161429@relay7.UU.NET...
> David R Tribble <dtribble@technologist.com> wrote in message
> news:371655EE.948EA1E6@technologist.com...
> > Gee, wouldn't it be nice if C++ had a 'null' keyword?
>
> It has. It's spelled "0". I don't see what difference a null keyword would
> make.
It would make a difference if it had a unique type that is not 'const int'.
> As of the original poster's code: I think it's correct. Template function
> matching is considered prior doing any conversion. It will fail. Then
> regular functions will be considered, with conversions and all. Therefore
> f(int *) will kick in.
This was my reasoning too. Thanks to all that provided feedback on the
issue.
--
Peter Dimov
Multi Media Ltd.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: paulp.removethis@ccnet.com (Paul)
Date: 1999/04/23 Raw View
>> Anyway, since the compiler can not convert
>> '0' to type 'int*',
>C++-Standard 4.10/1 [conv.ptr]:
>A null pointer constant is an integral constant
>expression [...] that evaluates to zero. A null pointer
>constant can be converted to a pointer type,
>the result is the null pointer value of that type ...
I did not say that the language doesn't allow you to
convert '0' to type 'int*'. I said the VC++ compiler can
not (always) convert '0' to 'int*'. Maybe I should have
been more clear. I see this kind of error all the time
with VC++.
Paul
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 1999/04/22 Raw View
David R Tribble <dtribble@technologist.com> wrote in message
news:371655EE.948EA1E6@technologist.com...
> Gee, wouldn't it be nice if C++ had a 'null' keyword?
It has. It's spelled "0". I don't see what difference a null keyword would
make.
As of the original poster's code: I think it's correct. Template function
matching is considered prior doing any conversion. It will fail. Then
regular functions will be considered, with conversions and all. Therefore
f(int *) will kick in.
Andrei
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James.Kanze@dresdner-bank.com
Date: 1999/04/22 Raw View
In article <QQglev00422.199904161429@relay7.UU.NET>,
"Andrei Alexandrescu" <andrewalex@hotmail.com> wrote:
> David R Tribble <dtribble@technologist.com> wrote in message
> news:371655EE.948EA1E6@technologist.com...
> > Gee, wouldn't it be nice if C++ had a 'null' keyword?
>
> It has. It's spelled "0". I don't see what difference a null keyword would
> make.
void f( int ) ;
void f( void* ) ;
void
g()
{
f( 0 ) ; // calls f( int )
f( null ) ; // should call f( void* )
}
The question isn't whether C++ needs a null keyword, it is rather
whether it is possible to add one without either watering down to the
point of making it useless, or breaking too much existing code. (I
happen to think it possible, but it is a question on which reasonable
people can differ. At to whether C++ would be better off with a real,
working null keyword, on the other hand, there really isn't much
discussion possible.)
--
James Kanze mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orientie objet/
Beratung in objekt orientierter Datenverarbeitung
Ziegelh|ttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86 27
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/04/22 Raw View
Andrei Alexandrescu wrote:
>
> David R Tribble <dtribble@technologist.com> wrote in message
> news:371655EE.948EA1E6@technologist.com...
> > Gee, wouldn't it be nice if C++ had a 'null' keyword?
>
> It has. It's spelled "0". I don't see what difference a null keyword would
> make.
void f(int) { cout << "foo" << endl; }
void f(char*) { cout << "bar" << endl; }
void g(int);
int main()
{
f(0); // prints foo
f(NULL); // prints foo again
f(null); // would print bar
g(0); // compiles
g(NULL); // compiles again
g(null); // would be an error
}
However, it doesn't really need a keyword, the following
should work on a conforming compiler:
struct
{
template<class T> operator T*() { return 0; }
private:
void operator&();
} null;
If you don't mind a macro (after all, a keyword wouldn't respect
scope as well), you can even make it a real rvalue:
class null
{
public:
template<class T> operator T*() { return 0; }
static null get() { return null(); }
private:
null();
null(null const&);
};
#define null (null::get())
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: hinnant@_anti-spam_metrowerks.com (Howard Hinnant)
Date: 1999/04/21 Raw View
In article <7ffmrv$j09$1@nnrp1.dejanews.com>,
James.Kanze@dresdner-bank.com wrote:
> $1@weber.techno-link.com>,
> "Peter Dimov" <pdimov@techno-link.com> wrote:
> > Is the following program well-formed?
> >
> > template<class U> void f(U*)
> > {
> > }
> >
> > void f(int*)
> > {
> > }
> >
> > int main()
> > {
> > f(0);
> > return 0;
> > }
> According to 13.3.3., a non-template function is a better match than a
> template function, so the non template f(int*) should be preferred. I
> suspect that Microsoft is trying to instantiate the template in order to
> create the list of all viable functions, and failing, because there are
> an infinite number of instantiations which could work.
>
> I don't think Microsoft is the only compiler to have this error. If you
> think about it, it isn't simple; if an instantiation of the template
> were to give f(int), this would be preferred over the non-template
> function. (I also have vague memories that at one time, if a
> non-template function could be called, template functions weren't even
> considered. But I could be wrong about that.)
Metrowerks CodeWarrior gets it right and runs on Windows.
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James.Kanze@dresdner-bank.com
Date: 1999/04/19 Raw View
$1@weber.techno-link.com>,
"Peter Dimov" <pdimov@techno-link.com> wrote:
> Is the following program well-formed?
>
> template<class U> void f(U*)
> {
> }
>
> void f(int*)
> {
> }
>
> int main()
> {
> f(0);
> return 0;
> }
I think so.
> Note: MSVC6SP2 rejects it:
>
> error C2784: 'void __cdecl f(U *)' : could not deduce template argument for
> ' *' from 'const int'
According to 13.3.3., a non-template function is a better match than a
template function, so the non template f(int*) should be preferred. I
suspect that Microsoft is trying to instantiate the template in order to
create the list of all viable functions, and failing, because there are
an infinite number of instantiations which could work.
I don't think Microsoft is the only compiler to have this error. If you
think about it, it isn't simple; if an instantiation of the template
were to give f(int), this would be preferred over the non-template
function. (I also have vague memories that at one time, if a
non-template function could be called, template functions weren't even
considered. But I could be wrong about that.)
--
James Kanze mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orientie objet/
Beratung in objekt orientierter Datenverarbeitung
Ziegelh|ttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86 27
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: paulp@ccnet.com (Paul Pedriana)
Date: 1999/04/16 Raw View
Small problem: the templates take 'int*' but your
program is using 'int'. Are you sure this is what
you intended?
Anyway, since the compiler can not convert
'0' to type 'int*', the problem is not that the two
versions of f conflict with each other, but that
neither can work. The compiler should say something
to the effect that the conversion is impossible.
However, it is a figment of how the MSVC++ compiler
works that you get a misleading error such as the
one you got.
For a description of this same problem outside the
standard itself, see:
www.cntc.com/resources/gotw049a.html
Paul
>template<class U> void f(U*)
>{
>}
>
>void f(int*)
>{
>}
>
>int main()
>{
> f(0);
> return 0;
>}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Peter Dimov" <pdimov@techno-link.com>
Date: 1999/04/18 Raw View
Paul Pedriana <paulp@ccnet.com> wrote in message
news:3730ea5e.2688074620@news.ccnet.com...
> Small problem: the templates take 'int*' but your
> program is using 'int'. Are you sure this is what
> you intended?
Absolutely. My program is using the manifest constant
0, which is not an ordinary 'int'.
> Anyway, since the compiler can not convert
> '0' to type 'int*', the problem is not that the two
It sure can.
> versions of f conflict with each other, but that
> neither can work. The compiler should say something
> to the effect that the conversion is impossible.
> However, it is a figment of how the MSVC++ compiler
> works that you get a misleading error such as the
> one you got.
The error is not misleading, in fact. It clearly shows what
I think is a bug in MSVC++: the compiler considers f(int*)
equivalent to an explicit specialization of f<U>(U*).
See also the following example:
#include <iostream>
template<class U> void f(U*)
{
std::cout << 1 << std::endl;
}
int main()
{
int i;
f(&i);
return 0;
}
void f(int*)
{
std::cout << 2 << std::endl;
}
What should this program print? MSVC++ says '2'.
--
Peter Dimov
Multi Media Ltd.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Joerg Barfurth <jbarfurth@vossnet.de>
Date: 1999/04/18 Raw View
Paul Pedriana wrote:
> =
> Small problem: the templates take 'int*' but your
> program is using 'int'. Are you sure this is what
> you intended?
> =
> Anyway, since the compiler can not convert
> '0' to type 'int*', =
C++-Standard 4.10/1 [conv.ptr]:
A null pointer constant is an integral constant expression [...] that
evaluates to zero. A null pointer constant can be converted to a pointer =
type,
the result is the null pointer value of that type ...
-- J=F6rg
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Peter Dimov" <pdimov@techno-link.com>
Date: 1999/04/15 Raw View
Is the following program well-formed?
template<class U> void f(U*)
{
}
void f(int*)
{
}
int main()
{
f(0);
return 0;
}
Note: MSVC6SP2 rejects it:
error C2784: 'void __cdecl f(U *)' : could not deduce template argument for
' *' from 'const int'
--
Peter Dimov
Multi Media Ltd.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fvali@biotrack.com
Date: 1999/04/16 Raw View
I believe it is well-formed. f(int*) should be resolved as
the best viable function according to the standard.
During template argument deduction, standard conversions are not
performed or considered. (They are if explicitly specified though, i.e
f<int>(0) will resolve to the template version)
-hope that helps
-fais
In article <7f1nnv$6c$1@weber.techno-link.com>,
"Peter Dimov" <pdimov@techno-link.com> wrote:
> Is the following program well-formed?
>
> template<class U> void f(U*)
> {
> }
>
> void f(int*)
> {
> }
>
> int main()
> {
> f(0);
> return 0;
> }
>
> Note: MSVC6SP2 rejects it:
>
> error C2784: 'void __cdecl f(U *)' : could not deduce template argument for
> ' *' from 'const int'
>
> --
> Peter Dimov
> Multi Media Ltd.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/04/16 Raw View
"Peter Dimov" <pdimov@techno-link.com> writes:
> Is the following program well-formed?
I'd say it is. In template argument deduction, the 0 argument would
have the type 'int', so template argument deduction fails. In turn,
the template is not a viable candidate, and the non-template function
will be called.
Regards,
Martin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/04/16 Raw View
Peter Dimov wrote:
>
> Is the following program well-formed?
>
> template<class U> void f(U*)
> {
> }
>
> void f(int*) // original
> {
> }
>
> int main()
> {
> f(0);
> return 0;
> }
>
> Note: MSVC6SP2 rejects it:
>
> error C2784: 'void __cdecl f(U *)' : could not deduce template
> argument for ' *' from 'const int'
Perhaps you should make the 2nd definition of f() an explicit
template specialization:
template<> void f(int *) // revised
{
}
This might, I think, lead to an ambiguous instantiation, though.
What type of pointer is 0 anyway? In other words, you might need
an explicit cast of the 0 to embue it with a type other than
'const int'.
Gee, wouldn't it be nice if C++ had a 'null' keyword?
-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]