fork download
  1.  
  2. class foo
  3. {
  4. public:
  5. static const int constant; // constant ist konstant, enthält auch später einen constexpr Wert
  6. };
  7.  
  8. const int bar = foo::constant + 1; // hier weiß der Compiler das aber noch nicht...
  9.  
  10. const int foo::constant = 6; // ... da die Definition von constant erst hier erfolgt
  11.  
  12. template <int i>
  13. void func();
  14.  
  15. int main()
  16. {
  17. func<bar>(); // und bar deshalb hier kein konstanter Ausdruck ist
  18. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:17:7: error: the value of ‘bar’ is not usable in a constant expression
  func<bar>(); // und bar deshalb hier kein konstanter Ausdruck ist
       ^
prog.cpp:8:11: note: ‘bar’ was not initialized with a constant expression
 const int bar = foo::constant + 1; // hier weiß der Compiler das aber noch nicht...
           ^
prog.cpp:17:12: error: no matching function for call to ‘func()’
  func<bar>(); // und bar deshalb hier kein konstanter Ausdruck ist
            ^
prog.cpp:17:12: note: candidate is:
prog.cpp:13:6: note: template<int i> void func()
 void func();
      ^
prog.cpp:13:6: note:   template argument deduction/substitution failed:
prog.cpp:17:12: error: the value of ‘bar’ is not usable in a constant expression
  func<bar>(); // und bar deshalb hier kein konstanter Ausdruck ist
            ^
prog.cpp:8:11: note: ‘bar’ was not initialized with a constant expression
 const int bar = foo::constant + 1; // hier weiß der Compiler das aber noch nicht...
           ^
prog.cpp:17:12: note: in template argument for type ‘int’ 
  func<bar>(); // und bar deshalb hier kein konstanter Ausdruck ist
            ^
stdout
Standard output is empty