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

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

问题描述

他们是在栈上不可变的值类型。是什么让我从他们有一个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.)

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

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