简体   繁体   English

VS2010中的const问题

[英]const problem in VS2010

I have the shortest question possible: 我有最短的问题:

Why does this not work in VS2010? 为什么这在VS2010中不起作用?

string keyword("lookuptable");
const int kwSize = keyword.size();
char oldBuffer[kwSize+1];

It works perfectly in GCC. 它在GCC中完美运行。 VS2010 tells me that VS2010告诉我

"expression must have constant value" “表达必须具有恒定的价值”

I am using Win32 console application / empty project. 我正在使用Win32控制台应用程序/空项目。

I am using absolutely nothing special, just 我正在使用绝对没什么特别的,只是

#include <iostream> 
#include <fstream> 
#include <stdio.h> 
#include <string.h> 
using namespace std

and its a single main function in a cpp file. 它是cpp文件中的一个主要功能。

The size of an array must be an integral constant expression, or ICE (which means that it must be known at compile-time). 数组的大小必须是一个整数常量表达式,或ICE(这意味着它必须在编译时知道)。 You can use a const int in an ICE, but only if its initializer is itself an ICE. 您可以在ICE中使用const int ,但前提是它的初始化程序本身就是ICE。

A function call, like keyword.size() is not usable in an ICE, so kwSize is not usable in an ICE. 函数调用(如keyword.size()在ICE中不可用,因此kwSize在ICE中不可用。

If it "works perfectly" in gcc it is either due to a bug or a language extension of some sort. 如果它在gcc中“完美地运行”,则可能是由于某种错误或语言扩展。

In C++0x, some function calls can be used in integral constant expressions, but they must be constexpr functions and there are restrictions on their use. 在C ++ 0x中,某些函数调用可用于整型常量表达式,但它们必须是constexpr函数,并且对它们的使用有限制。 To the best of my knowledge, no compiler fully supports constexpr yet. 据我所知,还没有编译器完全支持constexpr In any case, std::string::size is not constexpr . 无论如何, std::string::size不是constexpr

GCC has a language extension allowing variable-length arrays. GCC具有允许可变长度数组的语言扩展。 Visual C++ does not. Visual C ++没有。 You must initialize stack-based arrays with a fixed, compile-time constant. 必须使用固定的编译时常量初始化基于堆栈的数组。

As others have mentioned, non-constant array bounds are a GCC extension (likely a side benefit of its C99 support - C99 does allow non-constant array bounds). 正如其他人所提到的,非常数数组边界是GCC扩展(可能是其C99支持的附带好处--C99允许非常数数组边界)。 If you want this in C++, you should use a vector : 如果你想在C ++中使用它,你应该使用一个vector

std::vector oldBuffer(kwSize + 1);

To turn this into a char * , do: 要将其转换为char * ,请执行以下操作:

&oldBuffer[0]

This, while not strictly speaking valid C++ prior to C++0x, works properly in every compiler I've come across so far. 虽然严格来说这不是C ++ 0x之前的有效C ++,但是到目前为止我遇到的每个编译器都能正常工作。 C++0x retroactively blesses this usage, and also provides a oldBuffer.data() equivalent. C ++ 0x追溯性地oldBuffer.data()这种用法,并且还提供了oldBuffer.data()等价物。

keyword.size() is evaluated at runtime therefore it is not a compile time constant. keyword.size()在运行时计算,因此它不是编译时常量。 Depending on the compiler it may not be allowed to use an array size that is depending on some runtime value. 根据编译器的不同,可能不允许使用依赖于某些运行时值的数组大小。

Some further info about variable array length in gcc 关于gcc中变量数组长度的一些进一步信息

the answer is there "expression must have constant value". 答案是“表达必须具有恒定的价值”。 The const has to be something resolved at the compile time. const必须在编译时解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM