Update
If anybody is interested in the proceedings,GitHub project is here. Although it works pretty well, I have toned it down to Arithmetic types only.
Future developments will bring more.
Thank you all
Original Post
Yes, I know about this question. please do read it for a good introduction. And one clever but not universal solution.
I do care for that but I also do need to have as non-convertible, some fundamental types. Having this will severely limit the possibility to introduce bugs. Not just function calls without implicit conversion allowed, but also for variable creations, assignments, comparisons etc.
I am sure this has been attempted and maybe solved before. I must not have this in my program:
// implicit conversions
// no warnings
{
char character = 127 ;
bool truth = 0 ;
size_t size_ = truth ;
int integer = false ;
size_ = integer;
size_ = character ;
}
In my VS 2017 (up to date) this compiles without warnings in the default settings even on Level 4
aka /W4
. Same is with clang 7, etc.
Thus far I have managed to develop this:
namespace dbj {
namespace util {
using namespace std;
template<typename T>
struct nothing_but final
{
static_assert(false == std::is_array_v<T>,
"can not deal with arrays");
using type = nothing_but;
// default creation
nothing_but() : val_(T{}) {}
// allowed conversion
nothing_but(T const & t_) : val_(t_) {}
// allowed assignment
type & operator = (T const & new_val_)
{
val_ = new_val_;
return *this;
}
/*
explictly ban all attempts to construct from
any other type but T
*/
template<
typename X,
std::enable_if_t<
false == std::is_same_v<T, X>
, int> = 0
>
nothing_but(X const & x_) = delete;
/*
explictly ban all attempts to assign from
any other type but T
*/
template<
typename X,
std::enable_if_t<
false == std::is_same_v<T, X>
, int> = 0
>
type & operator = (X const & new_val_) = delete;
/* conversion to X is banned */
template<
typename X,
std::enable_if_t<
false == std::is_same_v<T, X>
, int> = 0
>
operator X & () = delete;
// conversion to T is allowed, but const stays const
operator T & () { return val_; }
// non const value, as other std class types do
T & data() const { return (T&)val_; }
private:
T val_{};
// compatibility
friend bool operator < ( type const & left_, type const & right_)
{
return left_.val_ < right_.val_;
}
};
} // util
} // dbj
Some quick testing:
{
using dbj::util::nothing_but;
nothing_but<int> si1 = 42;
si1 = 42;
nothing_but<int> si2 = 13;
//nothing_but<int> si3 = true ;
//si3 = true;
//nothing_but<int> si4 = '$' ;
//si4 = '$';
//nothing_but<int> si5 = 2.7 ;
//si5 = 2.7;
//nothing_but<int> si6 = size_t(BUFSIZ) ;
//si6 = size_t(BUFSIZ);
si1 = si2; (void)(si1 == si2);
int j = 9;
nothing_but<int *> sip1 = &j;
nothing_but<char const *> scc1 = "OK";
char name[] = "c++";
// scc1 = name;
}
And some constness testing
{ // constnes
const int cc = 42;
const int cb = cc;
nothing_but<int> const & sci1 = cc;
nothing_but<int> const sci2 = sci1 ; // ok
const nothing_but<int> sci3 = sci1 ; // ok
wprintf(L"%d", sci1.data()); // ok
//sci2 = sci1;
//const int ii1 = sci1;
//const int ii2 = static_cast<int>(sci1);
//const int * ii3 = const_cast<int *>(&(sci1));
const int & ii3 = (const int &)(sci1); // ok
int & ii4 = (int &)(sci1); // ok
}
{ // compatibility
// std::vector<nothing_but<bool>> bv{ true, 1 , true };
std::vector<nothing_but<bool>> bv{ true, true , true };
nothing_but<bool> bb = bv[1] ;
bool kb = bb; // OK
//int k = bb;
//int j = bv[1];
std::map< nothing_but<bool>, nothing_but<int> > bm;
bm[true] = 1;
// bm[true] = 2.3;
bm[false] = 0;
// bm[false] = 0.9;
}
And so on. Whatever is commented out does not compile.
So far I can see the potential but I am not sure if I am doing it right? Would you ever think of this as useful, and if you do would you do it differently?
Please do note, I need generic solution I am calling "non convertible basic types". I am hoping it can be done and it might be pretty small and generic, when done.