避免歧义在过载解析中

编程入门 行业动态 更新时间:2024-10-24 02:31:12
本文介绍了避免歧义在过载解析中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是对此问题的跟进,因此,如果您需要查看Register类,请参考该问题.现在,根据提供的答案,我编写了一个函数来执行此操作.我有两个版本的函数,一个将结果存储回原始版本,另一个将返回副本.这是我的功能:

This is a follow up to this question so if you need to see the Register class please refer to that question. Now based on the supplied answer I have written a function to do just that. I have 2 versions of the function one that will store the results back into the original and one that will return a copy. Here are my functions:

template<std::uint64_t N> void reverseBitOrder( Register<N>& reg ) { auto str = reg.register_.to_string(); std::reverse(str.begin(), str.end()); auto x = vpc::Byte(str); reg.register_ = x; } // Extra unused parameter to avoid ambiguity template<std::uint64_t N> Register<N> reverseBitOrder(Register<N>& reg, bool _x_ /*not used*/ ) { auto str = reg.register_.to_string(); std::reverse(str.begin(), str.end()); auto x = vpc::Byte(str); Register<N> temp; temp.register_ = x; return temp; }

第一个保存值,第二个返回副本.我的问题是关于2 nd 函数的,我最终添加了第二个未使用的参数,以避免由于重载解析而产生歧义,因为不能仅对返回类型进行函数解析.因此,当我调用此函数时,必须将0,1,true或false传递给无效的函数.

The first one saves the value, the second returns a copy. My question is on the 2nd function I ended up adding a second parameter that is not used in order to avoid ambiguity due to overload resolution as functions can not be resolved on return types alone. So when I call this function I would have to pass either 0, 1, true or false to the function which has no effect.

总体而言,这本身并不是什么大问题,但是,对我而言,这似乎并不十分简洁.还有其他方法可以做到这一点吗?我宁愿不要使它成为班级的职能.我的Register类或结构是完整的,并且对寄存器进行的任何类型的操作都将由引用一个或多个寄存器对象的函数完成.

Overall this in itself is not a very big deal, however, it doesn't seem very clean and concise to me. Are there any other ways to achieve this? I prefer not to make this a function of the class. My Register class or struct is complete as is and any kind of operations done on a register will be done by functions that take a reference to one or more register objects.

推荐答案

您可以使用 std::optional 实现这一目标.

You can use std::optional to achieve this.

功能模板reverseBitOrder的return类型应为std::optional<vpc::Register<N>>.

功能模板应修改为:

template<std::uint64_t N> std::optional<vpc::Register<N>> reverseBitOrder(vpc::Register<N>& reg, bool _x_ = false) { auto str = reg.register_.to_string(); std::reverse(str.begin(), str.end()); vpc::Register<N> temp; if(_x_) //return copy { temp.register_ = vpc::Byte(str); //since register_ is a vpc::Byte. Generalize accordingly. return temp; } else //save in the same variable { reg.register_ = vpc::Byte(str); return {}; } }

Live Demo here .

但是您实际上并不需要使用std::optional,因为在功能模板中确实没有失败"的情况.

But you don't really need to use std::optional, since there is really no "failure" case in the function template.

更多推荐

避免歧义在过载解析中

本文发布于:2023-10-30 23:49:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:歧义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!