使用 switch 的自动函数返回值
Auto function returning value using switch
我正在尝试创建一个 return 类型应该依赖于 switch 语句的函数,例如:
auto function_name (int value) {
switch (value) {
case 1 : {return 2.3;}
case 2 : {return 1;}
case 3 : {return "string";}
}
}
但是我不能因为一个错误:
error: inconsistent deduction for auto return type: 'double' and then 'int'
我可以做些什么来创建功能与上述示例类似的东西?
错误信息说明了一切:函数的所有分支必须return相同的类型。此限制并非特定于 auto
return 类型。
一个可能的修复:
std::variant<double, int, std::string> function_name(int value) {
switch(value) {
case 1 : return 2.3;
case 2 : return 1;
case 3 : return "string";
default: throw;
}
}
或者,您可以使用 boost::variant
。
C++ 中的一个函数只能有一个它 return 的类型。如果您使用 auto
作为 return 类型,并且您有不同的 return 语句 return 不同的类型,那么代码是错误的,因为它违反了单一类型规则。
这里需要用到std::variant
or std::any
。如果你有几种不同的类型可以通过一些 运行 时间值进行 returned,那么你可以将这些类型中的任何一种用作 "generic type"。 std::variant
更严格,因为您必须指定它可能的类型,但它也比 std::any
便宜,因为您知道它可能是什么类型。
std::variant<double, int, std::string> function_name (int value) {
using namespace std::literals::string_literals;
switch (value) {
case 1 : {return 2.3;}
case 2 : {return 1;}
case 3 : {return "string"s;} // use ""s here to force it to be a std::string
}
}
会让你return种不同的类型。
如果函数参数在编译时已知,您可以使用编译时分派,例如
template <int N>
constexpr auto function_name()
{
if constexpr(N == 1)
return 2.3;
else if constexpr (N == 2)
return 1;
else
return "string";
}
可以如下实例化调用
std::cout << function_name<1>() << "\n";
C++17 对于 if constexpr
部分是必需的。请注意,将 return 值绑定到变量时,请仔细选择类型(例如,不要意外地将 double
隐式转换为 int
),使用类型推导或 variant
-如现有答案所示输入。
请注意,正如@NathanOliver 在评论中指出的那样,还有一个 C++17 之前的解决方案,它使用模板特化而不是 if constexpr
:
template <int N> constexpr auto function_name() { return "string"; }
template <> constexpr auto function_name<1>() { return 2.3; }
template <> constexpr auto function_name<2>() { return 1; }
此模板的用法及其特化与上述没有区别。
我正在尝试创建一个 return 类型应该依赖于 switch 语句的函数,例如:
auto function_name (int value) {
switch (value) {
case 1 : {return 2.3;}
case 2 : {return 1;}
case 3 : {return "string";}
}
}
但是我不能因为一个错误:
error: inconsistent deduction for auto return type: 'double' and then 'int'
我可以做些什么来创建功能与上述示例类似的东西?
错误信息说明了一切:函数的所有分支必须return相同的类型。此限制并非特定于 auto
return 类型。
一个可能的修复:
std::variant<double, int, std::string> function_name(int value) {
switch(value) {
case 1 : return 2.3;
case 2 : return 1;
case 3 : return "string";
default: throw;
}
}
或者,您可以使用 boost::variant
。
C++ 中的一个函数只能有一个它 return 的类型。如果您使用 auto
作为 return 类型,并且您有不同的 return 语句 return 不同的类型,那么代码是错误的,因为它违反了单一类型规则。
这里需要用到std::variant
or std::any
。如果你有几种不同的类型可以通过一些 运行 时间值进行 returned,那么你可以将这些类型中的任何一种用作 "generic type"。 std::variant
更严格,因为您必须指定它可能的类型,但它也比 std::any
便宜,因为您知道它可能是什么类型。
std::variant<double, int, std::string> function_name (int value) {
using namespace std::literals::string_literals;
switch (value) {
case 1 : {return 2.3;}
case 2 : {return 1;}
case 3 : {return "string"s;} // use ""s here to force it to be a std::string
}
}
会让你return种不同的类型。
如果函数参数在编译时已知,您可以使用编译时分派,例如
template <int N>
constexpr auto function_name()
{
if constexpr(N == 1)
return 2.3;
else if constexpr (N == 2)
return 1;
else
return "string";
}
可以如下实例化调用
std::cout << function_name<1>() << "\n";
C++17 对于 if constexpr
部分是必需的。请注意,将 return 值绑定到变量时,请仔细选择类型(例如,不要意外地将 double
隐式转换为 int
),使用类型推导或 variant
-如现有答案所示输入。
请注意,正如@NathanOliver 在评论中指出的那样,还有一个 C++17 之前的解决方案,它使用模板特化而不是 if constexpr
:
template <int N> constexpr auto function_name() { return "string"; }
template <> constexpr auto function_name<1>() { return 2.3; }
template <> constexpr auto function_name<2>() { return 1; }
此模板的用法及其特化与上述没有区别。