fork download
  1. #include<string>
  2. #include<iostream>
  3.  
  4. template <std::string * temp> //pointer to object
  5. void f()
  6. {
  7. std::cout << *temp << std::endl;
  8. }
  9.  
  10. template <std::string & temp> //reference to object
  11. void g()
  12. {
  13. std::cout << temp << std::endl;
  14. temp += "...appended some string";
  15. }
  16.  
  17. std::string s; //must not be local as it must have external linkage! WHY???
  18.  
  19. int main() {
  20. s = "can assign values locally";
  21. f<&s>();
  22. g<s>();
  23. std::cout << s << std::endl;
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
can assign values locally
can assign values locally
can assign values locally...appended some string