需要从 enum (类)到 std::binary_function 的映射

发布于 2024-11-14 05:53:24 字数 795 浏览 2 评论 0原文

我有这个枚举(类)

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 :(

除了它无法编译的事实之外:

错误:“}”标记之前应有主表达式

错误:“}”标记之前应有主表达式

错误:“}”标记之前应有主表达式

错误:对于三行中的每一行, ,我应该如何执行此操作?我认为带有第二个虚拟参数的逻辑不可以工作,当然,一旦我编译它......

编辑:我可以使用 lambda 来实现这个吗?

I have this enum (class)

enum class conditional_operator
{
    plus_op,
    or_op,
    not_op
}

And I'd like a std::map that represents these mappings:

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 :(

Apart from the fact that this doesn't compile:

error: expected primary-expression before '}' token

error: expected primary-expression before '}' token

error: expected primary-expression before '}' token

for each of the three lines, how should I do this? I think a logical_not with a second dummy argument would work, once I get this to compile of course...

EDIT: Could I use lambda's for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-11-21 05:53:24

您确实需要 std::function,而不是 std::binary_function。这只存在于 typedef 和 C++03 中的东西。其次,我只使用 lambda - 它们足够短而且更清晰。 std::ological_and 之类的东西仅存在于 C++03 函数对象创建中,我随时都会在它们之上使用 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; } }
};

等等 - 你指的 not 到底是什么运算符?因为据我所知,这是一元的。

You really want std::function<bool(bool, bool)>, not std::binary_function<bool, bool, bool>. That only exists for typedefs and stuff in C++03. Secondly, I'd just use a lambda- they're short enough and much clearer. The std::logical_and and stuff only exists for C++03 function object creation, and I'd use a lambda over them any day.

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; } }
};

Wait- what exact operator are you referring to with not? Because that's unary, as far as I know.

找回味觉 2024-11-21 05:53:24

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

也就是说,您需要编写 std::logic_***() 而不仅仅是 std::logic_***

@DeadMG’s answer is spot-on but if you insist on using the predefined function objects, you need to instantiate them. At the moment you’re just passing their type names.

That is, you need to write std::logical_***<bool>() instead of just std::logical_***<bool>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文