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