Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ Visual Studio 2013中的函数模板替换错误?_C++_Templates_Visual Studio 2013 - Fatal编程技术网

C++ Visual Studio 2013中的函数模板替换错误?

C++ Visual Studio 2013中的函数模板替换错误?,c++,templates,visual-studio-2013,C++,Templates,Visual Studio 2013,假设有一个类TArray,它具有运算符[]。Tarray[index]的返回类型是已知的,我们称之为TResult,但是index本身的类型是TIndex,我想弄清楚。为此,我定义了 下面的模板 template<class TArray, class TResult> struct ArrayIndexType { template<class TIndex> using OperatorFunc = TResult(TArray::*)(TIndex)

假设有一个类
TArray
,它具有运算符[]。
Tarray[index]
的返回类型是已知的,我们称之为
TResult
,但是
index
本身的类型是
TIndex
,我想弄清楚。为此,我定义了 下面的模板

template<class TArray, class TResult>
struct ArrayIndexType
{
    template<class TIndex>
    using OperatorFunc = TResult(TArray::*)(TIndex);

    template<class TIndex> 
    static TIndex test(OperatorFunc<TIndex>);

    using type = decltype(test(&TArray::operator[]));
};
模板
结构ArrayIndexType
{
模板
使用运算符func=TResult(TArray::*)(TIndex);
模板
静态TIndex试验(OperatorFunc);
使用type=decltype(测试(&TArray::operator[]);
};
它实际上适用于std::vector:

cout << typeid(ArrayIndexType<vector<double>, /*TResult=*/double&>::type).name();
// output:    unsigned int.

这不是一个bug。
&DoubleVec::operator[]
的类型仍然是“指向
std::vector
的成员函数的指针”,编译器无法从中推断出“指向
DoubleVec
的成员函数的指针”

对于标准容器来说,这几乎毫无意义:

template<class T>
using ArrayIndexType = typename T::size_type;

明亮的你的第二个建议是有效的,这是我真正需要的,因为这个宏将不仅用于std::vector,真的。
template<class T>
using ArrayIndexType = typename T::size_type;
template<class TArray, class TResult>
struct ArrayIndexType
{
    template<class T, class TIndex>
    using OperatorFunc = TResult(T::*)(TIndex);

    template<class T, class TIndex> 
    static TIndex test(OperatorFunc<T, TIndex>);

    using type = decltype(test(&TArray::operator[]));
};