假设我有一个像这样的模板函数:
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));
};