现在的位置: 首页 > 综合 > 正文

C++函数模版调用重载函数模版

2013年12月03日 ⁄ 综合 ⁄ 共 502字 ⁄ 字号 评论关闭

对于两个不同的重载函数,两个之间可以互相调用,如下代码

void testoveride(std::string s){
	std::cout<<s<<std::ends;
}

void testoveride(std::vector<std::string> s){
	testoveride(s.at(1));
}

	std::vector<std::string> s;
	s.push_back(std::string("1"));
	s.push_back(std::string("2"));
	testoveride(s);

假如对于两个重载的函数模版

template<class T>
void testoveride(T s){
	std::cout<<s<<std::ends;
}
template<class T>
void testoveride(std::vector<T> s){
	testoveride(s.at(1));
}

	std::vector<std::string> s;
	s.push_back(std::string("1"));
	s.push_back(std::string("2"));
	testoveride(s);

正确的输出“2”

抱歉!评论已关闭.