This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Strange g++ behaviour?
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Strange g++ behaviour?
- From: David Howells <David dot Howells at nexor dot co dot uk>
- Date: Thu, 27 Jul 2000 12:03:56 +0100
Compiling the attached code fragment with g++ gives the following warning:
g++query.cpp: In function `void test(comptr_t<int>)':
g++query.cpp:20: warning: choosing `comptr_t<int>::operator int *()' over `comptr_t<int>::operator bool() const'
g++query.cpp:20: warning: for conversion from `pint' to `bool'
g++query.cpp:20: warning: because conversion sequence for the argument is better
g++query.cpp:20: warning: choosing `comptr_t<int>::operator int *()' over `comptr_t<int>::operator const int *() const'
g++query.cpp:20: warning: for conversion from `pint' to `bool'
g++query.cpp:20: warning: because conversion sequence for the argument is better
However, compiling with "g++ -DFOO" or compiling with MSVC++ with or without
the command line option does not give a warning!
Why is g++ reluctant to use the "operator bool() const" method? And does it
really need to give the warning message?
Compiler versions tested upon:
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.95.2/specs
gcc version 2.95.2 19991024 (release)
Reading specs from /opt/gcc/bin/../lib/gcc-lib/i386-pc-linux-gnu/2.96/specs
gcc version 2.96 20000719 (experimental)
Thanks,
David Howells
=============
template<class _I> class comptr_t
{
_I *_obj;
public:
comptr_t(_I *obj) : _obj(obj) {}
operator _I*() { return (_I*)_obj; }
operator const _I*() const { return (const _I*)_obj; }
operator bool () const { return !!_obj; }
#ifdef FOO
operator bool () { return !!_obj; }
#endif
};
typedef comptr_t<int> pint;
void test(pint p)
{
if (p)
test(p);
}