Operator overloading and enable_if
For a template class I'd like to add * and -> operators for types that support it (mainly pointers). However, most instantiations of this class won't be pointers and therefore these operators won't be valid. I understand that enable_if can't directly support member functions. But, how can I do the equivalent of this? template< typename T > class Sample { T data; public: Sample( T& data ) : data(data) { } typename boost::enable_if< boost::is_pointer<T>, T >::type operator->() const { return(data); } typename boost::enable_if< boost::is_pointer<T>, typename boost::remove_pointer<T>::type >::type operator*() const { return(*data); } };
On 06/06/07, Bill Buklis
For a template class I'd like to add * and -> operators for types that support it (mainly pointers). However, most instantiations of this class won't be pointers and therefore these operators won't be valid. I understand that enable_if can't directly support member functions. But, how can I do the equivalent of this?
[code snipped]
SFINAE -- you don't even need boost:
template <typename T>
struct holder {
T value;
T operator->() const { return value; }
};
struct foo { int bar; };
int main() {
holder
Good point. I was definitely making it more complicated than it needed to be. OK, given that what would be the best way to declare/define the operator* function. The following works for pointer types: typename boost::remove_pointer<T>::type& operator*() const { return(*data); } How can this be done for non-pointer types? I'm thinking that in addition to a "get" type function which simply returns data, I could use operator* to return the underlying data for all types. Pointers dereference, while non-pointers return itself. Of course, the real question is would this even be a good thing to do? Without overloading the return type, sort of: typename boost::remove_pointer<T>::type& operator*() const { return( DEREFENCE_ONLY_IF_A_POINTER(data) ); } -- Bill --
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of me22 Sent: Wednesday, June 06, 2007 1:24 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Operator overloading and enable_if
On 06/06/07, Bill Buklis
wrote: For a template class I'd like to add * and -> operators for types that support it (mainly pointers). However, most instantiations of this class won't be pointers and therefore these operators won't be valid. I understand that enable_if can't directly support member functions. But, how can I do the equivalent of this?
[code snipped]
SFINAE -- you don't even need boost:
template <typename T> struct holder { T value; T operator->() const { return value; } };
struct foo { int bar; };
int main() { holder
a; // compiles a->bar = 0; // compiles holder<int> b; // compiles // b->bar = 0; // wouldn't compile } ~ Scott McMurray _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 06/06/07, Bill Buklis
How can this be done for non-pointer types? I'm thinking that in addition to a "get" type function which simply returns data, I could use operator* to return the underlying data for all types. Pointers dereference, while non-pointers return itself.
Of course, the real question is would this even be a good thing to do?
I don't really know what you're doing, but inconsistency is at the very least a code smell.
participants (2)
-
Bill Buklis
-
me22