2

I have studied in C++ Primer that the dimension of an array should be a const expression but the following code compiles and gives the desired result. Why?

#include<iostream>
using namespace std;
int main()
{
unsigned int bufsize = 10;
int h[bufsize]={};
for(int i=0; i<=9; i++)
    cout<<h[i]<<endl;
}

But if I add the following:

constexpr int a = bufsize;

Then the compiler shows error meaning that bufsize is not a constant expression then why is the program compiling correctly otherwise if the dimension of the array should be constant expression

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
RSSB
  • 144
  • 2
  • 4
  • 14
  • 5
    Most compilers accept some non-conforming code by default (that's called *extensions*). If you use GCC or Clang, add `-std=c++?? -pedantic-errors` flags to disable that behaviour. (Replace `??` with language version: `17`, `14`, `11`, ...) – HolyBlackCat Sep 19 '18 at 15:25
  • I am following the gnu gcc compiler in codeblocks – RSSB Sep 19 '18 at 15:26
  • See the edited comment then. – HolyBlackCat Sep 19 '18 at 15:26
  • My wish is that gcc or whatever compiler you're using would turn this option **off** by default. Then less questions like this (and less posts with this code code) would occur. – PaulMcKenzie Sep 19 '18 at 15:27
  • Also, `constexpr int a = bufsize;` doesn't work because you can't initialize a `constexpr` variable with non-`constexpr` expression. Try `constexpr int a = 10;` instead. – HolyBlackCat Sep 19 '18 at 15:28
  • @PaulMcKenzie An understandable wish, but then gcc would fail to compile _lots_ of existing software packages that use those extensions (without build modifications to re-enable the extensions). Balancing compatibility with strict standard compliance is a delicate art... – TypeIA Sep 19 '18 at 15:29
  • @TypeIA how about a compromise and issue a warning by default (which is after all mandatory for standard compliance)? – eerorika Sep 19 '18 at 15:31
  • Yes, but I put most of the blame on `gcc` for this. We now have coding websites where they show samples of supposed C++ code that uses these extensions. Then either the newbie C++ programmer believes the code they're writing is valid, or worse, the newbie C++ programmer takes their code to Visual Studio, and are stumped why their code doesn't compile. Then we get the plethora of questions asked here on why the code doesn't compile on Visual Studio. The gcc engineers should have seen this, and just put a cutoff date on the extension being "on" by default. – PaulMcKenzie Sep 19 '18 at 15:33
  • @user2079303 You would not expect a warning when using a documented extension. As long as the default mode is GnuC++, we are stuck. – Baum mit Augen Sep 19 '18 at 15:33
  • @BaummitAugen my non-explicit suggestion is to use a standard compliant mode by default (allow extensions but warn), and require an option to explicitly enable GNU extensions. – eerorika Sep 19 '18 at 15:34
  • @user2079303 Ah ok. I misunderstood. – Baum mit Augen Sep 19 '18 at 15:37

0 Answers0