This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Strange g++ behaviour?



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);
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]