Null pointer to struct which has zero size (empty)... It is a good practice?

Posted by ProgramWriter on Stack Overflow See other posts from Stack Overflow or by ProgramWriter
Published on 2010-04-17T07:23:21Z Indexed on 2010/04/17 7:33 UTC
Read the original article Hit count: 104

Filed under:
|

Hi2All..

I have some null struct, for example:

struct null_type
{
    NullType& someNonVirtualMethod()
    {
        return *this;
    }
};

And in some function i need to pass reference to this type. Reason:

template <typename T1 = null_type, typename T2 = null_type, ... >
class LooksLikeATupleButItsNotATuple
{
public:
    LooksLikeATupleButItsNotATuple(T1& ref1 = defParamHere, T2& ref2 = andHere..) 
        : _ref1(ref1), _ref2(ref2), ...
    {
    }

    void someCompositeFunctionHere()
    {
        _ref1.someNonVirtualMethod();
        _ref2.someNonVirtualMethod();
        ...
    }

private:
    T1& _ref1; 
    T2& _ref2; 
    ...;
};

It is a good practice to use null reference as a default parameter?:

*static_cast<NullType*>(0)

It works on MSVC, but i have some doubts...

© Stack Overflow or respective owner

Related posts about c++

Related posts about template