为什么不能将结构声明为 const? [英] Why can't structs be declared as const?

查看:25
本文介绍了为什么不能将结构声明为 const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们是堆栈上的不可变值类型.是什么让我无法让它们成为常量?

They are immutable value types on the stack. What keeps me from having them a const?

参考文献:

推荐答案

因为值类型构造函数可以做任何事情——例如,根据一天中的时间切换逻辑.常量值类型在智力上是有意义的,但由于构造函数可以灵活地做任何他们想做的事情,它在实践中根本无法处理自定义值类型.(请记住,常量是在编译时求值的,这意味着您的构造函数必须在编译时运行.)

Because the value type constructor might do anything -- for example, switch logic based on the time of day. Constant value types makes sense intellectually, but it simply cannot work on custom value types in practice due to the flexibility of constructors to do whatever they please. (Remember that constants are evaluated at compile time, which means your constructor would have to be run at compile time.)

这篇关于为什么不能将结构声明为 const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆