问题描述
我试图查看在C ++ 14通用lambda中是否可行,但是我找不到表达它的正确方法(或者不可能)。简化的示例是:
auto ConfirmOperation = [](auto pr){
assert(pr.second);
};
这个想法是,如果您将其传递给 std :: pair
,其中 second
是 bool
(例如从返回的内容) emplace
函数),它可以查看此布尔值。
如果这是模板参数,则可以显式显示 pair
的配对类型是通用的,但是我认为lambda不可能吗?因此,相反,我将整个参数标记为泛型,因此编译器似乎无法推断出我正在将映射的 emplace()
的返回值传递给它。 / p>
有任何方法吗?
您可以约束lambda使用 enable_if
:
auto ConfirmOperation = [](auto pr)- >
std :: enable_if_t< std :: is_same< decltype(pr.second),bool> :: value> {
assert(pr.second);
};
。
I'm trying to see if this is possible in the C++14 generic lambda, but I cannot find a right way to express it (or perhaps it is not possible). The simplified example is:
auto confirmOperation = [](auto pr){
assert(pr.second);
};
The idea is that if you pass it an std::pair
where the second
is a bool
(such as what is returned from emplace
functions), it can look at this bool.
If this was a template parameter instead, I could explicitly show the pair
with the types of the pair as generic, but I don't think that is possible with a lambda? Thus instead I mark the entire argument as generic, and thus the compiler doesn't seem able to deduce that I'm passing it the return of a map's emplace()
.
Any way to do this?
You can constrain a lambda using enable_if
:
auto confirmOperation = [](auto pr) ->
std::enable_if_t<std::is_same<decltype(pr.second), bool>::value> {
assert(pr.second);
};
这篇关于std :: pair的通用Lambda参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!