当前位置:网站首页>d,cast转换aa为右值
d,cast转换aa为右值
2022-08-11 11:27:00 【fqbqrr】
原文
测试用例:
shared int[int] aa;
void main () {
cast()aa[1] = 1;//违反区间
}
原来工作正常.
你正在aa[1]上抛弃共享.该表达式是访问值,而不是赋值.与定义opIndex和opIndexAssign的结构(表达式调用opIndex,而不是opIndexAssign)是一致的.
如果使用cast()(aa[1])=1,即使在旧版本上也会出现区间错误.为何曾经奏效?
我认为旧编译器解析其为也适合新编译器且无区间错误的(cast()aa)[1].
本例,aa也是immutable.我知道使它工作的唯一方法现在非常丑陋(丢弃不变也许就该丑?):
shared immutable int[int] aa;
void main () {
// (cast()aa)[1] = 1; // 无不变仍工作,
(*cast(int[int]*)(&aa))[1] = 1;//这样?
}
有静态共享构造器:
shared static this()
{
aa[1] = 1; /*不必强制转换*/
}
但这样不行的:
void f() {
aa[1] = 1; // 错误
}
shared static this()
{
f();//f仍然是模板,模板插件,仍不管用
//在f嵌套函数中不能初化aa不变量
}
如果可在pure函数中构建数组的可变版本,则可这样做:
int[int] make_aa() pure
{
int[int] maa;
maa[1] = 1; /*或函数调用等等*/
return maa;
}
immutable int[int] aa;
shared static this()
{
aa = make_aa();
}
如果不能在pure函数中这样,则可用cast来这样:aa=cast(immutable)make_aa();.
从可变转换为不变更好,因为无未定义行为(只要以后不使用可变引用).丢弃不变,然后修改,确实有未定义行为.
边栏推荐
- 通过热透镜聚焦不同类型的高斯模式
- Incredible, thanks to this Android interview question, I have won offers from many Internet companies
- centos linux 下安装mysql 8.0
- 简单记录openguass_exporter对接prometheus通过grafanai来实现可视化监控
- Through the thermal lens focus on different types of gaussian model
- CCF大会腾源会专场即将召开,聚焦基础软件与开发语言未来发展
- PL4807-ADJ线性锂电池可调充电芯片
- C语言手写魂斗罗(一)
- 从零开始配置 vim(11)——插件管理
- PerfView专题 (第一篇):如何寻找热点函数
猜你喜欢

兴盛优选:时序数据如何高效处理?

使用神经网络进行医学影像识别分析

如何在游戏中实现一场下雨效果

SM5200原厂SOT23-6 500mA 线性锂电子替代芯片

「开源推荐」一个通用的后台管理系统

Through the thermal lens focus on different types of gaussian model

PerfView project (first) : how to find hot spots function

放苹果

【黑马早报】抖音否认与头部主播签对赌协议;阿迪达斯CEO承认在中国犯了错;网易云社交App心遇被指涉黄;联通董事长称5G资费比点外卖还便宜

天花板级微服务大佬总结出这份451页笔记告诉你微服务就该这么学
随机推荐
CCF大会腾源会专场即将召开,聚焦基础软件与开发语言未来发展
十九、一起学习Lua 垃圾回收
TiSpark 原理之下推丨TiDB 工具分享
HyperLynx(五)反射仿真
嵌入式开发:提示和技巧——退出时休眠
B端产品需求分析与优先级判断
挑战52天背完小猪佩奇(第02天)
七、一起学习Lua 函数
Common operations in Typora tables
TX12 + ExpressLRS 915MHz RC控制链路配置及问题汇总
I got the P8 "top" distributed architecture manual that went viral on Ali's intranet
小目标绝技 | 用最简单的方式完成Yolov5的小目标检测升级!
Flutter 教程之 在 Flutter 中生成 JSON 模型,在 Flutter GetX 中过滤列表和延迟搜索
老生常谈:面试必问“三次握手,四次挥手”这么讲,保证你忘不了
The old saying: The interview must ask "Three handshakes, four waves", so you can't forget it
Network Security - nmap
资本市场做好为工业互联网“买单”的准备了吗?
FS2956A 输入8-120V 用于液晶仪表5V-USB 充电口方案
Incredible, thanks to this Android interview question, I have won offers from many Internet companies
chrome is set to dark mode (including the entire webpage)