Preventing automatic casts with integers


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++?


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.


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).


  • Java完成POI的功能
  • Repeatedly calling python from Java in the most efficient way
  • An error occurred while creating the AVD. See idea.log for details. ubuntu 16.04
  • Make a huge dict from multiple numpy arrays
  • How do I paginate through a ViewModel in MVC CORE?
  • How do I specify the database isolation level to be used, at a high level?
  • Terms of use data from Facebook API
  • Android: Is there a uses-feature option for the sd card?
  • Is it safe to add a new method to Python Thread class?
  • double d=1/0.0 vs double d=1/0
  • How to use bash to run `mkdir -m -p`?
  • how to pass member function pointer to std::function
  • Should we call the super class before or after we execute some code
  • MethodNotAllowedHttpException while update record in laravel
  • How can one tell if a local variable is 'final' from Java bytecode? (Related to BCEL)
  • Combination of all rows in two numpy arrays
  • Casting errors when attempting to return an IQueryable
  • File.Copy target file is a directory, not a file.
  • Signed bit field in C++14
  • How to add a post tsc build task that copy files?
  • Neo4j Cypher WITH is required between CREATE and MATCH
  • Backtick in ECMASCRIPT : Unexpected Token Illegal
  • Access SignalR Hub without Constructor Injection
  • How to tell if an error captured by my global.asax was displayed on the screen of the user
  • Lazy Evaluation - Space Leak
  • Human readable URL Scheme for ASP.NET MVC
  • How to hide 'Add To Cart' for variable products, but keep product variations visible
  • Multiple custom authentication with spring security
  • Insert statement not working using execute(array()) of PDO Extension
  • Force fading edge on TextView
  • Videos won't upload
  • In metro, get all inherited classes of an (abstract) class?
  • Defer unused CSS
  • Modifying native query cannot have named parameter bindings?
  • WPF custom control and direct content support
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?