Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Per-type compile time constants?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Kai-Uwe Bux, post: 2573153"] [snip] Maybe someting like this (although it maybe overly complicated): #include <string> #include <iostream> struct a_string : private std::string { a_string ( void ) {} template < typename A > a_string ( A const & a ) : std::string( a ) {} template < typename A, typename B > a_string ( A const & a, B const & b ) : std::string( a, b ) {} a_string & operator+= ( a_string const & other ) { std::string::operator+=( other ); return ( *this ); } std::string const & str ( void ) const { return ( *this ); } }; struct b_string : private std::string { b_string ( void ) {} template < typename A > b_string ( A const & a ) : std::string( a ) {} template < typename A, typename B > b_string ( A const & a, B const & b ) : std::string( a, b ) {} b_string & operator+= ( b_string const & other ) { std::string::operator+=( other ); return ( *this ); } std::string const & str ( void ) const { return ( *this ); } }; struct new_line_const { new_line_const ( int ) {} operator a_string ( void ) const { return ( a_string( "hello" ) ); } operator b_string ( void ) const { return ( b_string( "world" ) ); } }; static new_line_const const new_line = 0; int main ( void ) { a_string a ( "greetings: " ); a += new_line; std::cout << a.str() << '\n'; b_string b ( "greetings: " ); b += new_line; std::cout << b.str() << '\n'; } Also note that the compiler will perform only one user defined conversion, so this trick might not work in all circumstances. Best Kai-Uwe Bux [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Per-type compile time constants?
Top