design_use_cases.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <string>
  2. #include <iostream>
  3. // Minimal class path
  4. class path
  5. {
  6. public:
  7. path( const char * )
  8. {
  9. std::cout << "path( const char * )\n";
  10. }
  11. path( const std::string & )
  12. {
  13. std::cout << "path( std::string & )\n";
  14. }
  15. // for maximum efficiency, either signature must work
  16. # ifdef BY_VALUE
  17. operator const std::string() const
  18. # else
  19. operator const std::string&() const
  20. # endif
  21. {
  22. std::cout << "operator string\n";
  23. return m_path;
  24. }
  25. #ifdef NAMED_CONVERSION
  26. std::string string() const
  27. {
  28. std::cout << "std::string string() const\n";
  29. return m_path;
  30. }
  31. #endif
  32. private:
  33. std::string m_path;
  34. };
  35. bool operator==( const path &, const path & )
  36. {
  37. std::cout << "operator==( const path &, const path & )\n";
  38. return true;
  39. }
  40. // These are the critical use cases. If any of these don't compile, usability
  41. // is unacceptably degraded.
  42. void f( const path & )
  43. {
  44. std::cout << "f( const path & )\n";
  45. }
  46. int main()
  47. {
  48. f( "foo" );
  49. f( std::string( "foo" ) );
  50. f( path( "foo" ) );
  51. std::cout << '\n';
  52. std::string s1( path( "foo" ) );
  53. std::string s2 = path( "foo" );
  54. s2 = path( "foo" );
  55. #ifdef NAMED_CONVERSION
  56. s2 = path( "foo" ).string();
  57. #endif
  58. std::cout << '\n';
  59. // these must call bool path( const path &, const path & );
  60. path( "foo" ) == path( "foo" );
  61. path( "foo" ) == "foo";
  62. path( "foo" ) == std::string( "foo" );
  63. "foo" == path( "foo" );
  64. std::string( "foo" ) == path( "foo" );
  65. return 0;
  66. }