fork(4) download
  1. #include <boost/format.hpp>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. const std::string my_str = "echo '/%1%/ some other stuff'";
  8. boost::format fmtr(my_str);
  9. fmtr % "sleep 3"; // should read: echo '/sleep 3/ some other stuff'
  10.  
  11. std::cout << "1: " << fmtr.str() << "\n"; // 1. echo '/sleep 3/ some other stuff' (OK)
  12. std::cout << "2: " << fmtr.str().c_str() << "\n"; // 2. echo '/sleep 3 (BAD)
  13.  
  14. // Try the c_str of a string not created through boost::format
  15. const std::string finished = "echo '/sleep 3/ some other stuff'";
  16. std::cout << "3: " << finished.c_str() << "\n"; // 3. echo '/sleep 3/ some other stuff' (OK)
  17.  
  18. // Try copying the string from format to see if that makes any difference (it doesn't)
  19. std::string copy = fmtr.str();
  20. std::cout << "4: " << copy.c_str() << "\n"; // 4. echo '/sleep 3 (BAD)
  21. }
  22.  
Success #stdin #stdout 0s 3500KB
stdin
Standard input is empty
stdout
1: echo '/sleep 3/ some other stuff'
2: echo '/sleep 3/ some other stuff'
3: echo '/sleep 3/ some other stuff'
4: echo '/sleep 3/ some other stuff'