fork download
  1. // based on test-wrapper.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. template <typename T> class foo {
  7. public:
  8. T id;
  9. void setId(T iniName) {
  10. id = iniName;
  11. }
  12. };
  13.  
  14. template <typename T> struct foowrapper {
  15. foo<T> const & objFoowrapper;
  16. };
  17.  
  18. template <typename T> foowrapper<T> print(foo<T> const & objprint) {
  19. return {objprint}; // without curly brackets: error: conversion from ‘const foo’ to non-scalar type ‘foowrapper’ requested
  20. }
  21.  
  22. template <typename T> std::ostream & operator<<(std::ostream & lhs, foowrapper<T> const & rhs) {
  23. lhs << '[' << rhs.objFoowrapper.id << ']';
  24. }
  25.  
  26. int main() {
  27. foo<std::string> f;
  28. f.setId("blah");
  29. std::cout << print(f) << std::endl;
  30.  
  31. foo<int> g;
  32. g.setId(147);
  33. std::cout << print(g) << std::endl;
  34. g.id *= 2;
  35. std::cout << print(g) << std::endl;
  36. }
  37.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘foowrapper<T> print(const foo<T>&)’:
prog.cpp:19: error: expected primary-expression before ‘{’ token
prog.cpp:19: error: expected ‘;’ before ‘{’ token
prog.cpp:19: error: expected `;' before ‘}’ token
prog.cpp: In function ‘std::ostream& operator<<(std::ostream&, const foowrapper<T>&)’:
prog.cpp:24: warning: no return statement in function returning non-void
prog.cpp: In function ‘foowrapper<T> print(const foo<T>&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’:
prog.cpp:29:   instantiated from here
prog.cpp:19: warning: statement has no effect
prog.cpp: In function ‘foowrapper<T> print(const foo<T>&) [with T = int]’:
prog.cpp:33:   instantiated from here
prog.cpp:19: warning: statement has no effect
stdout
Standard output is empty