自定义“数字”类-是否可以代替“浮动”类或其他内置类型使用?

如何解决自定义“数字”类-是否可以代替“浮动”类或其他内置类型使用?

我正在编写一个带有各种运算符重载的类,该类应以某些“外部”代码替代float(或类似名称)。 特别是,我想支持3种示例用法(见下文),而且我找不到一种能够满足所有3种要求的类定义方法。

有可能吗?

此外:实际上,我正在尝试在Lua 5.3中实现自定义lua_Number类型,通常为floatdouble。我正在将Lua编译为C ++,并尝试使用定点表示。虽然确实可以更改“外部”代码(因为它只是嵌入式Lua),但我宁愿不要(:

实时代码链接,如果您想玩转:https://godbolt.org/z/37c6nj

这是我需要支持的3个用例:

示例1:的简单用法,其中MyNum是工会成员。 这意味着我不能使用奇异的复制构造函数,因为这样的“非平凡”版本会导致联合体格式错误(未经修改)。

SomeUnion x;
x.num = 123.0f;
SomeUnion y = x;

示例2::MyNum是联合的成员(如上所述),而该联合是结构的成员。我不确定这与第一个示例有何不同,但这是我当前代码版本的最终症结所在。这是链接的示例中当前未编译的位。

SomeStruct s1;
s1.u.num = x.num;
SomeStruct s2;
// With all the code as-is,this is the line that fails:
//   error: use of deleted function 'SomeStruct& SomeStruct::operator=(const SomeStruct&)'
//   note: 'SomeStruct& SomeStruct::operator=(const SomeStruct&)' is implicitly deleted because the default definition would be ill-formed
s2 = s1;

示例3:volatile有关的用法。这需要自定义operator=

volatile SomeUnion v;
// If we don't have a custom operator= with 'volatile' qualifier,we get:
// ERROR: passing 'volatile MyNum' as 'this' argument discards qualifiers [-fpermissive]
v.num = x.num;

如果我删除了volatile的{​​{1}}实现,那么Example1和Example2会编译,而Example3不会。

如果我包含operator=的{​​{1}}版本,则Example1和Example3会编译,而Example2不会。

有什么办法可以使它们全部正常工作吗?

或者,如果失败,对外部代码进行一系列“微创”更改以使其能够正常工作的想法是什么?

解决方法

也许最简单的方法是将互操作性所需的并集定义为:

union SomeUnion {
    uint32_t num;
    void * pFoo;

    void set(const MyNum& myNum) {
        num = *reinterpret_cast<const uint32_t*>(&myNum);
    }

    MyNum get() {
        return *reinterpret_cast<MyNum*>(&this->num);
    }
};

MyNum副本应该只是默认设置:

MyNum(const MyNum& other) = default;
MyNum& operator=(const MyNum& other) = default;

该联合非常适合诸如volatile之类的任何低级操作,因为它只涉及原始类型;另一方面,很容易使用getter和setter方法将num字段用作MyNum对象的存储。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-