需要从 enum (类)到 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 :(
除了它无法编译的事实之外:
错误:“}”标记之前应有主表达式
错误:“}”标记之前应有主表达式
错误:“}”标记之前应有主表达式
错误:对于三行中的每一行, ,我应该如何执行此操作?我认为带有第二个虚拟参数的逻辑不可以工作,当然,一旦我编译它......
编辑:我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实需要
std::function
,而不是std::binary_function
。这只存在于 typedef 和 C++03 中的东西。其次,我只使用 lambda - 它们足够短而且更清晰。std::ological_and
之类的东西仅存在于 C++03 函数对象创建中,我随时都会在它们之上使用 lambda。等等 - 你指的 not 到底是什么运算符?因为据我所知,这是一元的。
You really want
std::function<bool(bool, bool)>
, notstd::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. Thestd::logical_and
and stuff only exists for C++03 function object creation, and I'd use a lambda over them any day.Wait- what exact operator are you referring to with not? Because that's unary, as far as I know.
@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 juststd::logical_***<bool>
.