【发布时间】:2010-03-29 01:39:20
【问题描述】:
想象一下我有一个这样的模板函数:
template<typename Iterator>
void myfunc(Iterator a, typename Iterator::value_type b)
{ ... }
有没有办法通过为 Iterator::valuetype 声明一个我可以在函数签名中使用的 typedef 来实现同样的事情?例如,我希望能够做这样的事情:
template<
typename Iterator,
typedef Iterator::value_type type>
void myfunc(Iterator a, type b)
{ ... }
到目前为止,我已经使用默认模板参数和 Boost 概念检查来确保始终使用默认值:
template<
typename Iterator,
typename type = typename Iterator::value_type >
void myfunc(Iterator a, type b)
{
BOOST_STATIC_ASSERT((
boost::is_same<
typename Iterator::value_type,
type
>::value
));
...
}
...但是如果语言支持这种类型的东西会很好。
编辑
我可能应该使用类而不是函数,因为默认参数不是函数的标准。
template<
typename T,
typename V = typename T::value_type>
class A : public B<T, V>
{
BOOST_STATIC_ASSERT((boost::is_same<typename T::value_Type, V>::type));
};
【问题讨论】:
-
注:函数模板上的默认模板参数是一个扩展。它们不在 c++03 中。
-
我也修正了你的其他遗漏类型名的地方。希望你不介意。