C++ 返回自动返回类型的std::函数

C++ 返回自动返回类型的std::函数,c++,types,return,auto,std-function,C++,Types,Return,Auto,Std Function,我想创建一个函子,将std::string转换为不同的类型 std::function<auto(const std::string)> create(const std::string &type) { if(type=="int") { return [&](const std::string &value){return std::stoi(value);} } else if(type=="float&qu

我想创建一个函子,将std::string转换为不同的类型

std::function<auto(const std::string)> create(const std::string &type)
{
  if(type=="int") {
    return [&](const std::string &value){return std::stoi(value);}
  } else if(type=="float") {
    return [&](const std::string &value){return std::stof(value);}
  }  else {
    throw std::runtime_error("");
  }
}
std::函数创建(常量std::字符串和类型)
{
如果(类型=“int”){
return[&](const std::string&value){return std::stoi(value);}
}else if(类型==“浮动”){
return[&](const std::string&value){return std::stof(value);}
}否则{
抛出std::runtime\u错误(“”);
}
}
但是我们似乎不能在这里使用auto作为std::function的返回类型


有人能建议一种方法吗?

对于一个函数或函数模板的一个实例化,返回类型必须相同且固定。您可以将函数模板设置为

template <typename R>
std::function<R(const std::string&)> create()
{
  if(std::is_same<R, int>::value) {
    return [](const std::string &value){return std::stoi(value);};
  } else if(std::is_same<R, float>::value) {
    return [](const std::string &value){return std::stof(value);};
  }  else {
    throw std::runtime_error("");
  }
}

我认为在
std::function
中不可能有自动返回类型推断

如果您需要存储一个调用
std::sto*
函数的lambda/functor,我已经想出了一些替代方法。只需创建一个模板本身并用它替换
auto
。这允许您删除
const std::string&
type参数

#include <type_traits> // for std::is_same

template <typename T = int>
constexpr std::function<T(const std::string)> create()
{
    if (std::is_same<T, int>::value)   
    { return [&](const std::string &value) { return std::stoi(value); }; }

    if (std::is_same<T, float>::value) 
    { return [&](const std::string &value) { return std::stof(value); }; }

    throw std::runtime_error("");
}
#include//for std::是否相同
模板
constexpr std::函数create()
{
if(std::is_same::value)
{return[&](const std::string&value){return std::stoi(value);};}
if(std::is_same::value)
{return[&](const std::string&value){return std::stof(value);};}
抛出std::runtime\u错误(“”);
}
或者你可以自己创建一个函子类

#include <type_traits> // for std::is_same

template <typename T = int>
struct Functor {

    T operator()(const std::string& input) {
        if (std::is_same<T, int>::value)   { return std::stoi(input); };
        if (std::is_same<T, float>::value) { return std::stof(input); };
        throw std::runtime_error("");
    }

};
#include//for std::是否相同
模板
结构函子{
T运算符()(常量std::字符串和输入){
如果(std::is_same::value){返回std::stoi(输入);};
如果(std::is_same::value){return std::stof(input);};
抛出std::runtime\u错误(“”);
}
};
以上的用法

int main() {
    
    auto f_i = create();
    auto f_f = create<float>();
    f_i("42");   // returns an int
    f_f("3.14"); // returns a float
    
    Functor fo_i;
    Functor<float> fo_f;
    fo_i("42");   // returns an int
    fo_f("3.14"); // returns a float
    
}

intmain(){
自动f_i=创建();
自动f_f=create();
f_i(“42”);//返回一个int
f_f(“3.14”);//返回一个浮点值
函子foui;
函子fo_f;
fo_i(“42”);//返回一个int
fo_f(“3.14”);//返回一个浮点值
}

我建议简单地调用正确的数值转换函数,而不是存储一个调用
std::sto*
的lambda/functor,这将是一个更好的方法。

您可以使用您真正想做的事情?你愿意使用模板和模板专门化吗?我有一些不同类型的数据作为字符串。所以我们需要投下他们。我可以使用模板该参数已过时now@SimonKraemer是的,修正了。这不行。我拥有的类型信息和值实际上是字符串格式的。因此,我必须首先检查类型字符串以生成适当的强制转换函子,然后使用它将值字符串强制转换为适当的type@shaaa因此,您希望在运行时根据字符串的内容更改返回类型。然后你必须像@SimonKraemer评论的那样使用
std::variant
std::any
。即使在函数返回时,您也必须知道确切的类型,否则无法从中获取值。
#include <type_traits> // for std::is_same

template <typename T = int>
constexpr std::function<T(const std::string)> create()
{
    if (std::is_same<T, int>::value)   
    { return [&](const std::string &value) { return std::stoi(value); }; }

    if (std::is_same<T, float>::value) 
    { return [&](const std::string &value) { return std::stof(value); }; }

    throw std::runtime_error("");
}
#include <type_traits> // for std::is_same

template <typename T = int>
struct Functor {

    T operator()(const std::string& input) {
        if (std::is_same<T, int>::value)   { return std::stoi(input); };
        if (std::is_same<T, float>::value) { return std::stof(input); };
        throw std::runtime_error("");
    }

};
int main() {
    
    auto f_i = create();
    auto f_f = create<float>();
    f_i("42");   // returns an int
    f_f("3.14"); // returns a float
    
    Functor fo_i;
    Functor<float> fo_f;
    fo_i("42");   // returns an int
    fo_f("3.14"); // returns a float
    
}