I
Ioannis Vranos
Noah said:Ioannis said:[Preliminary code]
>>
EWWW!!!! Not only is this a really long list of operators, you can't
extend it to new types (such as complex_number).
This was preliminary code. You may find the latest implementation at
http://www.cpp-software.net/documents/cpp_only_proposal.html
However since the C++ standard committee will not be accepting new proposals for one year or more, this
project has low priority in my free time.
Two ways you could solve this problem:
template < typename T >
struct only {
...
template < typename Other >
explicit only( only<Other> const& other ) : value(other.value)
{
BOOST_STATIC_ASSERT((is_convertable<Other, T>::value));
}
};
only<int> x = only<int>(result);
Option two:
template < typename T >
struct only
{
...
template < typename Return >
Return cast() const
{
BOOST_STATIC_ASSERT((is_convertable< Return, T >::value));
return value;
}
};
template < typename T, typename R >
R only_cast(only<T> const& o)
{
return static_cast<T>(o); // use cast operator you've made for T.
}
only<int> x = only_cast<int>(result);
There's other ways I'm sure. You just need to analyze the problem.
Yes. Thank you for your suggestions, however I have to note that the implementation of this class and its
facilities may only depend on C++ standard library, and not on third-party libraries like boost.
C++0x might let you do this:
http://en.wikipedia.org/wiki/C++0x#User-defined_literals
When C++ gets finalised and we get some decent implementations supporting it (this translates to years), I
will take a look at the new C++ facilities.
Thank you for your comments.
Best regards,
--
Ioannis A. Vranos
C95 / C++03 Developer
http://www.cpp-software.net