fork download
  1. #include <iostream>
  2.  
  3. int f(const int i)
  4. {
  5. return i;
  6. }
  7.  
  8. int main()
  9. {
  10. /* When constants allocate memory? */
  11. const int c1 = 1; // no memory needed
  12. const int c2 = 2; // need memory
  13. const int c3 = f(3); // need memory
  14. const int *p = &c2;
  15.  
  16. int t1[c1];
  17. int t2[c2];
  18. // prog.cpp:18:12: warning: ISO C++ forbids variable length array 't3'
  19. int t3[c3];
  20.  
  21. int i; std::cin >> i;
  22. switch(i)
  23. {
  24. case c1: std::cout << "c1"; break;
  25. case c2: std::cout << "c2"; break;
  26. // prog.cpp:25:10: error: 'c3' cannot appear in a constant-expression
  27. case c3: std::cout << "c3"; break;
  28. }
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:27:10: error: 'c3' cannot appear in a constant-expression
     case c3: std::cout << "c3"; break;
          ^
stdout
Standard output is empty