如何重载简单的本地 lambda 函数?
原始问题的SSE:
#include <iostream>
#include <map>
void read()
{
static std::string line;
std::getline(std::cin, line);
auto translate = [](int idx)
{
constexpr static int table[8]{ 7,6,5,4,3,2,1,0 };
return table[idx];
};
auto translate = [](char c)
{
std::map<char, int> table{ {'a', 0}, {'b', 1}, {'c', 2}, {'d', 3},
{'e', 4}, {'f', 5}, {'g', 6}, {'h', 7} };
return table[c];
};
int r = translate(static_cast<int>(line[0]));
int c = translate(static_cast<char>(line[1]));
std::cout << r << c << std::endl;
}
int main()
{
read();
return 0;
}
错误信息
error: conflicting declaration 'auto translate'
note: previous declaration as 'read()::<lambda(int)> translate'
请不要介意不检查用户输入,这是一个 SSE。
最佳答案
不,你不能重载 lambda!
lambda 表达式为 anonymous functors (即未命名的函数对象),而不是简单的函数。因此,不可能重载这些对象。 你基本上想做的几乎是
struct <some_name>
{
int operator()(int idx) const
{
return {}; // some int
}
}translate; // >>> variable name
struct <some_name>
{
int operator()(char idx) const
{
return {}; // some int
}
}translate; // >>> variable name
这是不可能的,因为在 C++ 中不能重复使用相同的变量名。
<小时/>但是,在 c++17我们有if constexpr
通过它可以实例化唯一在编译时为 true 的分支。
这意味着可能的解决方案是:
- 单个variabe template lambda 。或
- 通用 lambda 并使用
decltype
查找参数的类型 用于if constexpr
检查。(来源 @NathanOliver)
使用variabe template你可以做类似的事情。 ( See a live demo online )
#include <type_traits> // std::is_same_v
template<typename T>
constexpr auto translate = [](T idx)
{
if constexpr (std::is_same_v<T, int>)
{
constexpr static int table[8]{ 7,6,5,4,3,2,1,0 };
return table[idx];
}
else if constexpr (std::is_same_v<T, char>)
{
std::map<char, int> table{ {'a', 0}, {'b', 1}, {'c', 2}, {'d', 3}, {'e', 4}, {'f', 5}, {'g', 6}, {'h', 7} };
return table[idx];
}
};
并这样调用它
int r = translate<int>(line[0]);
int c = translate<char>(line[1]);
<小时/>
使用泛型 lambda(自 c++14 起),以上将是:( See a live demo online )
#include <type_traits> // std::is_same_v
constexpr auto translate = [](auto idx)
{
if constexpr (std::is_same_v<decltype(idx), int>)
{
constexpr static int table[8]{ 7,6,5,4,3,2,1,0 };
return table[idx];
}
else if constexpr (std::is_same_v<decltype(idx), char>)
{
std::map<char, int> table{ {'a', 0}, {'b', 1}, {'c', 2}, {'d', 3}, {'e', 4}, {'f', 5}, {'g', 6}, {'h', 7} };
return table[idx];
}
};
并像现在一样调用 lambda:
int r = translate(static_cast<int>(line[0]));
int c = translate(static_cast<char>(line[1]));
关于c++ - 重载 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58700542/