Creating a new primitive type

半腔热情 提交于 2019-12-01 18:08:00

I heard a rumour that C++0x will bring strong typedefs which will allow for the utf8 class in your case to be distinguishable from the char, but that doesn't exist currently. Maybe Boost's strong typedef would help, but I don't know.

C++ author Matthew Wilson calls what you're describing "true typedefs." They're implemented as a class containing a single data member (just like you said). He describes them and provides a template implementation in chapter 18 of Imperfect C++ (good book, by the way) and in this article in Dr. Dobb's Journal.

Regarding optimization, any good compiler should be able to optimize this as a primitive (since large parts of the STL, for example, rely on good quality optimizations like this), but I don't have any specifics.

The problem here is that typedef doesn't really define new types (like say Ada, where you can define mutually incompatable integer types), just aliases for existing ones.

I believe one of the proposals for the next version of C++ will include proper types.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!