I was watching a boostcon talk on youtube titled “Introduction to Modern C++ Techniques (Part I)”. Around minute 22 the speaker shows a class which overloads the dereference operator.
template<typename T,
typename CheckingPolicy = NoChecking,
typename BadPointerPolicy = BadPointerDoNothing>
class pointer_wrapper
{
public:
pointer_wrapper() : value_(0) {}
explicit pointer_wrapper(T* p) : value_(p) {}
operator T*()
{
if ( ! CheckingPolicy::check_pointer(value_) )
{
return BadPointerPolicy::handle_bad_pointer(value_);
}
else
{
return value_;
}
}
private:
T* value_;
};
I have never seen this way of overloading the dereference operator. Why is there no return type and why does the T appear after the ‘operator’ keyword?
I always thought the way to overload this operator was like this:
T& operator *()
{
// ...
return *value_
}
If anybody is interested, here is the talk
It’s
implicit conversion operator to type T*.n3337 12.3.2/1