OgreSharedPtr implicit conversion to bool

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
longer
Kobold
Posts: 37
Joined: Tue Aug 19, 2014 10:46 am
x 5

OgreSharedPtr implicit conversion to bool

Post by longer »

use dummy member function can do this.

Code: Select all

		struct Tester
		{
			Tester(int) {}  // No default constructor
			void dummy() {}
		};
		typedef void (Tester::*unspecified_bool_type)();
		operator unspecified_bool_type() const
		{
			return pRep == 0 ? 0 : &Tester::dummy;
		}

		//static void unspecified_bool( SharedPtr*** )
		//{
		//}

		//typedef void (*unspecified_bool_type)( SharedPtr*** );

		//operator unspecified_bool_type() const
		//{
		//    return pRep == 0 ? 0 : &unspecified_bool;
		//}
Ogre::SharedPtr a(new int);
// here not warning at msvc.
if(a)
{
// a is not empty.
}
User avatar
Ybalrid
Halfling
Posts: 89
Joined: Thu Jul 10, 2014 6:52 pm
Location: France
x 31
Contact:

Re: OgreSharedPtr implicit conversion to bool

Post by Ybalrid »

Isn't this expected behavior?

It's standard practice in C/C++ to test pointers if they are "NULL" or not.

if(pointer) is equivalent to testing if(pointer != 0). (NULL is a #define that has the value 0)

You'll get the same result with the C++ standard std::shared_ptr<> (available since C++11,) or the one from boost.
Ogre_glTF Ogre v2-1 GLTF2 loader : topic link github repo
BtOgre21 Fork of btOgre, for Ogre v2-1 : topic link github repo
OIS Current maintainer : Official repository
Annwvyn VR focused game engine using Ogre : https://github.com/Ybalrid/Annwvyn https://annwvyn.org/
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: OgreSharedPtr implicit conversion to bool

Post by paroj »

this is about a (stupid) MSVC warning:
https://msdn.microsoft.com/en-us/librar ... s.90).aspx
User avatar
Ybalrid
Halfling
Posts: 89
Joined: Thu Jul 10, 2014 6:52 pm
Location: France
x 31
Contact:

Re: OgreSharedPtr implicit conversion to bool

Post by Ybalrid »

Oh, stupid indeed. I didn't even catch this was the actual problem discussed by the OP.

Speaking of stupid :
Casting the expression to type bool will not disable the warning, which is by design
What? they really want you to force you to write "!= 0" so that the assigned result is inherently of type bool?
Ogre_glTF Ogre v2-1 GLTF2 loader : topic link github repo
BtOgre21 Fork of btOgre, for Ogre v2-1 : topic link github repo
OIS Current maintainer : Official repository
Annwvyn VR focused game engine using Ogre : https://github.com/Ybalrid/Annwvyn https://annwvyn.org/
Post Reply