的类型有两种结构:C++,模板:获得该项目
template <typename T>
struct AB
{
T a, b;
AB <T> () : a (0.0), b (0.0) {}
};
template <typename T>
struct ABList
{
typedef std::list < AB <T> > Type;
typedef T Type2;
};
和功能
template <typename List>
void test (List l)
{
List::iterator i_l = l.begin();
//Here *i_l type is needed instead of double
double val = (*il).a;
}
有什么办法如何让* I_L模板化的类型(此处双)即,
std::list::Item type
而不经过任何其他参数如果
int main(int argc, char* argv[])
{
ABList <double> ::Type intervals;
test (intervals);
return 0;
}
感谢您的帮助,C++ 03是首选。
更新问题
如果一个模板化的类型
std::list::Item type
代表测试)的正式参数(,此解决方案
template <typename List>
void test (List l, typename List::value_type::value_type val)
{
...
}
int main(int argc, char* argv[])
{
ABList <double> ::Type intervals;
double x = 7.0;
test <ABList<double>> (intervals, x);
return 0;
}
不工作...以下错误发生:
error C2770: invalid explicit template argument(s)
版本
test (intervals, x);
导致另一个错误:
Failed to specialize function template 'void test(List,List::value_type::{ctor})'
不完全相信你意思。你想要['std :: list :: value_type'](http://en.cppreference.com/w/cpp/container/list)。 – BoBTFish 2013-05-07 14:42:11
另一种选择:写你自己type_traits为你类型。 – maverik 2013-05-07 14:50:10
放弃了测试函数的显式模板参数,请尝试在main()中只调用test(intervals,x)并让“模板参数推导”找到函数的正确匹配。我试过了,它编译没有问题 – tmaric 2013-05-07 16:11:42