我的代码:
#include <iostream>
#include <functional>
using namespace std;
struct A {
A() = default;
A(const A&) {
cout << "copied A" << endl;
}
};
void foo(A a) {}
int main(int argc, const char * argv[]) {
std::function<void(A)> f = &foo;
A a;
f(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在控制台上看到"复制A"两次.为什么对象被复制两次而不是一次?我怎样才能正确预防?
互联网上的一些来源(特别是这一个)说std :: function使用小闭包优化,例如,如果闭包大小低于某些数据量,它不会分配堆(上面的链接指示gcc的16个字节)
所以我开始挖掘g ++标题
看看是否应用这种优化是由"功能"标题中的这段代码决定的(g ++ 4.6.3)
static void
_M_init_functor(_Any_data& __functor, _Functor&& __f)
{ _M_init_functor(__functor, std::move(__f), _Local_storage()); }
Run Code Online (Sandbox Code Playgroud)
和一些线下来:
static void
_M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
{ new (__functor._M_access()) _Functor(std::move(__f)); }
static void
_M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
{ __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
};
Run Code Online (Sandbox Code Playgroud)
例如,如果_Local_storage()为true_type,则调用placement-new,否则为 - regular new
_Local_storage的定义如下:
typedef integral_constant<bool, __stored_locally> _Local_storage;
Run Code Online (Sandbox Code Playgroud)
和__stored_locally:
static const std::size_t _M_max_size = sizeof(_Nocopy_types);
static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
static const bool __stored_locally =
(__is_location_invariant<_Functor>::value
&& sizeof(_Functor) <= _M_max_size …Run Code Online (Sandbox Code Playgroud) 我想在c ++类中使用GSL而不将成员函数声明为static.原因是因为我不太了解它们,我不确定线程的安全性.从我读到的,std::function可能是一个解决方案,但我不知道如何使用它.
我的问题归结为如何static在声明中删除g?
#include<iostream>
#include <functional>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_plain.h>
#include <gsl/gsl_monte_miser.h>
#include <gsl/gsl_monte_vegas.h>
using namespace std;
class A {
public:
static double g (double *k, size_t dim, void *params)
{
double A = 1.0 / (M_PI * M_PI * M_PI);
return A / (1.0 - cos (k[0]) * cos (k[1]) * cos (k[2]));
}
double result() {
double res, err;
double xl[3] = { 0, 0, 0 …Run Code Online (Sandbox Code Playgroud) 我正在玩std :: function和std :: bind,我注意到一些不直观的东西,我想更好地理解它.
例如:
void fun()
{
}
void hun(std::string)
{
}
int main()
{
function<void(int)> g = &fun; //This fails as it should in my understanding.
function<void(int)> f = std::bind(fun); //This works for reasons unknown to me
function<void(int, std::string)> h = std::bind(hun); //this doesn't work
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何function<void(int)>将一个函数绑定到void().然后我可以调用f(1)并获得乐趣().我想了解这是如何完成的.进入微软Visual Studio 2012的实现,让我迷失在无法理解的宏观之中.所以这就是我在这里问这个问题的原因.
我正在使用std :: function和自定义分配器,但是当我没有为初始仿函数提供函数时,它的行为不像我预期的那样.
当我向构造函数提供自定义分配器但没有初始函子时,分配器从未使用过或者看起来如此.
这是我的代码.
//Simple functor class that is big to force allocations
struct Functor128
{
Functor128()
{}
char someBytes[128];
void operator()(int something)
{
cout << "Functor128 Called with value " << something << endl;
}
};
int main(int argc, char* argv[])
{
Allocator<char, 1> myAllocator1;
Allocator<char, 2> myAllocator2;
Allocator<char, 3> myAllocator3;
Functor128 myFunctor;
cout << "setting up function1" << endl;
function<void(int)> myFunction1(allocator_arg, myAllocator1, myFunctor);
myFunction1(7);
cout << "setting up function2" << endl;
function<void(int)> myFunction2(allocator_arg, myAllocator2);
myFunction2 …Run Code Online (Sandbox Code Playgroud) 我有三个功能,我想合并在一起.
每个std::function参数都作为第一个参数,然后在try/ catch块中执行.
问题是,有三种不同类型的功能.没有参数的函数,带有一个整数参数的函数和带有两个整数参数的函数.具有整数参数的参数也具有通过原始函数传递的相应参数.
可以看出,每个函数几乎都是相同的,所以如果我可以将它们合并在一起会很好.但是,我不确定无论如何设置一个可以接收任何形式的参数,std::function并且还依赖于它已经提供了相应的数据来使用.
以下是功能:
void run_callback(std::function<void()>& func) {
try {
func();
} catch(const std::exception& ex) {
print_callback_error(ex.what());
} catch(const std::string& ex) {
print_callback_error(ex.c_str());
} catch(...) {
print_callback_error();
}
}
void run_callback_int(std::function<void(int)>& func, int data) {
try {
func(data);
} catch(const std::exception& ex) {
print_callback_error(ex.what());
} catch(const std::string& ex) {
print_callback_error(ex.c_str());
} catch(...) {
print_callback_error();
}
}
void run_callback_intint(std::function<void(int, int)>& func, int data1, int data2) {
try {
func(data1, data2);
} catch(const …Run Code Online (Sandbox Code Playgroud) 用我的编译器
typedef const double&(*fT)(const double&, const double&);
typedef std::function<const double&(const double&, const double&)> std_func;
fT f1 = std::max<double>; //(1)
std_func f2 = static_cast<fT>(std::max<double>); //(2)
std_func f3 = f1; //(3)
Run Code Online (Sandbox Code Playgroud)
(1,2,3)工作但是
auto f4 = std::max<double>; //(4)
std_func f5 = std::max<double>; //(5)
Run Code Online (Sandbox Code Playgroud)
(4,5)不要.编译器抱怨它无法选择案例5的重载.
这种行为是否正常?
什么是最便携和正确的写作方式?
我发现了以下行为std::function和类型演绎,这对我来说意外:
#include <functional>
template <typename T>
void stdfunc_test(std::function<T(T)> func) {};
int test_func(int arg)
{
return arg + 2;
}
int main()
{
stdfunc_test([](int _) {return _ + 2;});
stdfunc_test(test_func);
}
Run Code Online (Sandbox Code Playgroud)
两行都main导致错误:
没有函数模板"stdfunc_test"的实例与参数列表匹配
尝试在Visual Studio 2015中编译时.
为什么类型扣除不会从函数类型中扣除模板类型,是否有解决方法?
在将 lambda 转换为 的过程中是否复制了捕获的参数std::function?
我需要将捕获不可复制类型的 lambda 转换为std::function.
所以我将一个 lambdastd::function作为右值传递给,但发生了错误。
// Foo is non-copyable.
auto a = [f = Foo()]{ };
std::function<void()> b = std::move(a) // error, calls deleted Foo::Foo(const Foo&);
Run Code Online (Sandbox Code Playgroud) 我有一个结构体,call其中有一个名为 const 重载的方法。唯一的参数是 a,std::function它要么采用 int 引用,要么采用 const int 引用,具体取决于重载。
该genericCall方法执行完全相同的操作,但使用模板参数而不是 asstd::function类型。
struct SomeStruct {
int someMember = 666;
void call(std::function<void(int&)> f) & {
f(someMember);
std::cout << "call: non const\n";
}
void call(std::function<void(const int&)> f) const& {
f(someMember);
std::cout << "call: const\n";
}
template <typename Functor>
void genericCall(Functor f) & {
f(someMember);
std::cout << "genericCall: non const\n";
}
template <typename Functor>
void genericCall(Functor f) const& {
f(someMember);
std::cout << "genericCall: const\n";
}
}; …Run Code Online (Sandbox Code Playgroud)