'is_pointer_like' or 'is_smart_pointer' or similar, again

A while back there was discussion of detecting smart pointers. The
discussion started with Ovanes Markarian
Date: Thu, 22 Jun 2006 19:19:07 +0200 (CEST)
From: "Ovanes Markarian"
7.squirrel@www.keywallet.com> Content-Type: text/plain;charset=iso-8859-1
Hello all,
I am trying to specialize boost::is_pointer so, that it can determine if a type is a pointer wrapper (shared_ptr, auto_ptr, weak_ptr).
..... And, first off, yes, we now know that expanding is_pointer would be bad. So, instead we want 'is_pointer_like' or something like that. There were suggestions regarding detecting operator->() and/or unary operator *(). What ever came of this? I am asking, of course, because I could really use an 'is_pointer_like<>' or 'is_smart_ptr' right now. And if it is still unsolved, I've got 2 more suggestions: detecting that boost::get_pointer() (for boost::bind) has been defined, and/or just looking for some common internals of the smart_ptrs (hack!) - or adding some common internal typedef/value - ie T::is_smart_pointer. Thanks, Tony

Hi!
I solved the problem, by specializing template for every type I believe it
is a suitable smart ptr:
template<class T>
struct is_smart_ptr : boost::mpl::false_
{};
template<class T>
struct is_smart_ptr

Just a small addition. You can also specialize the template in another file,
that is seen in the translation unit. But anyway you need to specialize it.
I hope with a new C++09 standard (it is still pretty far away), it will be
possible to inspect if a class has a specific member function (using typeof
or decltype operators). Currently it is only possible with has_xxx macros.
Best Regards,
Ovanes
_____
From: Ovanes Markarian [mailto:om_boost@keywallet.com]
Sent: Saturday, January 20, 2007 12:39 PM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] 'is_pointer_like' or 'is_smart_pointer'
orsimilar, again
Hi!
I solved the problem, by specializing template for every type I believe it
is a suitable smart ptr:
template<class T>
struct is_smart_ptr : boost::mpl::false_
{};
template<class T>
struct is_smart_ptr

Ovanes Markarian wrote:
Just a small addition. You can also specialize the template in another file, that is seen in the translation unit. But anyway you need to specialize it. I hope with a new C++09 standard (it is still pretty far away), it will be possible to inspect if a class has a specific member function (using typeof or decltype operators). Currently it is only possible with has_xxx macros.
http://unitlib.svn.sourceforge.net/viewvc/unitlib/trunk/include/unitlib/metafunc/is_quantity_operand.hpp?revision=6&view=markup A meta function that is composed of two meta functions that check for the existence of a function with a particular signature in the passed in type and for the existence of a typedef of a particular name. Uses SFINAE for testing mechanics. I don't know that this would help with the OP's problem except to make sure there is a smart pointer that responds to a particular interface. If that is what is needed then something like that would help.

Noah, I am aware of such constructs and possibilities. The problem arises if you have the following class: template<class T> class ptr_like_type { public: T* operator->(); //... }; In this case you can not generically know thre result type of the operator-> (because it depends on specialization of type T) and therefore you can't say if this function is defined or not. The only thing would be the use of decltype or result_of operator. That is what I ment. Best Regards, Ovanes -----Original Message----- From: Noah Roberts [mailto:roberts.noah@gmail.com] Sent: Monday, January 22, 2007 7:06 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] 'is_pointer_like' or 'is_smart_pointer' orsimilar,again Ovanes Markarian wrote:
Just a small addition. You can also specialize the template in another file, that is seen in the translation unit. But anyway you need to specialize it. I hope with a new C++09 standard (it is still pretty far away), it will be possible to inspect if a class has a specific member function (using typeof or decltype operators). Currently it is only possible with has_xxx macros.
http://unitlib.svn.sourceforge.net/viewvc/unitlib/trunk/include/unitlib/meta func/is_quantity_operand.hpp?revision=6&view=markup A meta function that is composed of two meta functions that check for the existence of a function with a particular signature in the passed in type and for the existence of a typedef of a particular name. Uses SFINAE for testing mechanics. I don't know that this would help with the OP's problem except to make sure there is a smart pointer that responds to a particular interface. If that is what is needed then something like that would help. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes Markarian wrote:
Just a small addition. You can also specialize the template in another file, that is seen in the translation unit.
Be aware, however, that it's not necessarily a good idea: If you forget to #include the specialization definition, an instantiation of is_pointer_like<T>::value can be both 'false' and 'true' in the same program. This scenario can lead to strange effects that are not necessarily caught at compile time. You should at least add a forward declaration of the class template specialization to the header defining the smart pointer (but you might as well just define the specialization in this case because it's trivial). Regards, Tobias

On 1/27/07, Tobias Schwinger
Ovanes Markarian wrote:
Just a small addition. You can also specialize the template in another file, that is seen in the translation unit.
Be aware, however, that it's not necessarily a good idea:
If you forget to #include the specialization definition, an instantiation of is_pointer_like<T>::value can be both 'false' and 'true' in the same program. This scenario can lead to strange effects that are not necessarily caught at compile time.
You should at least add a forward declaration of the class template specialization to the header defining the smart pointer (but you might as well just define the specialization in this case because it's trivial).
Regards, Tobias
I think, at the end of the day, that it would be nice if smart ptrs derived from some tag classes describing their traits, just like iterators, containers, unary/binary functions, etc should/do. ie smart_ptr_tag, weak_ptr_tag, etc. Tony
participants (4)
-
Gottlob Frege
-
Noah Roberts
-
Ovanes Markarian
-
Tobias Schwinger