- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了这样一些 C 代码的问题:
struct SomeType { ...details immaterial... };
static struct SomeType array[] =
{
{ ... },
...
{ ... },
};
enum { ARRAY_SIZE = sizeof(array) / sizeof(array[0]) };
Unix 编译器(各种版本的 GCC,以及 AIX 和 HP-UX 上的编译器)都对 enum
非常满意。 MSVC 2005 反对,错误为 C2056:非法表达式
。根据MSDN ,这是因为“由于先前的错误,表达式无效”。这是报告的唯一错误,这让人有点惊讶。
但是,我的问题是:
enum
方面准确地解释了 C89 标准?enum
?FWIW:可接受的解决方案是将 enum
更改为:
static int const ARRAY_SIZE = sizeof(array) / sizeof(array[0]);
Michael Burr提供了一些非常有值(value)的额外信息,并帮助我解决了问题。
可编译示例(与实际问题同构):
static const char *names[] = { "abc", "def", "ghi" };
enum { NAMES_SIZE = sizeof(names) / sizeof(names[0]) };
static const struct stuff { const char *name; int flags; } array[] =
{
{ "abc", 1 },
{ "def", 2 },
{ "ghi", 3 },
};
enum { ARRAY_SIZE = sizeof(array) / sizeof(array[0]) };
MSVC 的精确版本(由 cl
给出)是:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
这就是 Michael 所说的版本。
而且,我找到了我的问题的原因。 (下面的命令行删除了所有项目特定的废话——示例代码不需要的大量/D 和/I 选项。)编译:
cl /W3 /c /LD /MD /Od aaa.c
上面的代码编译正常。
cl /Zg /W3 /c /LD /MD /Od aaa.c
这首先会生成警告:
cl : Command line warning D9035 : option 'Zg' has been deprecated and will be removed in a future release
一年或更长时间以来,我一直在提示使用已弃用的选项进行编译,但负责该子项目的团队中没有人愿意站出来修复它,我也没有应该——即将改变的事情。
然后它说:
aaa.c(2) : error C2056: illegal expression
aaa.c(10) : error C2056: illegal expression
所以,不仅是 /Zg
选项已弃用,但它也是造成问题的首要原因。现在我有了一些更好的弹药来追杀人!
感谢您提供额外的信息,迈克尔。
PS:关于 /Zg
的 MSDN 页面说:
If you use the
/Zg
option and your program contains formal parameters that havestruct
,enum
, orunion
type (or pointers to such types), the declaration of eachstruct
,enum
, orunion
type must have a tag (name).
这并不完全准确。示例片段中没有正式参数,enum
值从未传递给函数。但是没有enum
上的标签,错误仍然出现在/Zg
标志上。
(不;我不是特别喜欢 MSVC 2005。我工作的小组还没有升级到任何更新的东西。大多数情况下它不会影响我。有些时候,像这样,当它伤害很多。总有一天,我会搞清楚为什么 Windows 上的其他构建似乎正在接受 enum
;我担心我会发现这个特定的子产品与其他所有产品都不一致并使用比其他产品更旧的编译器。)
这是严格的 C 代码。 MSVC 标签是 Visual-C++ 标签的同义词。
最佳答案
这在 C89 中当然是合法的(只要生成的数组大小可表示为 int
)。
§3.5.2.2 中对枚举常量定义值的约束是:
The expression that defines the value of an enumeration constant shall be an integral constant expression that has a value representable as an int.
§3.4 描述了整数常量表达式:
Constraints
Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within the operand of a sizeof operator.
Each constant expression shall evaluate to a constant that is in the range of representable values for its type.
Semantics
An expression that evaluates to a constant is required in several contexts. If the expression is evaluated in the translation environment, the arithmetic precision and range shall be at least as great as if the expression were being evaluated in the execution environment.
An integral constant expression shall have integral type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integral constant expression shall only convert arithmetic types to integral types, except as part of an operand to the sizeof operator.
关于c - 是 Unix 编译器太松懈了,还是 MSVC 2005 缺少一个技巧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9883939/
我是新手。查看 Google 新闻...上下滚动页面时请注意左侧导航栏。 看看它是如何滚动一点,然后在它消失之前粘在页面顶部的? 关于如何做到这一点有什么想法吗? jQuery 和 CSS 可以复制吗
技巧 1:在 Web 服务器上缓存常用数据 技巧 2:在 Application 或 Session 对象中缓存常用数据 技巧 3:在 Web 服务器磁盘上缓存数据和 HTML 技巧 4:避免
我在 excel 中有一个电子表格,其中包含以下行: COLUMN Value1.Value2.Value3 Value4.Value5.Value6 Value7.Value8.Val
GNU Makefile 中是否有任何技巧来获取规则的所有依赖项? 例子: rule1: dep1_1 dep1_2 dep1_3 rule2: dep2_1 dep2_2 rule1 dump_
人们使用什么来追踪内存泄漏?我已经通过代码检查设法解决了一些问题,但我不知道下一步该做什么/当我的程序变大时我将如何管理问题。我知道我在泄漏什么类型的对象,但我不知道是什么让它保持活力。 在 Wind
有什么好的方法可以将“xlSum”、“xlAverage”和“xlCount”等字符串转换为它们在 Microsoft.Office.Interop.Excel.XlConsolidationFunc
我们都见过这个: javascript:document.body.contentEditable='true'; document.designMode='on';无效 0 但我的问题是,这实际上是
我的应用程序将输出一个图形,其布局由用户定义。自定义布局类应该实现我定义的接口(interface)。我应该怎么做?有一个特殊的文件夹,我可以在其中查找布局类?用户是否将类名作为参数传递给应用? 如有
我在弄清楚如何在 Javascript 中自引用表行时遇到了一些麻烦。 这是简化的代码: $( "#listitems tbody" ).append( "" + "" + id.va
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我正在将代码库从一种编程风格转移到另一种编程风格。 我们有一个名为 Operand 的类型,定义如下: class Operand {...}; 然后我们有 class OperandFactory
我使用以下缩略图类在我的内容包装器中显示 4x3 缩略图: .thumbnail { float:left; width:300px; height:200px; ma
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我认为这是不可能的,但我想在放弃之前问问你。 我想要类似 constexpr 增量的东西。 #include constexpr int inc() { static int inc = 0;
是否有任何适合 C++ 新手的技术和描述的好列表。我在想一个描述 RAII、RVO、左值的列表……这适用于目前不了解这些技术或来自不适用这些技术的其他语言的新手。 最好是短小精悍的:-) 最佳答案 是
我有一个二进制字符串 '01110000',我想在不编写 forloop 的情况下返回前面的前导零数。有谁知道如何做到这一点?如果字符串立即以“1”开头,最好也返回 0 最佳答案 如果您真的确定它是一
我需要优化我的应用程序的 RAM 使用率。 请省去那些告诉我在编写 Python 代码时不应该关心内存的讲座。我有内存问题,因为我使用非常大的默认字典(是的,我也想快点)。我目前的内存消耗是 350M
有时,当我看到一个我喜欢的网站或来自受人尊敬的人的网站时,我会查看源代码并尝试理解它们(就像我们所有人一样)。 关于 Jeremy Keiths他使用以下代码的网站: [role="navigatio
这是我怎样设置 Git 来管理我的家目录的方法。 我有好几台电脑。一台笔记本电脑用于工作,一台工作站放在家里,一台树莓派(或四台),一台 Pocket CHIP,一台 运行
shell 技巧 表变量 HBase 0.95 版本增加了为表提供 jruby 风格的面向对象引用的 shell 命令。以前,作用于表的所有 shell 命令都具有程序风格,该风格始终将表的名称作
我是一名优秀的程序员,十分优秀!