我是std :: thread的新手,我尝试编写代码parallel_for.我编写了以下内容:
// parallel_for.cpp
// compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread
// execution: time ./parallel_for 100 50000000
// (100: number of threads, 50000000: vector size)
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <thread>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
// Parallel for
template<typename Iterator, class Function>
void parallel_for(const Iterator& first, const Iterator& last, Function&& f, const int nthreads = 1, const int threshold = 1000)
{
const unsigned int group = …Run Code Online (Sandbox Code Playgroud) 我创建了一个模板类,其中包含std::function一个成员,方法如下:
template<typename Ret, typename... Args>
class Foo
{
private:
std::function<Ret(Args...)> _func;
public:
Foo(const std::function<Ret(Args...)>& func):
_func(func)
{}
};
Run Code Online (Sandbox Code Playgroud)
为了不必指定传递函数的参数和返回类型,我创建了一些make_foo重载:
template<typename Ret, typename... Args>
auto make_foo(Ret (&func)(Args...))
-> Foo<Ret, Args...>
{
return { std::function<Ret(Args...)>(func) };
}
template<typename Ret, typename... Args>
auto make_foo(const std::function<Ret(Args...)>& func)
-> Foo<Ret, Args...>
{
return { func };
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法创建一个make_foo以lambda作为参数的重载:
template<typename Ret, typename... Args>
auto make_foo(??? func)
-> Foo<Ret, Args...>
{
return { std::function<Ret(Args...)>(func) };
}
Run Code Online (Sandbox Code Playgroud)
我只是找不到从lambda自动推导出返回类型和参数类型的方法.有没有一种解决这种问题的惯用方法?
作为C++中函数式编程的一个小练习,我想重载operator*以允许我编写2个函数.
让我感到困惑的是,如果我像这样定义我的运算符:
std::function<float(float)> operator*(std::function<float(float)> func1, std::function<float(float)> func2)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我无法在常规函数中使用它,如:
float f(float);
float g(float);
auto composite = f*g;
Run Code Online (Sandbox Code Playgroud)
因为我得到:
error: invalid operands of types ‘float(float)’ and ‘float(float)’ to binary ‘operator*’
如果我尝试只添加一个函数指针版本,如:
std::function<float(float)> operator*(float(*func1)(float) , float(*func2)(float))
{
return [func1, func2](float x) { return func1(func2(x)); };
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error: ‘std::function<float(float)> operator*(float (*)(float), float (*)(float))’ must have an argument of class or enumerated type
我能够得到它通过增加工作的一个make_function帮手,但真正使代码丑陋.
有没有办法让这个运算符自动将函数转换为std::function实例?
我有代码
void prints_one()
{ cout << "one" << endl; }
int main(int argc, char *argv[])
{
std::function<void()> foo;
foo = prints_one;
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作; 它打印"一".我不知道的是在赋值中调用哪个赋值运算符原型以及如何调用.看看cpp引用,看起来它可能就是这个函数
template <class Fn> function& operator= (Fn&& fn);
Run Code Online (Sandbox Code Playgroud)
但如果那是被调用的原型,我不明白函数如何绑定到右值引用.谢谢!
更新:谢谢大家,我会阅读通用参考资料.关于40两个答案; 此代码打印出它是一个右值引用:
template<class Fn>
class Foo {
public:
Foo() {}
Foo& operator=(Fn&& x)
{
std::cout << std::boolalpha << std::is_rvalue_reference<decltype(x)>::value << std::endl;
}
};
void prints_one()
{
cout << "one" << endl;
}
int main(int argc, char *argv[])
{
Foo<void()> bar;
bar = prints_one;
}
Run Code Online (Sandbox Code Playgroud)
这打印 …
我所理解的是作为一种典型的用途 std::function
#include <iostream>
#include <functional>
using namespace std;
class C {
public:
C() { cout << "CREATING" << endl; }
C(const C&) { cout << "COPY C "<< endl; };
C(C&&) { cout << "MOVE C " << endl; };
~C() { cout << "DELETING"<< endl; }
C& operator =(const C&) {
cout << "COPY A " << endl; return *this;
};
C& operator =(C&&) {
cout << "MOVE A" << endl; return *this;
};
void operator ()() const …Run Code Online (Sandbox Code Playgroud) 有人可以帮助我理解为什么下面的代码会导致错误吗?
class A
{
public:
float& operator()()
{
return _f;
}
private:
float _f = 1;
} a;
auto& foo()
{
std::function<float()> func = a;
return func();
}
int main()
{
std::cout << foo() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: non-const lvalue reference to type 'float' cannot bind to a temporary of type 'float'
return func();
^~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
在这里,operator()我返回一个参考_f,因此,我认为func()这不是暂时的.如果有人帮助我理解会很棒.
我有一个事件驱动的应用程序.我想保持事件处理程序(EventHandler具有许多/所有事件的类)是一个常见的实现 - 同时允许EventSource可更改(特别是在编译时).
为连接EventHandler用EventSource,我将不得不存储的实例句柄内EventSource.我试图存储 各种形式的处理程序:
EventHandler(即在具体的定义公共处理方法EventHandler的std::function- 这提供了最大的灵活性但是,在这两种情况下,调用目标方法/ lambda的延迟都非常高(在我的测试设置上大约250ns) - 更糟糕的是,它是不一致的.可能是由于虚拟表和/或堆分配和/或类型擦除???
为了减少这种延迟,我想使用模板.
我能想到的最好的是:
template <typename EventHandler>
class EventSource1
{
EventHandler* mHandler;
public:
typedef EventHandler EventHandlerType;
void AssignHandler (EventHandler* handler)
{
this->mHandler = handler;
}
void EventuallyDoCallback (int arbArg)
{
this->mHandler->CallbackFunction (arbArg);
}
};
template <EventSourceType>
class EventSourceTraits
{
typedef EventSourceType::EventHandlerType EventHandlerType;
static void AssignHandler (EventSourceType& source, EventHandlerType* handler)
{ …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例:
#include <iostream>
#include <functional>
struct A
{
int i;
void operator()()
{
std::cout << ++i;
}
};
void test(std::function<void()> const& fun)
{
fun();
}
int main() {
const std::function<void()> f{A{}};
test(f);
test(f);
}
Run Code Online (Sandbox Code Playgroud)
在这里,a const std::function能够呼叫非const operator().
输出:
12
Run Code Online (Sandbox Code Playgroud)
如果我提供一个mutablelambda例如,也会发生同样的情况test([x = 0]() mutable { ++x; });
怎么可能?
const std::function可以包装一个可变的函子是正常的吗?
当我从主可执行文件中调用std :: function时,通过将其地址转换为/从中,我在插件中遇到段错误void*.我可以在几个自包含的行中重现这个问题:
#include <iostream>
#include <functional>
int main()
{
using func_t = std::function<const std::string& ()>;
auto hn_getter = func_t{[]() {
return "Hello";
}};
auto ptr = reinterpret_cast<void*>(&hn_getter);
auto getter = reinterpret_cast<func_t*>(ptr);
std::cout << (*getter)() << std::endl; // Bang!
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
即使我正在使用原始类型,它仍然是segfaulting.谁能看到我哪里出错了?
考虑这样的事情:
class A;
std::function<A(A)> f;
Run Code Online (Sandbox Code Playgroud)
显然,这是您要在程序中执行的操作。任何主要的编译器都可以毫无问题地接受它。
我的问题是:该标准在技术上允许吗?
如果我没看错的话
20.5.4.8其他功能
除非另有说明(并且不仅针对容器),否则禁止在实例化时使用不完整类型的模板参数。
在关于std::function或<functional>我没有看到允许这样做的参考部分。
有人知道这在标准中明确写在什么地方吗?
c++ ×10
std-function ×10
c++11 ×5
c++14 ×1
callback ×1
composition ×1
lambda ×1
stdthread ×1
templates ×1
types ×1