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: 26
Joined: Tue Aug 19, 2014 10:46 am

OgreSharedPtr implicit conversion to bool

Post by longer » Thu Nov 30, 2017 11:07 am

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.
}
0 x

User avatar
Ybalrid
Halfling
Posts: 41
Joined: Thu Jul 10, 2014 6:52 pm
Location: France
x 3
Contact:

Re: OgreSharedPtr implicit conversion to bool

Post by Ybalrid » Mon Jan 01, 2018 9:37 pm

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.
1 x

paroj
OGRE Team Member
OGRE Team Member
Posts: 462
Joined: Sun Mar 30, 2014 2:51 pm
x 55
Contact:

Re: OgreSharedPtr implicit conversion to bool

Post by paroj » Sun Jan 07, 2018 7:11 pm

this is about a (stupid) MSVC warning:
https://msdn.microsoft.com/en-us/librar ... s.90).aspx
0 x

User avatar
Ybalrid
Halfling
Posts: 41
Joined: Thu Jul 10, 2014 6:52 pm
Location: France
x 3
Contact:

Re: OgreSharedPtr implicit conversion to bool

Post by Ybalrid » Sun Jan 07, 2018 7:57 pm

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?
0 x

Post Reply