#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
template <typename T>
typename T::ElementT Get(T const* a, int k) //这个ElementT是什么东东????
{
return a[k];
}
void func(char* p)
{
char c1 = Get<char>(p, 0);
//error C2770: invalid explicit template argument(s) for 'T::value Get(const T *,int)'
//Get<char*>(p, 0);
//error C2770: invalid explicit template argument(s) for 'T::value Get(const T *,int)'
char c2 = Get(p, 2);
//error C2893: Failed to specialize function template 'T::value Get(const T *,int)'
cout<<"c1 = "<<c1<<endl;
cout<<"c2 = "<<c2<<endl;
}
int main()
{
char cc[] = "abcd";
func(cc);
getch();
}
c++ 模板的一个例子,实参演绎的时候,有个地方没看明白
最新推荐文章于 2024-07-15 17:30:15 发布
本文探讨了一个特定的模板函数实现,并尝试解决在使用该模板函数时出现的编译错误。通过几个示例展示了如何正确地调用模板函数以及可能出现的问题。

606

被折叠的 条评论
为什么被折叠?



