typesafe typedef in C++

前端 未结 6 1607
南方客
南方客 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:24

    As you say, a typedef won't help you here. I can't think of a better way immediately, however if you go with your wrapping in a struct/class option you could use a conversion operator to eliminate the member method or function call.

    For example:

    struct WrappedType
    {
        operator type()
        {
             return _value;
        }
    
        type _value;  
    }
    

    I'm not saying this is the way to do it mind you ;-)

提交回复
热议问题