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[]));
};