C++ 我应该如何定义带有默认参数的std::函数变量?

C++ 我应该如何定义带有默认参数的std::函数变量?,c++,c++11,lambda,std-function,C++,C++11,Lambda,Std Function,要将std::function变量设置为具有默认参数的lambda函数,我可以使用auto,如下所示: auto foo = [](int x = 10){cout << x << endl;}; foo(); 用std::function替换auto struct Bar { std::function<void(int x = 10)> foo = [](int x = 10}(cout << x << endl}; //

要将std::function变量设置为具有默认参数的lambda函数,我可以使用
auto
,如下所示:

auto foo = [](int x = 10){cout << x << endl;};
foo();
用std::function替换
auto

struct Bar
{
    std::function<void(int x = 10)> foo = [](int x = 10}(cout << x << endl}; //error: default arguments are only permitted for function parameters
};
Bar bar;
bar.foo();

那么我应该如何声明foo呢?

不知道这是否对您有帮助,但您可以将lambda存储在模板结构中

template <typename F>
struct Bar {
  F foo;
  Bar (F fun): foo (std::move (fun)) {}
};

auto f = [](int x = 10) {cout << x << endl;};
Bar<decltype (f)> bar (f);
bar.foo();

auto makeFun = [](){return [](int x = 10) {cout << x << endl;};};

Bar<decltype (makeFun())> bar2 (makeFun());
bar2.foo();
模板
结构条{
F foo;
酒吧(F fun):foo(std::move(fun)){}
};

auto f=[](int x=10){coutstd::function
中的签名基于您计划如何调用它,而不是基于您如何构造/分配它。由于您希望以两种不同的方式调用它,因此需要存储到不同的
std::function
对象,如:

struct Call
{
    template<typename F>
    explicit Call(F f) : zero_(f), one_(std::move(f)) {}

    void operator()() { zero_(); }
    void operator()(int i) { one_(i); }

    std::function<void()>    zero_;
    std::function<void(int)> one_;
};

解决此问题的一种方法是将
std::function
包装在functor对象中,该对象为您实现默认参数:

struct MyFunc
{
    void operator()(int x = 10) { f(x); }
    std::function<void(int x)> f;
};

struct Bar
{
    MyFunc foo = {[](int x){std::cout << x << "\n";}};
};

int main() {
    Bar bar;
    bar.foo();
}
struct MyFunc
{
void运算符()(int x=10){f(x);}
std::函数f;
};
结构条
{

MyFunc foo={[](int x){std::cout在C++20中,您将能够执行以下操作:

struct foo {
    decltype([](int a = 10){std::cout << a << '\n';}) my_lambda{};
};

int main() {
    foo f;
    f.my_lambda();
    f.my_lambda(5);
}
structfoo{

decltype([](int a=10){std::cout无法使用默认参数生成
std::function
struct Call
{
    template<typename F>
    explicit Call(F f) : zero_(f), one_(std::move(f)) {}

    void operator()() { zero_(); }
    void operator()(int i) { one_(i); }

    std::function<void()>    zero_;
    std::function<void(int)> one_;
};
class TECall
{
    struct Concept
    {   
        Concept() = default;
        Concept(Concept const&) = default;
        virtual ~Concept() = default;

        virtual Concept* clone() const = 0;

        virtual void operator()() = 0;
        virtual void operator()(int) = 0;
    };  

    template<typename T>
    struct Model final : Concept
    {   
        explicit Model(T t) : data(std::move(t)) {}
        Model* clone() const override { return new Model(*this); }

        void operator()() override { data(); }
        void operator()(int i) override { data(i); }

        T data;
    };  

    std::unique_ptr<Concept> object;

public:
    template<typename F>
    TECall(F f) : object(new Model<F>(std::move(f))) {}

    TECall(TECall const& that) : object(that.object ? that.object->clone() : nullptr) {}
    TECall(TECall&& that) = default;
    TECall& operator=(TECall that) { object = std::move(that.object); return *this; }

    void operator()() { (*object)(); }
    void operator()(int i) { (*object)(i); }
};
struct MyFunc
{
    void operator()(int x = 10) { f(x); }
    std::function<void(int x)> f;
};

struct Bar
{
    MyFunc foo = {[](int x){std::cout << x << "\n";}};
};

int main() {
    Bar bar;
    bar.foo();
}
struct foo {
    decltype([](int a = 10){std::cout << a << '\n';}) my_lambda{};
};

int main() {
    foo f;
    f.my_lambda();
    f.my_lambda(5);
}