I'm developing an audio application (in C++) and I have lots of functions that call each other that either take the number of frames (i.e. 1 mono or 2 stereo float samples) or the raw number of samples...
It is getting harder to keep track of the semantics of each function (samples or frames?) and when to mult or div by nChannels, so I would like to somehow do a typedef samples_t and frames_t (to unsigned int) and have the compiler help me out...
Is there any simple way to have an implicit conversion from frames_t to samples_t be marked as an error in C++?Answer1:
Not unless frames_t and samples_t are actually different types. You could make them POD structs containing an integer, but typedefing them will have no effect.Answer2:
You could go all out and define your own system within <a href="http://www.boost.org/doc/libs/release/doc/html/boost_units/Quick_Start.html" rel="nofollow">boost.units</a>, or alternatively just use the <a href="http://www.boost.org/doc/libs/release/libs/serialization/doc/strong_typedef.html" rel="nofollow">
BOOST_STRONG_TYPEDEF</a> macro to create a strong typedef for each type.
You can't do it with
typedef but boost can help you out.
If all you want is non-convertible types,
BOOST_STRONG_TYPEDEF will do the trick: It will allow you to make up multiple types that are all
unsigned but not convertible between each other when being passed around.
But in your particular application you may wish to consider the boost units framework where you can actually create units types, where the results from arithmetic inherit the proper units (so for example
freq * time yields unitless).