- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我没有什么好的解决办法。我有一段代码:
#include <iostream>
#include <future>
#include <memory>
#include <functional>
#include <cstddef>
#include <tuple>
#include <typeinfo>
#include <queue>
#include <type_traits>
#include "threadsafe_queue.hpp"
using namespace std;
template<int ...>
struct seq { };
template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };
template<int ...S>
struct gens<0, S...> {
typedef seq<S...> type;
};
class JoinThreads {
vector<std::thread>& m_threads;
public:
explicit JoinThreads(vector<std::thread>& threads): m_threads(threads) {}
~JoinThreads() {
for (unsigned long a = 0; a < this-> m_threads.size(); a++) {
if (this->m_threads[a].joinable()) {
this->m_threads[a].join();
}
}
}
};
template<typename T>
class Callable {
T m_proc;
public:
Callable() {}
Callable(T proc): m_proc(proc) {
//
}
template<typename ... V>
typename std::result_of<T>::type Call(V ... args) {
return this-> m_proc(args ...);
}
};
template<typename ResultType>
class TaskResult {
ResultType m_result;
public:
TaskResult(ResultType result) {}
ResultType result() const {
return this-> m_result;
}
};
template<typename CallableType, typename ... ArgType>
class Task {
Callable<CallableType> m_callable;
std::tuple<ArgType...> m_args;
void (*m_callback)(TaskResult<CallableType>);
template<int ...S>
typename std::result_of<CallableType>::type invokeCallable(seq<S...>) {
return this-> m_callable.Call(std::get<S>(this-> m_args) ...);
}
protected:
typedef typename std::result_of<CallableType>::type callableType;
public:
Task() {}
Task(Callable<CallableType> callable, ArgType ... args, TaskResult<CallableType> callback): m_callable(callable), m_callback(callback) {
this-> m_args = std::make_tuple(args ...);
}
void Execute() {
typename std::result_of<CallableType>::type result = this-> invokeCallable(typename gens<sizeof...(ArgType)>::type());
void (*callback)(TaskResult<CallableType>) = reinterpret_cast<void (*)(TaskResult<CallableType>) >(this-> m_callback);
if (0 < (size_t)callback) {
this-> m_callback(TaskResult<CallableType>(result));
}
}
};
template<typename T>
class Dequeue {
threadsafe_queue<T> m_tasks;
std::vector<std::thread> m_threads;
std::atomic_bool m_done;
JoinThreads m_joiner;
void Process() {
typename threadsafe_queue<T>::m_size_type size = 0;
while (!m_done) {
T task;
if (this-> m_tasks.try_pop(task)) {
task.Execute();
} else {
std::this_thread::yield();
}
}
}
public:
Dequeue(unsigned threads = 0): m_done(false), m_joiner(m_threads) {
unsigned const threadCount = threads > 0 ? threads : std::thread::hardware_concurrency();
cout << "Threads count: " << threadCount << endl;
try {
for (unsigned i = 0; i < threadCount; ++i) {
this-> m_threads.push_back(std::thread(&Dequeue::Process, this));
}
} catch (...) {
this-> m_done = true;
throw;
}
}
~Dequeue() {
this-> m_done = true;
}
template<typename CallableType, typename CallbackType, typename ... ArgType>
void Subscribe(CallableType callable, CallbackType callback, ArgType ... args) {
this-> m_tasks.push(Task<CallableType, CallbackType, ArgType...> (callable, args ..., callback));
}
void Subscribe(T task) {
this-> m_tasks.push(task);
}
};
template<typename CallableType, typename CallbackType, typename ... ArgType>
Task<CallableType, CallbackType, ArgType ...> SubscribeTask(CallableType callable, CallbackType callback, ArgType ... args) {
return Task<CallableType, CallbackType, ArgType...> (callable, args ..., callback);
}
int foo(int a, int b) {
int N = 1024, tab[N], z = 0;
//Some long task
return tab[0];
}
void callback(TaskResult<int (*)(int, int)> res) {
cout << "Callback" << endl;
}
/*
* Callback jednak warto zrobić z parametrem jako obiekt jakiegoś zdarzenia i
* nie wymuszać konieczności podania parametru szablonu.
*/
int main() {
cout << "Hello world!" << endl;
Dequeue<Task<int (*)(int, int), int, int> > dequeue(4);
cout << "Pushing tasks..." << endl;
dequeue.Subscribe(foo, callback, 2156, 55);
for (int a = 0; a < 1024; a++) {
for (int b = 0; b < 1024; b++) {
dequeue.Subscribe(foo, callback, a, b);
}
}
cout << "Processing..." << endl;
int a;
cin >> a;
return 0;
}
编辑:这里是标题
#include <mutex>
#include <condition_variable>
#include <queue>
#include <memory>
template<typename T>
class threadsafe_queue {
private:
mutable std::mutex mut;
std::queue<T> data_queue;
std::condition_variable data_cond;
public:
typedef
typename
std::queue<T>::size_type m_size_type;
threadsafe_queue() {
}
threadsafe_queue(threadsafe_queue const& other) {
std::lock_guard < std::mutex > lk(other.mut);
data_queue = other.data_queue;
}
void push(T new_value) {
std::lock_guard < std::mutex > lk(mut);
data_queue.push(new_value);
data_cond.notify_one();
}
void wait_and_pop(T& value) {
std::unique_lock < std::mutex > lk(mut);
data_cond.wait(lk, [this] {return !data_queue.empty();});
value = data_queue.front();
data_queue.pop();
}
std::shared_ptr<T> wait_and_pop() {
std::unique_lock < std::mutex > lk(mut);
data_cond.wait(lk, [this] {return !data_queue.empty();});
std::shared_ptr<T> res(std::make_shared < T > (data_queue.front()));
data_queue.pop();
return res;
}
bool try_pop(T& value) {
std::lock_guard < std::mutex > lk(mut);
if (data_queue.empty())
return false;
value = data_queue.front();
data_queue.pop();
return true;
}
std::shared_ptr<T> try_pop() {
std::lock_guard < std::mutex > lk(mut);
if (data_queue.empty())
return std::shared_ptr<T>();
std::shared_ptr<T> res(std::make_shared < T > (data_queue.front()));
data_queue.pop();
return res;
}
bool empty() const {
std::lock_guard < std::mutex > lk(mut);
return data_queue.empty();
}
m_size_type size() const {
std::lock_guard < std::mutex > lk(mut);
return data_queue.size();
}
};
我有错误:
main.cpp: In instantiation of ‘class Callable<int (*)(int, int)>’:
main.cpp:69:25: required from ‘class Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>’
main.cpp:135:3: required from ‘void Dequeue<T>::Subscribe(CallableType, CallbackType, ArgType ...) [with CallableType = int (*)(int, int); CallbackType = void (*)(TaskResult<int (*)(int, int)>); ArgType = {int, int}; T = Task<int (*)(int, int), int, int>]’
main.cpp:175:43: required from here
main.cpp:51:35: error: invalid use of incomplete type ‘class std::result_of<int (*)(int, int)>’
typename std::result_of<T>::type Call(V ... args) {
^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
from /usr/include/c++/4.9/bits/stl_pair.h:59,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9/type_traits:2060:11: error: declaration of ‘class std::result_of<int (*)(int, int)>’
class result_of;
^
main.cpp: In instantiation of ‘class Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>’:
main.cpp:135:3: required from ‘void Dequeue<T>::Subscribe(CallableType, CallbackType, ArgType ...) [with CallableType = int (*)(int, int); CallbackType = void (*)(TaskResult<int (*)(int, int)>); ArgType = {int, int}; T = Task<int (*)(int, int), int, int>]’
main.cpp:175:43: required from here
main.cpp:74:46: error: invalid use of incomplete type ‘class std::result_of<int (*)(int, int)>’
typename std::result_of<CallableType>::type invokeCallable(seq<S...>) {
^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
from /usr/include/c++/4.9/bits/stl_pair.h:59,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9/type_traits:2060:11: error: declaration of ‘class std::result_of<int (*)(int, int)>’
class result_of;
^
main.cpp:78:54: error: invalid use of incomplete type ‘class std::result_of<int (*)(int, int)>’
typedef typename std::result_of<CallableType>::type callableType;
^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
from /usr/include/c++/4.9/bits/stl_pair.h:59,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9/type_traits:2060:11: error: declaration of ‘class std::result_of<int (*)(int, int)>’
class result_of;
^
main.cpp: In instantiation of ‘void Dequeue<T>::Subscribe(CallableType, CallbackType, ArgType ...) [with CallableType = int (*)(int, int); CallbackType = void (*)(TaskResult<int (*)(int, int)>); ArgType = {int, int}; T = Task<int (*)(int, int), int, int>]’:
main.cpp:175:43: required from here
main.cpp:135:3: error: no matching function for call to ‘Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>::Task(int (*&)(int, int), int&, int&, void (*&)(TaskResult<int (*)(int, int)>))’
this-> m_tasks.push(Task<CallableType, CallbackType, ArgType...> (callable, args ..., callback));
^
main.cpp:135:3: note: candidates are:
main.cpp:82:2: note: Task<CallableType, ArgType>::Task(Callable<CallableType>, ArgType ..., TaskResult<CallableType>) [with CallableType = int (*)(int, int); ArgType = {void (*)(TaskResult<int (*)(int, int)>), int, int}]
Task(Callable<CallableType> callable, ArgType ... args, TaskResult<CallableType> callback): m_callable(callable), m_callback(callback) {
^
main.cpp:82:2: note: candidate expects 5 arguments, 4 provided
main.cpp:80:2: note: Task<CallableType, ArgType>::Task() [with CallableType = int (*)(int, int); ArgType = {void (*)(TaskResult<int (*)(int, int)>), int, int}]
Task() {}
^
main.cpp:80:2: note: candidate expects 0 arguments, 4 provided
main.cpp:68:7: note: constexpr Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>::Task(const Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>&)
class Task {
^
main.cpp:68:7: note: candidate expects 1 argument, 4 provided
main.cpp:68:7: note: constexpr Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>::Task(Task<int (*)(int, int), void (*)(TaskResult<int (*)(int, int)>), int, int>&&)
main.cpp:68:7: note: candidate expects 1 argument, 4 provided
main.cpp: In instantiation of ‘class Task<int (*)(int, int), int, int>’:
main.cpp:105:6: required from ‘void Dequeue<T>::Process() [with T = Task<int (*)(int, int), int, int>]’
main.cpp:121:44: required from ‘Dequeue<T>::Dequeue(unsigned int) [with T = Task<int (*)(int, int), int, int>]’
main.cpp:173:55: required from here
main.cpp:74:46: error: invalid use of incomplete type ‘class std::result_of<int (*)(int, int)>’
typename std::result_of<CallableType>::type invokeCallable(seq<S...>) {
^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
from /usr/include/c++/4.9/bits/stl_pair.h:59,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9/type_traits:2060:11: error: declaration of ‘class std::result_of<int (*)(int, int)>’
class result_of;
^
main.cpp:78:54: error: invalid use of incomplete type ‘class std::result_of<int (*)(int, int)>’
typedef typename std::result_of<CallableType>::type callableType;
^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
from /usr/include/c++/4.9/bits/stl_pair.h:59,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from main.cpp:1:
/usr/include/c++/4.9/type_traits:2060:11: error: declaration of ‘class std::result_of<int (*)(int, int)>’
class result_of;
我尝试在使用 result_of 之前添加类型名称,但它导致了其他错误:“不完整类型的有效使用...”。
我想要像您在 main 函数中看到的那样。我想将新任务添加到线程池之类的东西中。模板参数应尽可能少。完美的解决方案是完全推导 :-)。
最佳答案
在result_of<>
您不仅必须传递函数的类型(在您的情况下为 T
),还必须传递参数的类型。
所以,如果我理解你的意图,你的 Callable
类还应该接收可变参数列表(例如 ArgsT
)并使用它们( std::result_of<T(ArgsT...)>::type
)。
换句话说,我想你的 Callable
类应该是
template<typename T, typename ... ArgsT>
class Callable {
T m_proc;
public:
Callable() {}
Callable(T proc): m_proc(proc) {
//
}
template<typename ... V>
typename std::result_of<T(ArgsT...)>::type Call(V ... args) {
return this-> m_proc(args ...);
}
};
并且,通过 Task
中的示例, 应称为
Callable<CallableType, ArgType...> m_callable;
与 result_of<>
的其他用途相同的解决方案: 将 arg 类型传递给类型函数
typename std::result_of<CallableType(ArgType...)>::type
关于c++ - 在 ‘typename’ 之前需要 ‘std::result_of<_Signature>::type’ 因为 ‘std::result_of<_Signature>’ 是一个依赖范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40057013/
我试图任意“绑定(bind)”模板参数,但遇到了一个优雅问题。 直接切入根本问题,gcc 6.2 有以下问题,但逻辑上我认为没有问题...... template P, typename A, typ
我想知道为什么类特化来自 template至 template不支持。 例如: template struct B{}; template struct B{}; //ok template str
我希望以下代码在 foo 从 base 派生任何东西时编译,否则会出现编译错误。我已经编写了类型特征类 is_Base,因为 std::is_base_of 不能很好地与我的模板内容一起使用。我很接近
最近我偶然发现了这样一段代码: template template void SomeClass::Function() {} 有模板函数,但它有奇怪的语法,我不太明白。它有什么作用?附近有没有t
我有一个 vector : std::vector> m_connections 然后我想声明一个由共享指针组成的 vector ,指向与该 vector 以通用方式由弱指针持有的相同类型: std:
探索时this answer我发现采用参数包的模板不会被期望具有特定数量参数的模板的模板接受。 在我看来,这是一个缺陷,因为如果一个模板可以接受任意数量的参数,它应该能够映射到一个特定的数字。有语言律
我需要为我的类获取 2 个类型参数:T1,它是一个具有模板的类,以及 T2,它是 T1 模板的参数。 在我的例子中,一个顶点类型(有 2 个,一个从另一个继承),以及顶点存储的数据类型(在我的例子中是
有时我发现自己需要以下东西: template struct choose{ typedef T1 type; }; template struct choose{ typedef T2 ty
对我来说,编译器可以推导出这样的模板类型看起来很自然: template struct wrap { std::function func_; template wrap(Ar
我对嵌套模板及其模板特化有疑问。给定以下类: 一个小模板类 template class T { public: T(){} virtual ~T (){} }; 还有一些嵌套模板 t
我正在从事一个使用 VS 2008 中内置的测试工具的项目。 我会定期看到类似于以下内容的错误:“AcademyPro.Code.BLL.Appearance”类型的值无法转换为“AcademyPro
代码如下: namespace o { template struct Alias; template inline std::ostream &operator &inst); template
根据文档(https://en.cppreference.com/w/cpp/utility/move),std::move有两种构造函数,在下面发布。 这些构造函数之间有什么区别? 最让我困惑的是,
我有三个 View (AddMatchView、TeamPickerView 和 TeamsOfCountryView)。一切都应该像这样工作:从 AddTeamView 我转到 TeamPicker
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我试图通过想象神秘的构造来更全面地掌握模板语法和语义。我认为 C++11 标准不允许使用以下语法: template class A {...}; // phony "specialization"
我非常惊讶地发现,当依赖类型作为基类出现时,没有必要添加 typename: struct B {}; struct wr { typedef B type; }; template struct A
我一直在阅读有关删除类型引用的内容,here . 它给出了以下示例: #include // std::cout #include // std::is_same template void pr
一段时间以来,我一直在尝试使用模板,但我做的越多,我意识到我理解的就越少。这个最新的问题感觉就像是我发现了一个相当根本的误解,我开始比以往任何时候都想得更多,“好吧,明天我不应该写任何代码,而是找一个
以下是代码,引用自 Addison Wesley 的 C++ 模板: template class MyClass { typename T::SubType * ptr;
我是一名优秀的程序员,十分优秀!