Corrected: Proposal: Increasing type safety with a keyword

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.



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top