Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回类型未知的Std函数_C++_Std Function - Fatal编程技术网

C++ 返回类型未知的Std函数

C++ 返回类型未知的Std函数,c++,std-function,C++,Std Function,我试图在不直接知道返回类型的情况下声明std函数 显然,它将在编译时被知道,但我找不到正确的方法来声明它 最重要的是,我需要创建一个容器,其中将包含此函数返回的值 template <typename... Args> class Sample { public: Sample(Args... args, std::function</*unknown return type*/(Args...)> fnct) : _inputBuff(std::

我试图在不直接知道返回类型的情况下声明std函数

显然,它将在编译时被知道,但我找不到正确的方法来声明它

最重要的是,我需要创建一个容器,其中将包含此函数返回的值

template <typename... Args>
class Sample
{
public:
    Sample(Args... args, std::function</*unknown return type*/(Args...)> fnct) :
        _inputBuff(std::forward_as_tuple(std::forward<Args>(args)...))

    { }

    std::tuple<Args...>                     _inputBuff;
    std::vector</*unknown return type*/>    _resultBuff;

};
模板
类样本
{
公众:
示例(Args…Args,std::function fnct):
_inputBuff(std::forward作为元组(std::forward(args)…)
{ }
std::tuple\u inputBuff;
std::vector_resultBuff;
};

有什么想法吗?

您也不知道函数参数,并使用模板参数(好吧,参数包,因为有零个或多个)解决了这个问题

只需对返回类型执行相同的操作

template <typename ReturnType, typename... Args>
class Sample
{
public:
    Sample(Args... args, std::function<ReturnType(Args...)> fnct) :
        _inputBuff(std::forward_as_tuple(args...))

    { }

    std::tuple<Args...>     _inputBuff;
    std::vector<ReturnType> _resultBuff;
};
模板
类样本
{
公众:
示例(Args…Args,std::function fnct):
_inputBuff(标准::转发作为元组(args…)
{ }

std::tuple)您可以在模板参数中接受可调用类型,而不是参数列表。这将允许您发送任何函数或函数对象并检查返回类型:

template<typename F, typename... Args>
struct Sample {
    Sample(F fnct, Args... args) :
        _inputBuff{std::forward_as_tuple(std::forward<Args>(args)...)} { }

    using Result = std::result_of_t<F(Args...)>;

    std::tuple<Args...> _inputBuff;
    std::vector<Result> _resultBuff;
};
模板
结构样本{
样本(F fnct,Args…Args):
_inputBuff{std::forward\\作为元组(std::forward(args)…)}{}
使用Result=std::Result\u of\u t;
std::tuple\u inputBuff;
std::vector_resultBuff;
};
为了简化该类的构造,可以引入一个make函数来推断参数(注意,它可以成为C++17中的推断指南):

模板
自动生成示例(F&&fnct,参数&&…参数){
返回样本{std::forward(f),std::forward(args)…};
}

什么时候知道,如何知道?@LightnessRacesinOrbit我正试图找到一种优雅的方法来做,但是,是的,这是一种选择。@Wimps:似乎是一个很简单的问题,不是吗?@Jarod42何时我会调用constructructor@Wimps:那没有任何意义。你调用了一个类型的构造函数。在你定义它之前你没有类型。因此你不能我同意,没有其他选择。Thx!@Wimps:不知道你为什么要寻找任何类型。你认为这种传统方法会有问题吗?我想找到一种方法来声明我的函数和容器,而不询问用户需要输入的类型it@Wimps:那么你在做什么ELAI询问是如何在调用站点获得模板参数的推导。@ WMPS:这是C++的一个限制。
template<typename F, typename... Args>
auto makeSample(F&& fnct, Args&&... args) {
    return Sample<std::decay_t<F>, Args...>{std::forward<F>(f), std::forward<Args>(args)...};
}