Topic: Proposal: Increasing type safety with a keyword
Author: Ioannis Vranos <ivranos@freemail.gr>
Date: Wed, 1 Apr 2009 18:38:46 CST Raw View
Because of the unavoidable mistakes, typos and other, and also to keep
track how the proposal evolves, I have created a web page for it:
http://www.cpp-software.net/documents/cpp_only_proposal.html
So please base your answers to that document.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Ioannis Vranos <ivranos@freemail.gr>
Date: Thu, 2 Apr 2009 11:41:31 CST Raw View
I decided to make this proposal an open proposal. What does this mean?
It means that everyone who contributes to this proposal, and he wishes
to take credit for his contribution, his personal information will be
included in the final text of the proposal, if the proposal becomes
complete.
That's why, everyone who will contribute to this proposal, and wants
to take a credit for his contribution, can provide the following
personal information, along with his proposal contributions:
Name [Middle Name] Last name, email address, [web site], [postal address].
The information inside the brackets is optional.
The contributions must be mainly syntax-based, and must comply with
the design aims at the top of the page:
http://www.cpp-software.net/documents/cpp_only_proposal.html
You can post your contributions to the proposal, in this discussion
thread, or send me an email.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Ioannis Vranos <ivranos@freemail.gr>
Date: Fri, 3 Apr 2009 10:54:03 CST Raw View
Today I updated the text with significant changes. Rules have been
relaxed, and the implicit integer/floating
point promotions issue has been solved (I think).
The text, which is a work in progress, can be found here:
http://www.cpp-software.net/documents/cpp_only_proposal.html
(press Ctrl-Refresh in your browser, to make sure you see the most
recent version of the page).
Any comments, corrections and contributions are welcomed.
As mentioned in the page, anyone that contributes to the proposal and
wishes to take credit for his
contributions, if the proposal gets completed, he can follow the
directions mentioned at the end of the page.
Thanks.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Ioannis Vranos <ivranos@freemail.gr>
Date: Sat, 4 Apr 2009 09:48:47 CST Raw View
The proposal (currently a work in progress), becomes a compile-time
type-safety template class, taking the
type as an argument:
#include <iostream>
template <class ValueType>
class only;
// It makes the object work with cout, provided the type ValueType is
supported by cout.
template <class ValueType>
inline std::ostream & operator<<(std::ostream &objOstream, const
only<ValueType> &obj)
{
return objOstream<< obj.getValue();
}
// It converts between compatible only<T> types
template<class NewType, class OnlyType>
inline NewType type_cast(const OnlyType &obj)
{
return static_cast<NewType>(obj.getValue());
}
// Original class provided by Jeff Schwab <jeff@schwabcenter.com>
template <class ValueType>
class only
{
ValueType localValue;
template<class U> only(U); // No constr. from other types.
public:
typedef ValueType value_type;
typedef ValueType& reference;
only(value_type value): localValue( value ) { }
const value_type &getValue() const { return localValue; }
};
// An example of completely type-safe code:
#include <iostream>
only<double> somefunc()
{
// only<double> value= 1.1F; // It produces an error, great!
only<double> value= 1.1;
// Perform some operations
return value;
}
int main()
{
using namespace std;
only<char> c= 'a';
// It doesn't work without a cast, interesting! (because 4 is int)
only<signed char> sc= static_cast<signed char>(4);
// It doesn't work without a cast, interesting! (because 4 is int)
only<unsigned char> uc= static_cast<unsigned char>(4);
// It requires an explicit conversion between different types
only<signed char> schar= type_cast<signed char>(c);
// It doesn't work without a cast, interesting! (because 4 is int)
only<short> s= static_cast<short>(4);
// It doesn't work without a cast, interesting! (because 4 is int)
only<unsigned short> su= static_cast<unsigned short>(4);
// It doesn't work without a cast, interesting! (because 4 is int)
only<long> l= static_cast<long>(4);
// It doesn't work without a cast, interesting! (because 4 is int)
only<unsigned long> lu= static_cast<unsigned long>(4);
only<double> result= somefunc();
only<int> x= type_cast<int>(result);
cout<< x<< endl;
// double result2= somefunc(); // It produces an error,
great!
// only<int> x2= result; // It produces an error,
great!
}
New prefixes for signed char, unsigned char, short, unsigned short
constants:
1. ?HH? and ?hh? for signed char constants.
2. ?HHU? and ?hhu? for unsigned char constants.
3. ?H? and ?h? for short constants.
4. ?HU? and ?hu? for unsigned short constants.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Ioannis Vranos <ivranos@freemail.gr>
Date: Sat, 4 Apr 2009 09:51:10 CST Raw View
The updated proposal, making use of the template class "only", can be
found here (it is a work in progress):
http://www.cpp-software.net/documents/cpp_only_proposal.html
Make sure you clear the cache of your browser.
The old proposal, making use of the "keyword" only (abandoned) can be
found here:
http://www.cpp-software.net/documents/cpp_only_proposal_old.html
This proposal is being "ported" to the template class "only" proposal.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Ioannis Vranos <ivranos@freemail.gr>
Date: Tue, 31 Mar 2009 12:51:32 CST Raw View
Proposal:
We can increase type safety in C++ by adding a single keyword. The
current proposal uses the keyword "only".
Examples:
only int x= 4;
only unsigned x= 4U;
only float f= 4.0F;
void somefuc(only int x);
// It accepts both a const vector<int> and a vector<int> object
void somefunc(const vector<int> &vec);
// It accepts only a const vector<int> object
void somefunc(only const vector<int> &vec);
It is simple like that, and the concept is backwards compatible.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]