C++ Strong Typing

Started by
4 comments, last by Tac-Tics 14 years ago
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?
Advertisement
eh?

C++ is strongly typed. That's it. Some other languages are strongly typed as well. Some are not.
Errr.... wrong term. Strong typedef'ing perhaps. Sorry...
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.
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.
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.

This topic is closed to new replies.

Advertisement