GVKun编程网logo

是否可以编写一个将自身作为类型返回的c std :: function?(编写一个程序,要求能够将元素为任意)

3

如果您对是否可以编写一个将自身作为类型返回的cstd::function?和编写一个程序,要求能够将元素为任意感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解是否可以编写一个将自身作为类型返回的

如果您对是否可以编写一个将自身作为类型返回的c std :: function?编写一个程序,要求能够将元素为任意感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解是否可以编写一个将自身作为类型返回的c std :: function?的各种细节,并对编写一个程序,要求能够将元素为任意进行深入的分析,此外还有关于c – GCC错误与否:默认std :: function?、c – std :: async可以调用std :: function对象吗?、c – std :: bind to std :: function?、c – std :: function可以序列化吗?的实用技巧。

本文目录一览:

是否可以编写一个将自身作为类型返回的c std :: function?(编写一个程序,要求能够将元素为任意)

是否可以编写一个将自身作为类型返回的c std :: function?(编写一个程序,要求能够将元素为任意)

参见英文答案 > Recursive typedef function definition : std::function returning its own type                                    3个
让我们从函数的简单typedef开始:

typedef std::function<uint32_t(SomeStruct *)> HandlerFunction;

这里我们有一个返回uint32_t的函数,并获取指向SomeStruct的指针.

我想要做的是修改这个函数,使它返回一个自己的类型,如下所示:

typedef std::function<HandlerFunction(SomeStruct *)> HandlerFunction;

显然,这不会编译,但它代表了我正在努力实现的最终目标.

解决方法

我不知道如何用std :: function实现这一点.

但是,此代码(使用仿函数)在Visual Studio 2017中编译并运行:

struct SomeStruct {};

struct Func
{
   Func operator()(SomeStruct*) { return *this; }
};

int main()
{
   Func f;
   Func f2 = f(nullptr);
}

c – GCC错误与否:默认std :: function?

c – GCC错误与否:默认std :: function?

如何将默认函数指定为类成员的参数?

从我的代码派生的当前示例是:

#include <iostream>
#include <functional>

template<typename T> struct C
{
    static T test(std::function<T(int)> f = [](int i){return i;})
    {return f(42);}
};

int main(int argc,char* argv[])
{
    C<int>::test(); // ERROR = internal compiler error : in tsubst_copy,at cp/pt.c:11354
    C<int>::test([](int i){return i;}); // OK
    return 0;
}

这是GCC的错误吗?

用另一种语法可以避免这个问题吗?

你能在其他C 11编译器上尝试吗(对于那些有编译器的人)?

解决方法

毫无疑问,这是一个编译器错误.无论您的程序是否格式正确,编译器都会检测到自己的数据结构不一致.

请遵循GCC错误报告说明:http://gcc.gnu.org/bugs/#report

c – std :: async可以调用std :: function对象吗?

c – std :: async可以调用std :: function对象吗?

是否可以使用std :: async调用使用std :: bind创建的函数对象.以下代码无法编译:
#include <iostream>
#include <future>
#include <functional>

using namespace std;

class Adder {
public:
    int add(int x,int y) {
        return x + y;
    }
};

int main(int argc,const char * argv[])
{
    Adder a;
    function<int(int,int)> sumFunc = bind(&Adder::add,&a,1,2);
    auto future = async(launch::async,sumFunc); // ERROR HERE
    cout << future.get();
    return 0;
}

错误是:

没有用于调用’async’的匹配函数:
候选模板被忽略:替换失败[使用Fp = std :: _ 1 :: function&,Args =<>]:’std :: _ 1 :: __ invoke_of,>中没有名为’type’的类型

是不是可以与std :: function对象使用异步,或者我做错了什么?

(这是使用Xcode 5和Apple LLVM 5.0编译器编译的)

解决方法

Is it possible to call function objects created with std::bind using std::async

是的,只要您提供正确数量的参数,就可以调用任何仿函数.

am I doing something wrong?

您将绑定函数(不带参数)转换为函数< int(int,int)>,它接受(并忽略)两个参数;然后尝试在没有参数的情况下启动它.

您可以指定正确的签名:

function<int()> sumFunc = bind(&Adder::add,2);

或者避免创建函数的开销:

auto sumFunc = bind(&Adder::add,2);

或根本不打扰绑定:

auto future = async(launch::async,&Adder::add,2);

或者使用lambda:

auto future = async(launch::async,[]{return a.add(1,2);});

c – std :: bind to std :: function?

c – std :: bind to std :: function?

我使用这个编译错误:
std::vector<std::function<int(int)>> functions;

std::function<int(int,int)> foo = [](int a,int b){};
std::function<int(int)> bar = std::bind(foo,2);

functions.push_back(bar);

我得到的错误是:

/usr/include/c++/4.6/functional:1764:40: error: no match for call to '(std::_Bind<std::function<int(int,int)>(int)>) (int)'

有人可以告诉我如何将std :: bind转换成std ::函数?

解决方法

std::function<int(int)> bar = std::bind(foo,2,std::placeholders::_1);

c – std :: function可以序列化吗?

c – std :: function可以序列化吗?

这是一个理论问题.假设存在一些对象,其中包含订阅这些对象的事件的回调函数列表.现在我们要将这些对象存储在磁盘上. std :: function是否可序列化?

解决方法

没有.

每当使用类型擦除(即隐藏接口背后的实现细节)时,在不知道对象的动态类型的情况下可用的唯一操作是接口提供的操作.

C标准中没有序列化,也没有简单的方法来序列化函数(没有反射),因此std :: function接口不提供序列化.

另一方面,没有什么能阻止您使用提供序列化支持的Callback基类.

今天关于是否可以编写一个将自身作为类型返回的c std :: function?编写一个程序,要求能够将元素为任意的分享就到这里,希望大家有所收获,若想了解更多关于c – GCC错误与否:默认std :: function?、c – std :: async可以调用std :: function对象吗?、c – std :: bind to std :: function?、c – std :: function可以序列化吗?等相关知识,可以在本站进行查询。

本文标签: