繁体   English   中英

需要从枚举(类)到 std::binary_function 的 map

[英]Need a map from enum (class) to std::binary_function

我有这个枚举(类)

enum class conditional_operator
{
    plus_op,
    or_op,
    not_op
}

我想要一个代表这些映射的std::map

std::map<conditional_operator, std::binary_function<bool,bool,bool>> conditional_map = 
        { { conditional_operator::plus_op, std::logical_and<bool> },
          { conditional_operator::or_op,   std::logical_or<bool>},
          { conditional_operator::not_op,  std::binary_negate<bool>} // this seems fishy too, binary_negate is not really what I want :(

除了这不能编译的事实之外:

错误:“}”标记之前的预期主表达式

错误:“}”标记之前的预期主表达式

错误:“}”标记之前的预期主表达式

对于三行中的每一行,我应该怎么做? 我认为带有第二个虚拟参数的logical_not会起作用,当然,一旦我得到它来编译......

编辑:我可以为此使用 lambda 吗?

你真的想要std::function<bool(bool, bool)> ,而不是std::binary_function<bool, bool, bool> 这只存在于 C++03 中的 typedef 和东西。 其次,我只使用 lambda - 它们足够短且更清晰。 std::logical_and和东西只存在于 C++03 function object 创建,我会在任何一天使用 lambda 。

std::map<conditional_operator, std::function<bool(bool,bool)>> conditional_map = 
{ 
    { conditional_operator::plus_op, [](bool a, bool b) { return a && b; } },
    { conditional_operator::or_op,   [](bool a, bool b) { return a || b; } },
    { conditional_operator::not_op,  [](bool a, bool b) { return !a; } }
};

等等 - 你指的是哪个确切的运营商? 因为这是一元的,据我所知。

@DeadMG 的答案很明确,但如果您坚持使用预定义的 function 对象,则需要实例化它们。 目前你只是传递他们的类型名称。

也就是说,您需要编写std::logical_***<bool>()而不仅仅是std::logical_***<bool>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM