I have the shortest question possible:
Why does this not work in VS2010?
string keyword("lookuptable");
const int kwSize = keyword.size();
char oldBuffer[kwSize+1];
It works perfectly in GCC. VS2010 tells me that
“expression must have constant value”
I am using Win32 console application / empty project.
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.
The size of an array must be an integral constant expression, or ICE (which means that it must be known at compile-time). You can use a
const intin an ICE, but only if its initializer is itself an ICE.A function call, like
keyword.size()is not usable in an ICE, sokwSizeis not usable in an ICE.If it “works perfectly” in gcc it is either due to a bug or a language extension of some sort.
In C++0x, some function calls can be used in integral constant expressions, but they must be
constexprfunctions and there are restrictions on their use. To the best of my knowledge, no compiler fully supportsconstexpryet. In any case,std::string::sizeis notconstexpr.