// file: date.h
#include <cstdio>
class date
{
friend bool operator< ( date d1, date d2) ;
public :
// constructor
date( int y= 2015 , int m= 1 , int d= 1 ) : year( y) , month( m) , day( d) { }
// explicit constructor
explicit date( const char * s) ;
private :
int year;
int month;
int day;
} ;
// question: use member or global operators?
bool operator< ( date d1, date d2) ;
inline bool operator== ( date d1, date d2) { return ! ( d1< d2 || d2< d1) ; }
inline bool operator! = ( date d1, date d2) { return d1< d2 || d2< d1; }
inline bool operator<= ( date d1, date d2) { return ! ( d2< d1) ; }
inline bool operator>= ( date d1, date d2) { return ! ( d1< d2) ; }
inline bool operator> ( date d1, date d2) { return d2< d1; }
// file: date.cpp
date:: date ( const char * s)
{
sscanf ( s, "%d.%d.%d" , & year, & month, & day) ;
}
bool operator< ( date d1, date d2)
{
return d1.year < d2.year || d1.month < d2.month || d1.day < d2.day ;
}
// file: main.cpp
int main( )
{
date d( 2015 , 3 , 12 ) ;
if ( d < date( 2000 , 1 , 1 ) ) // works: explicit call of constructor
{
/* ... */
}
else if ( d < 2000 ) // works: implicit constructor
{
/* ... */
}
else if ( d < "2015.1.12" ) // does not work: explicit constructor
{
/* ... */
}
else if ( d < date( "2015.1.12" ) ) // works: explicit call of constructor
{
/* ... */
}
}
Ly8gZmlsZTogZGF0ZS5oCiNpbmNsdWRlIDxjc3RkaW8+CgpjbGFzcyBkYXRlCnsKICBmcmllbmQgYm9vbCBvcGVyYXRvcjwoZGF0ZSBkMSwgZGF0ZSBkMik7CnB1YmxpYzoKICAvLyBjb25zdHJ1Y3RvciAgICAKICBkYXRlKGludCB5PTIwMTUsIGludCBtPTEsIGludCBkPTEpIDogeWVhcih5KSwgbW9udGgobSksIGRheShkKSB7IH0KICAvLyBleHBsaWNpdCBjb25zdHJ1Y3RvcgogIGV4cGxpY2l0IGRhdGUoY29uc3QgY2hhciAqcyk7CnByaXZhdGU6CiAgaW50IHllYXI7CiAgaW50IG1vbnRoOwogIGludCBkYXk7Cn07CgovLyBxdWVzdGlvbjogdXNlIG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IHsKICAgIC8qIC4uLiAqLwogIH0KICBlbHNlIGlmIChkIDwgIjIwMTUuMS4xMiIpICAgICAgICAgLy8gZG9lcyBub3Qgd29yazogZXhwbGljaXQgY29uc3RydWN0b3IKICB7CiAgICAvKiAuLi4gKi8KICB9CiAgZWxzZSBpZiAoZCA8IGRhdGUoIjIwMTUuMS4xMiIpKSAgIC8vIHdvcmtzOiBleHBsaWNpdCBjYWxsIG9mIGNvbnN0cnVjdG9yIAogIHsKICAgIC8qIC4uLiAqLwogIH0KfQ==
compilation info
prog.cpp: In function 'int main()':
prog.cpp:52:16: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
else if (d < "2015.1.12") // does not work: explicit constructor
^
prog.cpp:9:3: note: initializing argument 1 of 'date::date(int, int, int)'
date(int y=2015, int m=1, int d=1) : year(y), month(m), day(d) { }
^
stdout