How to default-initialize local variables of built-in types in C++?

后端 未结 5 752
春和景丽
春和景丽 2021-01-06 20:34

How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef:

typedef unsigned char boolean;//that\'s Microsoft RPC         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 21:31

    You could provide a wrapper that behaves as the underlying type through overloaded conversion operators.

    #include 
    
    template 
    class Type
    {
        T t;
    public:
        Type(const T& t = T()): t(t) {}
        operator T&() { return t; }
        operator const T&() const { return t; }
    };
    
    int main()
    {
        Type some_value;
        assert(some_value == '\0');
    }
    

    This should be a rather OK usage for conversion operators.

提交回复
热议问题