typesafe typedef in C++

前端 未结 6 1589
南方客
南方客 2020-12-10 03:41

I would like to use something like typedef in my C++ programs to enhance type safety.

As an example, suppose I have two functions

void function1(unsi         


        
6条回答
  •  無奈伤痛
    2020-12-10 04:12

    Use Boost strong typedef:

    typedef creates an alias for an existing type. It does not create a new type that can be used for matching either function or template parameters...

    Usage of BOOST_STRONG_TYPEDEF addresses this...

    BOOST_STRONG_TYPEDEF is a macro which generates a class named "name" wraps and instance of its primitive type and provides appropriate conversion operators in order to make the new type substitutable for the one that it wraps.

提交回复
热议问题