fork(1) download
  1. #include <iostream>
  2.  
  3. template <const char * MESSAGE> void print()
  4. {
  5. std::cout << MESSAGE << '\n';
  6. }
  7.  
  8. namespace
  9. {
  10. char namespace_message[] = "Anonymous Namespace Message";
  11. const char namespace_const_message[] = "Anonymous Namespace Const Message";
  12. constexpr char namespace_constexpr_message[] = "Anonymous Namespace Constexpr Message";
  13. }
  14.  
  15. char message[] = "Message";
  16. const char const_message[] = "Const Message";
  17. constexpr char constexpr_message[] = "Constexpr Message";
  18.  
  19. int main()
  20. {
  21. print<namespace_message>();
  22. print<namespace_constexpr_message>();
  23.  
  24. print<message>();
  25. print<constexpr_message>();
  26.  
  27. print<namespace_const_message>();
  28. print<const_message>();
  29. print<"Literal">();
  30.  
  31. return 0;
  32. }
Compilation error #stdin compilation error #stdout 0s 3140KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:27:8: error: the value of '{anonymous}::namespace_const_message' is not usable in a constant expression
  print<namespace_const_message>();
        ^
prog.cpp:11:12: note: '{anonymous}::namespace_const_message' was not declared 'constexpr'
 const char namespace_const_message[] = "Anonymous Namespace Const Message";
            ^
prog.cpp:28:8: error: the value of 'const_message' is not usable in a constant expression
  print<const_message>();
        ^
prog.cpp:16:12: note: 'const_message' was not declared 'constexpr'
 const char const_message[] = "Const Message";
            ^
prog.cpp:29:19: error: no matching function for call to 'print()'
  print<"Literal">();
                   ^
prog.cpp:29:19: note: candidate is:
prog.cpp:3:38: note: template<const char* MESSAGE> void print()
 template <const char * MESSAGE> void print()
                                      ^
prog.cpp:3:38: note:   template argument deduction/substitution failed:
prog.cpp:29:19: error: '"Literal"' is not a valid template argument for type 'const char*' because string literals can never be used in this context
  print<"Literal">();
                   ^
stdout
Standard output is empty