- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下两个函数用于将 std 容器转换为字符串以进行日志记录。
template <typename TCollection, std::string(*ToStringFunc)(typename TCollection::value_type)>
std::string LOG_COLLECTION(const TCollection& collection)
{
std::string as_str;
for (const auto& element : collection)
{
as_str += ToStringFunc(element) + " ";
}
return as_str;
}
template <typename TCollection>
std::string LOG_COLLECTION(const TCollection& collection)
{
return LOG_COLLECTION<TCollection, std::to_string>(collection);
}
我想要一个针对 std::string
容器的特化。类似于以下内容:
template <typename TCollection<std::string>>
std::string LOG_COLLECTION(const TCollection<std::string>& collection)
{
std::string as_str;
for (const auto& element : collection)
{
as_str += ToStringFunc(element) + " ";
}
return as_str;
}
int main()
{
std::vector<std::string> vec{ "a", "b", "c" };
std::string as_str = LOG_COLLECTION(vec)
}
我该怎么做(我使用的是 c++11
)?我尝试在网上搜索但没有找到解决方案。
最佳答案
喜欢 @Scheff 在评论中建议,你可以提供一个template template function specialization对于 std::string
s.
以下是示例代码:( See Online Live )
#include <iostream>
#include <vector>
#include <string>
// template alias for function pointer
template<template<class...> class TCollection, typename ValueType>
using FunctionPtrType = std::string(*)(typename TCollection<ValueType>::value_type);
// `LOG_COLLECTION` for the Container<non-std::string>
template <template<class...> class TCollection, typename ValueType>
std::string LOG_COLLECTION(const TCollection<ValueType>& collection,
FunctionPtrType<TCollection, ValueType> ToStringFunc)
{
std::string as_str;
for (const ValueType element : collection) {
as_str += ToStringFunc(element) + " ";
}
return as_str;
}
// `LOG_COLLECTION` for the Container<std::string>
template <template<class...> class TCollection, typename... Args>
std::string LOG_COLLECTION(const TCollection<std::string, Args...>& collection)
{
std::string as_str;
for (const auto& element : collection) {
as_str += (element + " ");
}
return as_str;
}
int main()
{
std::vector<int> vec{ 1, 2, 3 };
// call the Container<non-std::string> like
std::string as_str = LOG_COLLECTION(vec,
[](int val) { return ::std::to_string(val); });
std::vector<std::string> vec2{ "1", "2", "3" };
std::string as_str2 = LOG_COLLECTION(vec2); // call with no `ToString` function!
}
或者 SFINAE(“替换失败不是错误”)LOG_COLLECTION
函数如下:
( See Online Live )
#include <type_traits> // std::integral_constant, std::is_same, std::enable_if
// traits for checking the `value_type == std::string`
template<class Container> struct has_std_string_value_type final
: public std::integral_constant<bool, std::is_same<std::string, typename Container::value_type>::value>
{};
// template alias for function pointer
template<typename TCollection>
using FunctionPtrType = std::string(*)(typename TCollection::value_type);
// for the Container<non-std::string>
template <typename TCollection>
auto LOG_COLLECTION(const TCollection& collection, FunctionPtrType<TCollection> ToStringFunc)
-> typename std::enable_if<! has_std_string_value_type<TCollection>::value, std::string>::type
{
std::string as_str;
for (const auto element : collection) {
as_str += ToStringFunc(element) + " ";
}
return as_str;
}
// for the Container<std::string>
template <typename TCollection>
auto LOG_COLLECTION(const TCollection& collection)
-> typename std::enable_if<has_std_string_value_type<TCollection>::value, std::string>::type
{
std::string as_str;
for (const auto& element : collection) {
as_str += (element + " ");
}
return as_str;
}
关于c++ - Container::value_type 的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62893715/
这个问题在这里已经有了答案: Passing by value vs const & and && overloads (3 个答案) 关闭 8 年前。 为什么push_back的函数签名如下? v
我可以在 C++ 中进行这样的参数解包吗?这段代码无法编译,但我认为这是可能的。 template void foo(Container& container, Args&&... args){
我正在用 C++11 实现自定义容器的迭代器 我收到以下错误: no type named 'value_type' in 'struct std::iterator_traits::seq_read
我正在尝试使用以下代码从容器中提取 value_type。 //CustomTraits.h template struct has_push_back : std::false_type {}; t
我正在尝试定义迭代器包装器的 value_type,它取消引用包装迭代器的 value_type。 对于具有作为实际指针的 value_type 的迭代器,这是微不足道的,但我也需要它为某些“类指针”
我试图了解何时实际使用了 iterator::value_type。 因为,迭代器的所有运算符,似乎只使用iterator::pointer 和iterator::reference。 问题 iter
在我的类(class)里,我有一个成员: std::vector memory_; 现在我想要一个 fnc 返回内存的第一个元素中的内容,但我不想将 std::string 指定为返回类型,以防以后我
在他今年 Going Native 的主题演讲中 The Essence of C++ (转到 40:30)Bjarne Stroustrup 给出了以下代码示例: template vector*>
我正在为自定义容器编写范围构造器: MyContainer(const InputIterator& first, const InputIterator& last,
首先,这是我的例子: SymmetricMatrix, int> a; SymmetrixMatrix, int> b; SymmetricMatrix, double> c; SymmetricMa
对于任何可取消引用的类型,我将如何实现以下目标? 我发现我当前的解决方案缺乏,因为我需要为我希望它使用的每种类型做一个类模板特化: template struct get_value_type {
应该如何使用标准容器的 value_type? 我试着像这样使用它: #include using namespace std; template class TSContainer { priv
STL 通常这样定义输出迭代器: template class insert_iterator : public iterator { // ... 为什么输出迭代器要定义 value_typ
我有以下两个函数用于将 std 容器转换为字符串以进行日志记录。 template std::string LOG_COLLECTION(const TCollection& collection)
我有一个在 typename ContainerType 上参数化的函数模板,它接受一个 const ContainerType& 参数。它将容器内的每个元素转换为其他类型,并(当前)返回转换后类型的
我想编写一个类,将一对迭代器作为构造函数的参数,但我不知道如何在这些迭代器的 value_type 不匹配时在编译时引发错误一个预期的类型。这是我尝试使用 typeid 的结果: #include
我正在写一个序列化和反序列化方法,我在反序列化的实现中遇到了一个问题:我不能new一个value_type,这实际上是一个Skill*。 template static istream &DeSer
给定一个容器,例如 std::list 或 std::vector,我想在我事先不知道容器(std::list 或 std::vector)的情况下分别生成一个新类型 std::list 或 std:
假设我声明 std::vector> .可能发生的更糟情况是什么?我运行了它,似乎没有遇到任何内存错误。 最佳答案 它会导致未定义的行为。对于像 vector 这样的分配器感知容器,标准在表 99 中
我不明白这个神秘的错误消息,但我得到了 30 个 `'value_type' : is not a member of 'TextFileLineBuffer'` 当我在 VC++ 6 中编译以下代码
我是一名优秀的程序员,十分优秀!