例如:
int a = 12;
cout << typeof(a) << endl;
Run Code Online (Sandbox Code Playgroud)
预期产量:
int
Run Code Online (Sandbox Code Playgroud) 我正在尝试将迭代器作为模板参数传递给模板方法,但编译器抱怨:
error C2783: 'void Test::Assert(std::vector<T>::const_iterator)':
could not deduce template argument for 'T'
Run Code Online (Sandbox Code Playgroud)
产生错误的代码是:
#include "stdafx.h"
#include <iostream>
#include <vector>
class Test
{
public:
template <typename T>
void Assert(typename std::vector<T>::const_iterator it)
{
std::cout << *it << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Test test;
std::vector<double> myVec;
test.Assert(myVec.cbegin());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我猜有一种简单的方法可以使这项工作,因为大多数std算法可以从迭代器中推断出类型.