相关疑难解决方法(0)

将捕获lambda作为函数指针传递

是否可以将lambda函数作为函数指针传递?如果是这样,我必须做错了,因为我收到编译错误.

请考虑以下示例

using DecisionFn = bool(*)();

class Decide
{
public:
    Decide(DecisionFn dec) : _dec{dec} {}
private:
    DecisionFn _dec;
};

int main()
{
    int x = 5;
    Decide greaterThanThree{ [x](){ return x > 3; } };
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我得到以下编译错误:

In function 'int main()':
17:31: error: the value of 'x' is not usable in a constant expression
16:9:  note: 'int x' is not const
17:53: error: no matching function for call to 'Decide::Decide(<brace-enclosed initializer list>)'
17:53: note: candidates are: …
Run Code Online (Sandbox Code Playgroud)

c++ lambda function-pointers c++11

186
推荐指数
4
解决办法
12万
查看次数

std :: function的性能开销是多少?

我在论坛上听到使用std::function<>原因性能下降.这是真的吗?如果是真的,这是一个很大的性能下降?

c++ boost std

61
推荐指数
4
解决办法
3万
查看次数

std :: function <>和标准函数指针之间的区别?

什么是std :: function <>和标准函数指针之间的区别?

那是:

typedef std::function<int(int)> FUNCTION;
typedef int (*fn)(int);
Run Code Online (Sandbox Code Playgroud)

它们实际上是一回事吗?

c++ c++11

56
推荐指数
4
解决办法
3万
查看次数

40
推荐指数
3
解决办法
2万
查看次数

C++使用函数作为参数

可能重复:
如何在C中将函数作为参数传递?

假设我有一个叫做的函数

void funct2(int a) {

}


void funct(int a, (void)(*funct2)(int a)) {

 ;


}
Run Code Online (Sandbox Code Playgroud)

调用此函数的正确方法是什么?我需要设置什么才能让它工作?

c++

11
推荐指数
2
解决办法
3万
查看次数

现代c ++替代函数指针

到目前为止,我一直在使用函数指针,就像c ++中的这种格式一样.我偶尔会有一些用途,我想知道在c ++ 11/14中还有其他什么作为替代方案.

#include <iostream>
using namespace std;

void sayHello();
void someFunction(void f());

int main() {

    someFunction(sayHello);
    return 0;
}
void sayHello(){
    std::cout<<"\n Hello World";
}
void someFunction(void f()){
    f();
} 
Run Code Online (Sandbox Code Playgroud)

我确实看过这个问题,但是无法理解任何优于传统使用函数指针的优点.另外我想问一下,使用函数指针有什么不对(不推荐)的事情因为我从未见过有人使用它们.或任何其他替代礼物.

c++ pointers function-pointers c++11

11
推荐指数
3
解决办法
6853
查看次数

比函数引用更有效的方法?

我有一个使用函数引用的类:

double u( const double& x, const double& y )
{
  return x * y;
}

class equation
{
  equation( double (&in_u)(const double&, const double&) );
//...
protected:
  double (&u)(const double&, const double&);
}
Run Code Online (Sandbox Code Playgroud)

该功能将被称为像10周8典型的运行过程中的时间.

该类进入库,该函数u由库的用户定义.所以我不能在类中有函数定义.

我读过这个:

(std::function)...的缺点是在被调用时引入一些(非常小的)开销(所以在一个非常严重的性能问题上它可能是一个问题,但在大多数情况下它不应该)

有没有更有效的方法将函数传递u给类equation?这会被视为"一个非常严重的性能危机"吗?

编辑

似乎有点混乱.只是要清楚,该功能u 在可执行文件编译时已知,但不是在图书馆的.在运行时获取该功能是我将在库的更高版本中考虑的功能,但现在不是.

c++ performance reference class c++11

8
推荐指数
1
解决办法
252
查看次数

std :: function vs function pointer

  • 有什么不同吗?

  • 哪种"保存/转移"功能最好?

    function<void(int)> fcn = 
                        [](int par) {std::cout<<"fcn: "<<par<<std::endl; };
    void(*fcn_a)(int) = 
                        [](int par) {std::cout<<"fcn_a: "<<par<<std::endl; };
    
    
    fcn(12);
    fcn_a(12);
    
    Run Code Online (Sandbox Code Playgroud)

c++ function-pointers std-function

4
推荐指数
2
解决办法
2438
查看次数

如何理解此声明中的typedef

最近,我读了有效C++这本书,第35项中有关于typedef的声明使我感到困惑.

class GameCharacter; // Question1: Why use forward declaration?

int defaultHealthCalc(const GameCharacter& gc);

class GameCharacter{

    public:
        typedef int (*HealthCalcFunc)(const GameCharacter&); // Question 2: What does this mean?

        explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc)
            : healthFunc(hcf)
        {}

        int healthValue() const
        {return healthFunc(*this); }

    private:
        HealthCalcFunc healthFunc;
};
Run Code Online (Sandbox Code Playgroud)

所以我的第一个问题是:为什么作者在这里使用前瞻性声明?有什么具体原因吗?

我的第二个问题是:我如何理解typedef声明,以及如何使用它?我只知道类似的东西typedef int MyInt;

c++ typedef forward-declaration effective-c++

4
推荐指数
4
解决办法
942
查看次数

你可以传递一个功能,以便以后可以调用它吗?

我希望对象有一个调用函数的方法(但每个对象应该有一个不同的函数来调用).我将通过展示一个例子来向您展示我的意思:

class Human
{
    public:
        void setMyFunction(void func);  // specify which function to call
        void callMyFunction();  // Call the specified function
};

void Human::setMyFunction(void func)    // ''
{
    myFunction = func;
}

void Human::callMyFunction()    // ''
{
    myFunction();
}

void someRandomFunction()   // A random function
{
    // Some random code
}

int main()
{
    Human Lisa;     // Create Object
    Lisa.setMyFunction();   // Set the function for that object
    Lisa.callMyFunction();  // Call the function specified earlier
}
Run Code Online (Sandbox Code Playgroud)

这个代码(显然)不起作用,但我希望你理解我想要完成的事情.

MfG,TPRammus

c++ class function object

3
推荐指数
1
解决办法
82
查看次数

将继承的方法传递给另一个方法

我正在尝试使用方法作为参数构建一个具有成员函数的类.这些方法在继承的类中定义.我构建了一个最小的例子:

#include <iostream>

struct base
{
    base() {}

    int number(int (*f)(int))
    {
        return f(1);
    }
};

struct option1 : base 
{
    int timesTwo(int i){return 2*i;}
    option1() 
    {
        std::cout << number(timesTwo);
    }
};

struct option2 : base
{
    int timesThree(int i){return 3*i;}
    int timesFour (int i){return 4*i;}
    option2() 
    {
        std::cout << number(timesThree);
    }
};

int main()
{
    option1 a; //I would expect this to print "2"
}
Run Code Online (Sandbox Code Playgroud)

函数中的当前语法number是针对一般函数的,但我无法使其适用于任何继承类的方法.

c++ methods inheritance

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

使用std :: function的优点

亲爱的stackoverflow用户.

我试图找到优点std::function,为什么我应该使用它而不是函子或指针来运行.可悲的是,我找不到令人满意的答案.

这就是为什么我会非常感激,如果你能告诉我什么是优点,什么std::function时候我应该使用它.希望我的问题能够帮助其他人.

c++ c++11 c++14

0
推荐指数
1
解决办法
1113
查看次数