Question

This may be simple enough, I was just unable to find the answer. I have a template class that implements a variety of operators. One of which is the dereference operator (operator*). Unfortunately I can't seem to figure out how to properly specify the return type. Here's what I'm doing.

template <typename Type>
class Noodles
{
private:
    Type _value;
public:
    Type operator+( const Type& rhs ) const { return Type + rhs; }
    Type operator-( const Type& rhs ) const { return Type - rhs; }
    // Many more operators that simply pass the call
    // down to the actual value type. You get the idea.

    // This is where I'm having the problem
    [what goes here] operator*( ) const { return *_value; }
};

As you can see, this class is just trying to wrap any given type and perform the operator functions on that given type. I've tried the below, but to no avail:

// Illegal use of Type as an expression
auto operator*( ) -> decltype( *Type ) const { return *_value; }

// Undeclared identifier / not a static field
auto operator*( ) -> decltype( *_value) const { return *_value; }

// Undeclared identifier / not a static field
auto operator*( ) -> decltype( *Noodles<Type>::_value ) const { return *_value; }

// 'this' can only be referenced in non-static member functions
auto operator*( ) -> decltype( *this->_value ) const { return *_value; }

So I'm not sure what to do. Most examples show a method that takes template arguments and the declaration looks like auto Add( A lhs, B rhs ) -> decltype( lhs + rhs )... but that doesn't really provide any help for me.

Note: I'm currently using VS2010, so I am using decltype v1.0.

I appreciate your help!

Was it helpful?

Solution

You'd need std::declval for this, but unfortunately, VS2010 doesn't implement that yet. So you'll have to work around it a bit:

template <typename Type>
class Noodles
{
private:
    Type _value;
    static Type myTypeDeclval();
public:
    auto operator*( ) -> decltype( *myTypeDeclval() ) const { return *_value; }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top