C ++ Switch不会使用大小写的外部定义变量进行编译

编程入门 行业动态 更新时间:2024-10-25 05:26:02
本文介绍了C ++ Switch不会使用大小写的外部定义变量进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用MinGW GNU编译器编写C ++,当我尝试在switch语句中尝试使用外部定义的整数变量作为案例时,会出现问题.我收到以下编译器错误:"大小写标签没有减少为整数常量".

I'm writing C++ using the MinGW GNU compiler and the problem occurs when I try to use an externally defined integer variable as a case in a switch statement. I get the following compiler error: "case label does not reduce to an integer constant".

因为我已经将整数变量定义为extern,我相信它应该可以编译,所以有人知道问题出在哪里吗?

Because I've defined the integer variable as extern I believe that it should compile, does anyone know what the problem may be?

下面是一个示例:

test.cpp

#include <iostream> #include "x_def.h" int main() { std::cout << "Main Entered" << std::endl; switch(0) { case test_int: std::cout << "Case X" << std::endl; break; default: std::cout << "Case Default" << std::endl; break; } return 0; }

x_def.h

extern const int test_int;

x_def.cpp

x_def.cpp

const int test_int = 0;

此代码将在Visual C ++ 2008上正确编译.此外,我的一位Montanan朋友检查了ISO C ++标准,并且似乎任何const-integer表达式都可以使用.这可能是编译器错误,还是我错过了明显的东西?

This code will compile correctly on Visual C++ 2008. Furthermore a Montanan friend of mine checked the ISO C++ standard and it appears that any const-integer expression should work. Is this possibly a compiler bug or have I missed something obvious?

这是我的编译器版本信息:

Here's my compiler version information:

从C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs中读取规范 配置为:../gcc-3.4.5-20060117-3/configure --with-gcc --with-gnu-ld --with-gnu-as --host = mingw32 --target = mingw32 --prefix = /mingw --enable-threads --disable-nls --enable-languages = c,c ++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions- enable-libgcj --disable-java-awt --without-x --enable-java-gc = boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug 线程模型:win32 gcc版本3.4.5(mingw-vista特殊R3)

Reading specs from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs Configured with: ../gcc-3.4.5-20060117-3/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug Thread model: win32 gcc version 3.4.5 (mingw-vista special r3)

推荐答案

case标签需要一个整数常量表达式,该表达式具有严格的要求,可以在编译时确定其值.使用点.

A case label requires an integral constant expression which have strict requirements that enable their value to be determined at compile time at the point of use.

从5.19 [expr.const]开始,"整数常量表达式只能包含文字(2.13),枚举器,const变量或整数或枚举类型初始化为常量的静态数据成员表达式(8.5),...".

From 5.19 [expr.const], "an integral constant expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5),...".

在使用需要常量表达式的test_int时,它是一个声明为extern的const变量,没有任何初始化程序,并且不满足常量表达式的要求,尽管事实是您实际上是在另一个转换单元中使用整数常量表达式对其进行了初始化. (*从标准的措词中还不能完全清楚,但这是我目前对它的解释.)

At the point at which you use test_int where a constant expression is required, it is a const variable declared extern and without any initializer and does not meet the requirements for a constant expression, despite the fact that you do actually initialize it with a integral constant expression in another translation unit. (*This is not completely clear from the wording of the standard but is my current interpretation of it.)

标准中的限制不允许使用,例如:

The restrictions in the standard disallow usages such as:

void f(int a, int b) { const int c = b; switch (a) { case c: //... } }

在您的示例中,编译器在编译test.cpp时,无法确定x_def.cpp中的初始化程序.您可能已经完成:

In your example, when the compiler is compiling test.cpp, it has no way to determine what the initializer might be in x_def.cpp. You might have done:

const int test_int = (int)time();

很显然,在这两个示例中,const int的值都不能在编译时确定,这是整数常量表达式的目的.

Clearly, in neither of these examples could the value of the const int be determined at compile time which is the intention for integral constant expressions.

更多推荐

C ++ Switch不会使用大小写的外部定义变量进行编译

本文发布于:2023-11-13 12:10:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小写   变量   定义   Switch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!