标签: stdbind

<functional>中的奇怪模板语法

我一直在寻找的源代码std::function,并std::bind在GCC-4.7.2,和整个使用成员函数指针,我有些不明白的语法来了.

我不明白的是_Maybe_wrap_member_pointer:

template<typename _Tp, typename _Class>
struct _Maybe_wrap_member_pointer<_Tp _Class::*> // note no comma here
Run Code Online (Sandbox Code Playgroud)
  • 为什么_Tp和之间没有逗号_Class::*

  • 由于成员函数void foo::bar()(在下面我示例应用程序),你会_Tp_Class::*决心吗?

下面是我的示例应用程序,它绑定成员函数指针和对象.(我已经提取了与std::bind成员函数的特化/内部相关的源代码)

#include <iostream>
#include <functional>

template<typename T>
struct _Maybe_wrap_member_pointer;

template<typename _Tp, typename _Class>
struct _Maybe_wrap_member_pointer<_Tp _Class::*> // <-- I don't understand this
{                                                // why not <_Tp, _Class::*>  
    typedef std::_Mem_fn<_Tp _Class::*> type;    

    static type __do_wrap(_Tp _Class::* __pm)  
    {
        return type(__pm);
    }
};

template<typename _Func, typename... …
Run Code Online (Sandbox Code Playgroud)

c++ templates stdbind c++11 std-function

2
推荐指数
1
解决办法
229
查看次数

使用std :: bind和std :: function

编辑3:如果我删除第二个createTriangle函数,它的工作原理.那么如何绑定重载函数呢?

我有一个函数,它接受一个带有一个参数的函数对象,如下所示:

int createObject(std::function<void(int)>);
Run Code Online (Sandbox Code Playgroud)

如何用std :: bind调用这个函数?我试过这样的:

createObject(std::bind(&ClassA::createTriangle, std::placeholders::_1, a, b, c, color));
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误:

候选模板被忽略:无法推断模板参数

ClassA :: createTriangle是一个静态函数.


附加信息:

void ClassA::createTriangle(int id, const glm::vec3 & a, const glm::vec3 & b, const glm::vec3 & c, const glm::vec3 & col) {
    /* ... */
}

int ClassB::createTriangle(const glm::vec3 & a, const glm::vec3 & b, const glm::vec3 & c, const glm::vec3 & color) {
    return classCInstance.createObject(std::bind(&classA::createTriangle, std::placeholders::_1, a, b, c, color));
}

int ClassC::createObject(std::function<void(int)> f) {
    f(++id);
    return id;
}
Run Code Online (Sandbox Code Playgroud)

ClassA中还有另一个静态createTriangle函数,它具有不同的last参数.那可能是个问题吗?绑定不知道选择哪个createTriangle?它看起来像这样:

void ClassA::createTriangle(int …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind c++11 std-function

2
推荐指数
1
解决办法
284
查看次数

std::bind 具有多个参数的成员函数

我有这个代码

struct A {
    void f(int) {}
    void g(int, double) {}
};

int main() {
    using std::placeholders;
    A a;

    auto f1 = std::bind(&A::f, &a, _1);
    f1(5);                                   //  <--- works fine

    auto f2 = std::bind(&A::g, &a, _1);
    f2(5, 7.1);                              //  <--- error!
}
Run Code Online (Sandbox Code Playgroud)

我从编译器(gcc 4.8.1)收到此错误:

error: no match for call to '(std::_Bind<std::_Mem_fn<void (A::*)(int, double)>(A*, std::_Placeholder<1>)>) (int, double)'
 f2(1, 1.1);
           ^  
Run Code Online (Sandbox Code Playgroud)

你能告诉我错误在哪里吗?

谢谢,

马西莫

bind member stdbind

2
推荐指数
1
解决办法
1606
查看次数

无法std :: bind成员函数

我写了以下课程:

class SomeClass {
private:
    void test_function(int a, size_t & b, const int & c) {
        b = a + reinterpret_cast<size_t>(&c);
    }
public:
    SomeClass() {
        int a = 17;
        size_t b = 0;
        int c = 42;
        auto test = std::bind(&SomeClass::test_function, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
        test(a, b, c);
    }
}
Run Code Online (Sandbox Code Playgroud)

就其本身而言,这段代码在IDE(Visual Studio 2015)中看起来很好,但是当我尝试编译它时,我收到以下错误:

Error   C2893   Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'    Basic Server    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits  1441    
Error   C2672   'std::invoke': no matching overloaded function found …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind c++11

2
推荐指数
1
解决办法
3751
查看次数

在 C++ 中使用 std::bind 和 std::function 时出错

我尝试在多元函数上尝试我的牛顿方法片段并使用std::bindstd::function。但我被一个错误困住了

错误:请求从 'std::_Bind_helper&, int>::type {aka std::_Bind, int))(double, double, double)>}' 转换为非标量类型 'std::function'

此错误消息是什么意思,我应该如何修复我当前的代码?

#include <iostream>
#include<functional>
#include<cmath>

double newton(std::function<double(double)> F, std::function<double(double)> f,
              double x=0, int maxiter=1000, double epsilon=0.001)
{
    int n = 0;
    while((n < maxiter) && (fabs(F(x)) > epsilon))
    {
        x = x - F(x) / f(x);
        n++;
    }
    return x;
}

// I'd like to fix x and z at 1 and 2 and find root for y
double ftest(double x, double y, double z) …
Run Code Online (Sandbox Code Playgroud)

c++ newtons-method stdbind c++11 std-function

2
推荐指数
1
解决办法
2974
查看次数

绑定std :: function错误

尝试使用std :: function和std :: bind绑定方法时遇到问题.

在我的CommunicationService类中:

this->httpServer->BindGET(std::bind(&CommunicationService::ManageGETRequest, this, std::placeholders::_1));
Run Code Online (Sandbox Code Playgroud)

CommunicationService :: ManageGetRequest签名:

MessageContent CommunicationService::ManageGetRequest(std::string uri, MessageContent msgContent)
Run Code Online (Sandbox Code Playgroud)

BindGET签名:

void RESTServer::BindGET(RequestFunction getMethod)
Run Code Online (Sandbox Code Playgroud)

RequestFunction typedef:

typedef std::function<MessageContent(std::string, MessageContent)> RequestFunction;
Run Code Online (Sandbox Code Playgroud)

BindGET上的错误:

错误C2664:'void RESTServer :: BindGET(RequestFunction)':无法从'std :: _ Binder <std :: _ Unforced,MessageContent(__cdecl communication :: CommunicationService ::*)(std :: string,MessageContent)转换参数1, communication :: CommunicationService*const,const std :: _ Ph <1>&>'to'RequestFunction'

之前,我的RequestFunction是这样的:

typedef std::function<void(std::string)> RequestFunction;
Run Code Online (Sandbox Code Playgroud)

它工作得很好.(当然,调整了所有签名方法).

我不明白导致错误的原因.

c++ stdbind c++11 std-function

2
推荐指数
1
解决办法
2027
查看次数

C++17 不能使用 std::bind 生成 std​​::function

我有一个Register将 astd::function<void(const uint8_t* data, size_t len)>作为参数的函数。我想使用对象的成员函数作为目标。

我发现这个问题的答案是用来std::bind将第一个参数(隐式this指针)绑定到实际对象指针,然后将其用作std::function参数。

然而,这在 C++11、C++14 和 C++17 中都不再起作用了?

考虑以下测试程序。

#include <iostream>
#include <cstdint>
#include <functional>

void Register(std::function<void(const uint8_t* data, size_t len)> func) {
    //Dummy - directly call into function
    func(nullptr, 0);
}

class TestClass {
public:
    void TestRegister() {
         Register(
                 std::bind(&TestClass::TestTarget, this, std::placeholders::_1)
         );
    }

    void TestTarget(const uint8_t* data, size_t len) {
        (void) data;
        (void) len;
        std::cout << "Hello there" << std::endl;
    }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ functional-programming stdbind c++17

2
推荐指数
1
解决办法
2120
查看次数

为什么std :: bind静态类型检查参数传递给函数?

这不是一个问题,而是一个沉思......我写了一个程序来测试'std :: bind'如何将参数传递给被绑定的函数.在这种情况下,C++编译器似乎执行静态类型检查:

#include <iostream>
#include <functional>

void take_int(const int *a)
{
    if (a)
    {
        std::cout << "a is " << *a << std::endl;
    }
    else
    {
        std::cout << "null" << std::endl;
    }
}

int main()
{
    int *a = new(int);
    *a = 4;
    take_int(NULL); //prints 'null'
    auto fn = std::bind(take_int, NULL); //fails to compile
    fn();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

似乎不一致的是能够直接用NULL调用该函数,但是在编译时它通过std :: bind失败了.

我猜std :: bind正在使用更现代的C++功能选择强制执行此操作?

c++ stdbind

2
推荐指数
1
解决办法
51
查看次数

std::function 和 std::bind 返回值

我试图了解 std::bind 和 std::function 是如何工作的。我无法编译以下代码:

#include <iostream>
#include <string>
#include <functional>

void function(int a, float b, std::string const &s)
{
    std::cout << "printing function" << std::endl;
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << s << std::endl;
}

int main(int argc, char *argv[])
{
    std::bind(&function, 10, 11.1, "hello")();
    std::function<void(int, float, std::string const&)> fun = std::bind(&function, 10, std::placeholders::_1, std::placeholders::_2);

    fun(0.2, "world");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨说:

main.cpp: In function 'int main(int, char**)':
main.cpp:16:69: error: conversion from 'std::_Bind_helper<false, void …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind std-function

2
推荐指数
1
解决办法
4167
查看次数

如何使用模板而不是宏来绑定“ this”?

我试图用模板替换代码中的所有宏。我有一个宏,它绑定this到函数的第一个参数,以便可以在静态上下文中使用成员函数。如何使用模板来实现?

我想要此功能:

#define BIND_EVENT_FN(func) std::bind(&func, this, std::placeholders::_1)
Run Code Online (Sandbox Code Playgroud)

或使用lambda:

#define BIND_EVENT_FN(func) [this](Event& e){ return func(e); }
Run Code Online (Sandbox Code Playgroud)

与此类似的东西,但显然不完全像这样,因为它不能编译:

template<class T>
auto bind_event_fn(T& func)
{
    return [this](Event&& e){ return func(e); };
}
Run Code Online (Sandbox Code Playgroud)

下面的最小工作示例,是否可以替换宏?

#include <functional>
#include <iostream>

#define LAMBDA_BIND_FN(fn) [this](const int& event) { return fn(event); }
#define BASIC_BIND_FN(func) std::bind(&func, this, std::placeholders::_1)

void run_in_static_context(std::function<void(const int&)> fn)
{
    fn(42);
}

class A {
  public:
    A()
    {
        run_in_static_context(LAMBDA_BIND_FN(A::member_fn));
    }
  private:
    void member_fn(const int& event)
    { 
        std::cout << "Event: " << event << std::endl;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ lambda stdbind c++17

2
推荐指数
1
解决办法
108
查看次数