fork download
  1. template <std::string * temp> //pointer to object
  2. void f()
  3. {
  4. cout << *temp << endl;
  5. }
  6.  
  7. template <std::string & temp> //reference to object
  8. void g()
  9. {
  10. cout << temp << endl;
  11. temp += "...appended some string";
  12. }
  13.  
  14. std::string s; //must not be local as it must have external linkage! WHY???
  15.  
  16. int main() {
  17. s = "can assign values locally";
  18. f<&s>();
  19. g<s>();
  20. cout << s << endl;
  21. return 0;
  22. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1: error: ‘std::string’ has not been declared
prog.cpp: In function ‘void f()’:
prog.cpp:4: error: ‘cout’ was not declared in this scope
prog.cpp:4: error: ‘endl’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:7: error: ‘std::string’ has not been declared
prog.cpp: In function ‘void g()’:
prog.cpp:10: error: ‘cout’ was not declared in this scope
prog.cpp:10: error: ‘endl’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:14: error: ‘string’ in namespace ‘std’ does not name a type
prog.cpp: In function ‘int main()’:
prog.cpp:17: error: ‘s’ was not declared in this scope
prog.cpp:18: error: no matching function for call to ‘f()’
prog.cpp:19: error: ‘s’ cannot appear in a constant-expression
prog.cpp:19: error: no matching function for call to ‘g()’
prog.cpp:20: error: ‘cout’ was not declared in this scope
prog.cpp:20: error: ‘endl’ was not declared in this scope
stdout
Standard output is empty