Value_type容器模板参数
Value_type of a container template parameter
在他今年的主题演讲《走向原生的本质C++》(转到40:30)中,Bjarne Stroustrup给出了以下代码示例:
template<typename C, typename V>
vector<Value_type<C>*> find_all(C& cont, V v)
{
vector<Value_type<C>*> res;
for (auto& x : cont)
if (x == v)
res.push_back(&x)
return res;
}
此函数用于查找容器中某个值的所有匹配项并返回指向找到的元素的指针。视频中的示例:
string m{"Mary had a little lamb"};
for (const auto p: find_all(m,'a')) // p is a char*
if (*p != 'a')
cerr << "string bug!n";
我的问题是关于Value_Type<C>*
.标准中有这样的东西吗图书馆?我找了找,没有找到。如果它不在 std 中,如何实现?
我不知道标准中是这样,但实现起来并不难:
template <class C>
struct value_type
{
typedef typename C::value_type type;
};
template <class T, int N>
struct value_type<T[N]>
{
typedef T type;
};
template <class T>
struct value_type<T*>
{
typedef T type;
};
现在,您可以使用typename value_type<C>::type
访问容器包含的类型。 如果您有自己的容器想要使用,但它没有value_type
typedef(无论出于何种原因您无法更改它),那么您也可以简单地将此结构专用于该容器。
要避免typename ...::type
您可以执行以下操作:
template <class C>
using Value_Type = typedef value_type<C>::type;
现在你只需在任何地方使用Value_Type<C>
。
编辑
正如 stefan 在 soon's 的回答中所建议的那样,您可以使用 std::begin
更轻松地执行此操作,这没关系,因为您使用/创建的任何容器都希望能够调用 std::begin
并std::end
:
template <class C>
using Value_Type = typename std::remove_reference<
decltype(*std::begin(std::declval<
typename std::add_lvalue_reference<C>::type>()))>::type;
这要简洁得多,尽管阅读起来有点密集。 它仍然比第一个选项更好,这将需要更少的自定义容器类型的样板代码。
Value_type<C>
只是C::value_type
的typedef。据我所知,标准库中没有这样的 typedef,但您可以自己定义它:
template <class T>
using Value_type = typename T::value_type;
template<typename C, typename V>
std::vector<Value_type<C>*> find_all(C& cont, V v)
{
std::vector<Value_type<C>*> res;
for (auto& x : cont)
if (x == v)
res.push_back(&x);
return res;
}
int main()
{
std::vector<int> v{1, 2, 3, 3, 5};
for(const auto x: find_all(v, 3))
{
std::cout << *x << std::endl;
}
}
但是,正如@stefan所建议的,这仅适用于标准容器。您可以使用std::begin
函数(也是为数组定义的)来检索底层类型,它是如何在@GuyGreer的答案中实现的
相关文章:
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 模板参数'(type)0'与'EnumValue'不匹配
- 编译器如何处理<Type> <Type> 具有多参数的向量initializer_list
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- 使用 std::array<Type, N> 的实例作为模板参数
- 按参数类型更改"return type template"
- std::remove_if 将“const type”作为“this”参数传递会丢弃 Linux 上的限定符
- 当您声明"pointer type"形式(函数)参数"like an const array"时,什么是常量?
- "unresolved overloaded function type"错误并将运算符作为函数参数传递
- 默认模板参数结果为"expected type-specifier"
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 无法将参数 1 从"cli::interior_ptr<Type>"转换为"CvCapture **"
- cmake -D <var>:<type>=<value> 参数"-D"是什么意思
- 无法将参数 1 从"cli::array<Type> ^"转换为"无符号短整型"
- 使用 T::Type typedef 作为默认模板参数
- 为什么称为"non-type"模板参数?
- C++函数模板专用声明和模板参数;无 vs. <> vs. <type>
- 同时在另一个类中创建类的对象,该类仅具有参数化的 Ctor "error: x is not a type"
- 对"non-type"模板参数强制实施规则
- 如何将静态双数组作为参数传递给(type**)函数