C++ 模板类中运算符*的尾部返回类型

C++ 模板类中运算符*的尾部返回类型,c++,visual-studio-2010,templates,c++11,trailing-return-type,C++,Visual Studio 2010,Templates,C++11,Trailing Return Type,这可能很简单,我就是找不到答案。我有一个实现各种操作符的模板类。其中之一是解引用运算符(运算符*)。不幸的是,我似乎不知道如何正确地指定返回类型。这是我正在做的 template <typename Type> class Noodles { private: Type _value; public: Type operator+( const Type& rhs ) const { return Type + rhs; } Type operator

这可能很简单,我就是找不到答案。我有一个实现各种操作符的模板类。其中之一是解引用运算符(
运算符*
)。不幸的是,我似乎不知道如何正确地指定返回类型。这是我正在做的

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; }
};
模板
班级面条
{
私人:
类型_值;
公众:
类型运算符+(常量类型&rhs)常量{返回类型+rhs;}
类型运算符-(const Type&rhs)const{return Type-rhs;}
//更多的运营商只是通过电话
//具体到实际值类型,你明白了。
//这就是我的问题所在
[这里是什么]运算符*()常量{return*_value;}
};
正如您所看到的,这个类只是试图包装任何给定的类型,并对该给定类型执行操作符函数。我尝试了以下方法,但没有效果:

// 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; }
//非法使用类型作为表达式
自动运算符*()->decltype(*Type)常量{return*_value;}
//未声明的标识符/非静态字段
自动运算符*()->decltype(*u值)常量{return*_值;}
//未声明的标识符/非静态字段
自动运算符*()->decltype(*面条::_值)常量{return*_值;}
//“this”只能在非静态成员函数中引用
自动运算符*()->decltype(*this->_值)常量{return*_值;}
所以我不知道该怎么办。大多数示例显示了一个接受模板参数的方法,声明看起来像
autoadd(a-lhs,B-rhs)->decltype(lhs+rhs)…
,但这并不能真正为我提供任何帮助

注意:我目前使用的是VS2010,所以我使用的是decltype v1.0


我感谢你的帮助

为此需要
std::declval
,但不幸的是,VS2010还没有实现这一点。因此,您必须稍微解决一下:

template <typename Type>
class Noodles
{
private:
    Type _value;
    static Type myTypeDeclval();
public:
    auto operator*( ) -> decltype( *myTypeDeclval() ) const { return *_value; }
};
模板
班级面条
{
私人:
类型_值;
静态类型myTypeDeclval();
公众:
自动运算符*()->decltype(*myTypeDeclval())常量{return*\u value;}
};

太好了!非常感谢。我会尽快(10分钟)接受你的回答。请注意,
decltype(*u value)操作符*()
应该可以工作,只要
\u value
是在
操作符*
之前声明的(并且假设编译器实现了它:)。我假设vs2010附带的msvc没有工作,看看我尝试时它是如何抱怨的。但是谢谢@Xeo噢,我不知道在尾部返回类型中也应用了隐式的
这个
解引用。