【发布时间】:2012-01-24 19:29:21
【问题描述】:
我正在尝试为元组创建打印方法。我检查了其他人指定的解决方案,所有这些都使用辅助结构。我不想使用辅助结构。我觉得下面的代码是有效的,但无法弄清楚。
#include <iostream>
#include <tr1/tuple>
template<typename tupletype,size_t i>
void print< tupletype ,0>(tupletype t)//error: expected initializer before ‘<’ token
{
std::cout<<std::tr1::get<0><<" ";
}
template<typename tupletype,size_t i>
void print(tupletype t)
{
std::cout<<std::tr1::get<i><<" ";// no match for 'operator<<' in 'std::cout << get<-78ul>'(my ide actually hangs here!)
print<tupletype,i-1>(t);
}
int main (int argc, char * const argv[]) {
std::tr1::tuple<int,float> a(3,5);
typedef std::tr1::tuple<int,float> tupletype;
print<tupletype,0>(a);
}
【问题讨论】:
-
你看到什么样的错误?
-
没有“模板函数”,但有函数模板。
-
为什么不想使用辅助结构?
-
@Mankarse 在我的私人项目中,我使用了一个类似于 'print' 的函数,它对元组进行操作。但是,该函数已经在深层嵌套类中。我觉得 helper struct 又增加了一步复杂性。 mwigdahl, sbi - 我已经更新了问题