c++ - How to convert 0 (and only 0) into my data structure -


i implementing si unit type system. ensure units don't leak in , out, don't want implicit conversion of values unit, , vice-versa. however, convenient able convert 0 unit system, bit can pointers, 0 implicitly converts null pointer, no other value does.

so question is: can replicate kind of implicit conversion of 0 null pointer? or can use advantage achieve same thing?

for example think of such constructor?

constexpr unit_t(void*) : _value(0) { } 

note: unit in type, not in value.

edit: why casting 0 unit?

the reason want this, write algorithms don't know units. example, if have matrix of lengths , want invert it, first compute determinant, , if it's not 0, return inverse (otherwise, error).

so convenient treat 0 literal generic unit. , happens pointers. can type:

void* c = 0; 

but not:

void f(int a) { void *c = a; } 

the classical solution pointer private type, user can't name. way, way can match through implicit conversion 0. (using void* means practically can convert pointer can passed.)

note solution works integral constant 0. if user wants use 0.0 (because thinks double values make more sense), can't.

you might consider using default constructor, client can write someunit() when wants default initialization.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -