我正在尝试实现一个类似于std::transform算法的函数,而不是通过我想创建的参数获取输出迭代器,并返回一个带有转换输入元素的容器.
让我们说它的名称transform_container并带有两个参数:容器和仿函数.它应返回相同的容器类型,但可能由不同的元素类型进行参数化(Functor可以返回不同类型的元素).
我想使用我的函数,如下例所示:
std::vector<int> vi{ 1, 2, 3, 4, 5 };
auto vs = transform_container(vi, [] (int i) { return std::to_string(i); });
//vs will be std::vector<std::string>
assert(vs == std::vector<std::string>({"1", "2", "3", "4", "5"}));
std::set<int> si{ 5, 10, 15 };
auto sd = transform_container(si, [] (int i) { return i / 2.; });
//sd will be of type std::set<double>
assert(sd == std::set<double>({5/2., 10/2., 15/2.}));
Run Code Online (Sandbox Code Playgroud)
我能够写两个函数 - 一个for std::set和one for std::vector- 似乎正常工作.它们是相同的,除了容器类型名称.他们的代码如下所示.
template<typename T, typename Functor> …Run Code Online (Sandbox Code Playgroud) 在文章中多次阅读索赔 - 我想将此问题添加到Stackoverflow,并询问社区 - 以下代码是否可移植?
template<template<typename T, typename Alloc> class C>
void f() {
/* some code goes here ... */
}
int main() {
f<std::vector>();
}
Run Code Online (Sandbox Code Playgroud)
供应的实施是否std::vector真的允许有两个众所周知的额外的,默认的模板参数?这会使上面的代码格式错误,因为它假设有两个模板参数.见最后一段在这篇文章中对这种要求的一个例子.
我想问一下下面的代码示例是否应该编译:
#include <iostream>
#include <vector>
#include <typeinfo>
using namespace std;
template <template <class...> class C>
struct convert_container
{
using type = C<double>;
// Visual Studio requires this to be:
// using type = C<double, std::allocator<doble>>
};
int main()
{
std::cout << typeid(convert_container<std::vector>::type).name();
}
Run Code Online (Sandbox Code Playgroud)
代码可以使用GCC 4.8.1和Clang 3.4编译,但不适用于Visual Studio 2013.我得到的错误:
error C2976: 'std::vector' : too few template arguments
c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see declaration of 'std::vector'
c:\users\micha?\documents\visual studio 2013\projects\transform\transform.cpp(14) : see reference to class template instantiation 'convert_container<std::vector>' being compiled …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
template<typename T>
struct A { };
// same as A, but with one extra defaulted parameter
template<typename T, typename F = int>
struct B { };
template<template<typename> typename T>
T<int> build() { return {}; }
int main()
{
build<A>(); // works in gcc and clang
build<B>(); // works in gcc, does not work in clang
}
Run Code Online (Sandbox Code Playgroud)
g ++(7.3.0)编译代码就好了,但是,clang ++(5.0.1)会发出以下命令:
example.cpp:14:5: error: no matching function for call to 'build'
build<B>(); // works in gcc, does not work in clang
^~~~~~~~ …Run Code Online (Sandbox Code Playgroud) 我使用 C++ 并行快速排序程序进行了测试,如下所示,首先使用列表作为容器,然后我转移到通用容器类型,但它报告了标题错误。
\n\n可以帮忙解决这个问题吗?
\n\n#include <iostream> // std::cout\n#include <future> // std::packaged_task, std::future\n#include <chrono> // std::chrono::seconds\n#include <thread> // std::thread, std::this_thread::sleep_for\n#include <list>\n#include <algorithm>\n#include <type_traits>\n#include <iterator>\n\ntemplate<typename F, typename A>\nstatic std::future<typename std::result_of<F(A&&)>::type> spawn_task(F&& f, A&& a)\n{\n typedef typename std::result_of<F(A&&)>::type result_type;\n\n std::packaged_task<result_type(A&&)> task(std::move(f));\n std::future<result_type> res(task.get_future());\n std::thread myThread(std::move(task), std::move(a));\n myThread.detach();\n return res;\n}\n\ntemplate<class T, template<class T> class Container>\n\nstatic Container<T> parallel_quick_sort(Container<T> input)\n{\n if (input.empty())\n {\n return input;\n }\n\n Container<T> result;\n result.splice(result.begin(), input, input.begin());\n T const& partition_val = *result.begin();\n\n typename Container<T>::iterator divide_point = std::partition\n (input.begin(), input.end(), [&](T …Run Code Online (Sandbox Code Playgroud) 我试图通过实现通用容器类来理解C++模板模板.这是代码:
using namespace std;
template <typename T, template <typename STORETYPE> class Container>
class Store {
public:
~Store() {};
Store() {};
void someFunc( const T & ) {};
//...
private:
Container<T> storage;
};
int main(int argc, char *argv[])
{
Store<int,deque> myStore; // error here, won't compile!
}
Run Code Online (Sandbox Code Playgroud)
上面的代码生成编译器错误消息.错误消息是:
"模板模板参数具有与其对应的模板模板参数Store aStack1不同的模板参数;
我不知道为什么.怎么了?