Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;11使用auto和decltype的返回类型扣除_C++_C++11_Templates_Decltype - Fatal编程技术网

C++ C++;11使用auto和decltype的返回类型扣除

C++ C++;11使用auto和decltype的返回类型扣除,c++,c++11,templates,decltype,C++,C++11,Templates,Decltype,我试图写一个函数,将向量乘以标量值。 我想返回一个具有最高数据类型类型的向量 当我编译下面的代码时,我得到的错误如下 “错误:decltype的参数必须是表达式” 我该如何解决这个问题 template<typename T> class Vector { private: T* data; int length; template <typename S> auto operator*(S scalar) const / -&g

我试图写一个函数,将向量乘以标量值。 我想返回一个具有最高数据类型类型的向量

当我编译下面的代码时,我得到的错误如下 “错误:decltype的参数必须是表达式”

我该如何解决这个问题

template<typename T>
class Vector {
private:
    T* data;
    int length;

    template <typename S>
    auto operator*(S scalar) const /
    -> decltype(Vector<typename std::common_type<S,T>::type>);
    {
        // Function Logic
    }
}
模板
类向量{
私人:
T*数据;
整数长度;
样板
自动运算符*(S标量)常量/
->decltype(向量);
{
//功能逻辑
}
}

decltype
用于确定表达式的类型<代码>矢量已经是一种类型。所以没有什么可以使用的
decltype
on

此外,这不是返回类型扣除。它只是一个晚指定的返回类型,在本例中不需要,因为返回类型不依赖于任何参数名。您也可以轻松做到这一点:

template<typename S>
Vector<typename std::common_type<S,T>::type operator*(S scalar) const
模板

矢量只需删除
decltype
<代码>矢量
已经是您想要的类型,对吗?