fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct foo{
  6. string bar;
  7. // foo() = delete; // Nothing changes if this is uncommented
  8. foo(const string& bar) : bar(bar) { }
  9.  
  10. void operator()(){
  11. cout << "my bar is \"" << bar << '"' << endl;
  12. }
  13. };
  14.  
  15. void hmm1(string str){
  16. foo(str)(); // Interpreted as 'foo str();', so nothing more
  17. // than a declaration of 'str' as type foo
  18. }
  19. // warning: unused parameter ‘str’ [-Wunused-parameter]
  20. // void hmm1(string str){
  21. void hmm2(string str){
  22. foo(str).operator()(); // Both of these construct a foo object
  23. (foo(str))(); // and call its () operator
  24. }
  25. void hmm3(string str){
  26. foo{str}(); // Same as in hmm2
  27. }
  28. int main(){
  29. hmm1("this is hmm1");
  30. foo("similar format to hmm1")();
  31.  
  32. hmm2("these are hmm2");
  33.  
  34. hmm3("this is hmm3");
  35. foo{"similar format to hmm3"}();
  36.  
  37. return 0;
  38. }
  39.  
  40.  
  41. /*
  42. void compilation_error(){
  43.   string x("lol");
  44.   foo(x)();
  45. }
  46. // error: ‘foo x()’ redeclared as different kind of symbol
  47. // foo(x)();
  48. // ^
  49. // note: previous declaration ‘std::__cxx11::string x’
  50. // string x("lol");
  51. */
  52.  
  53. void but_this_is_okay(){
  54. string x("lol");
  55. foo{x}();
  56. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
my bar is "similar format to hmm1"
my bar is "these are hmm2"
my bar is "these are hmm2"
my bar is "this is hmm3"
my bar is "similar format to hmm3"