C ++ Switch不会使用大小写的外部定义变量进行编译 [英] C++ Switch won't compile with externally defined variable used as case

查看:110
本文介绍了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时,它是一个声明为externconst变量,没有任何初始化程序,并且不满足常量表达式的要求,尽管事实是您实际上是在另一个转换单元中使用整数常量表达式对其进行了初始化. (*从标准的措词中还不能完全清楚,但这是我目前对它的解释.)

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不会使用大小写的外部定义变量进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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