Sign in to follow this  
popsoftheyear

C++ Strong Typing

Recommended Posts

popsoftheyear    2194
So I've heard mention before of strong typing in C++. I am totally naive of methods for this, and haven't had much luck finding any tricks through google or searching the forums. Besides the obvious overload a class with all operators of that type, for each type you want, what are some methods/the best method (if there are more than 1) for this? 2 Scenerios come to mind, and they may have the same solution for all I know. - An enum-like type with limited choices - A fundamental type that you do NOT want assignable to another (possibly same) fundamental type. For instance they both may be 32 bit integers. What are some drawbacks of doing this?

Share this post


Link to post
Share on other sites
Antheus    2409
As soon as third-party code is involved, any such notions are basically useless.

If however you have adequate control over major portion of the source, then consider investing into tools like PC-Lint.

IMHO, C++ is messed up enough as it is, rather than patching broken concepts with another set of broken concepts, trivialize the code, and use tools to test it.

Share this post


Link to post
Share on other sites
_moagstar_    465
You might want to take a look at BOOST_STRONG_TYPEDEF, even if you're not using boost you can use this as a basic idea of how to implement hack strong typedefs in c++. Basically you wrap the type in another struct and provide conversion, construction functions and any required operators to make the new type act like the orignal type.

Share this post


Link to post
Share on other sites
Tac-Tics    197
If you're interested in type theory, check out Ocaml, Haskell, and Scala. These languages make their type systems a top priority in their design. In Haskell, in particular, you aren't allowed to break the type system at all. It allows a large number of theoretical advantages when it comes time to optimize a Haskell program.

Changing the type system in C++ doesn't make sense. The language is already so damn messed up, adding anything to it only makes it worse.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this