user81993
user81993

Reputation: 6613

What is the C++CLI equivalent of vb's Nothing?

I'm in this kind of a place

generic <typename ItemType> where ItemType : ItemBase
public ref class Container {
    ItemType GetItem(int i) {
        ...
        if (someSpecialCondition) return ???
        ...
    }
};

I want to return the equivalent of vb's "Nothing" but can't figure out the syntax for it. It doesn't like null or nullptr, I know that much.

Upvotes: 1

Views: 106

Answers (1)

Hans Passant
Hans Passant

Reputation: 942247

It is pretty unintuitive for generics, note that it can't be nullptr if the type parameter is a value class. It also does not match the language spec, which promises that nullptr is valid when the type is constrained to ref class.

The default value for type T is T(). So it is:

ItemType GetItem(int i) {
    ...
    if (someSpecialCondition) return ItemType();
    ...
}

Which produces nullptr if ItemType is a reference type and the default value (all members zero-initialized) when ItemType is a value type. Same thing that Nothing does in VB.NET

Upvotes: 2

Related Questions