Click here to Skip to main content
14,937,556 members
Articles / Programming Languages / C++/CLI
Article
Posted 18 May 2005

Stats

58.3K views
563 downloads
10 bookmarked

CNullable<T> : A convenient Nullable<T> equivalent class for C++/CLI

Rate me:
Please Sign up or sign in to vote.
4.79/5 (18 votes)
18 May 20052 min read
CNullable<T> can be used to represent a value type (or simple native type) such that the type can be nulled. You can also compare it to nullptr using the == and != operators both of which have been overloaded.

Overview

Starting with Whidbey, C# supports nullable types (value types that can also be null) and it does this using the

MC++
System::Nullable<T>
generic
class defined in the mscorlib assembly. It also provides a short-cut form, T? var which corresponds to
C#
System.Nullable<T> 
var
. Take a look at the following C# code snippet :-

C#
int? q1 = null;

if (q1.HasValue)
    Console.WriteLine(q1.Value);
else
    Console.WriteLine("It's null");    

q1 = 99;
q1++;

int? q2 = q1;
if (q2 != null)
{
    q2 = null;
}

Now, take a look at the corresponding C++ code (C# code in comments) :-

MC++
//int? q1 = null;
Nullable<int> q1;

//if (q1.HasValue)
//    Console.WriteLine(q1.Value);
//else
//    Console.WriteLine("It's null");

if(q1.HasValue)
    Console::WriteLine(q1.Value);
else
    Console::WriteLine("It's null");

//q1 = 99;

q1 = Nullable<int>(99);

//q1++;

if(q1.HasValue)
    q1 = Nullable<int>(q1.Value + 1);

//int? q2 = q1;

Nullable<int> q2 = q1;

//if (q2 != null)
//{
//    q2 = null;
//}

if( q2.HasValue )
{
    q2 = Nullable<int>();
}

Not very convenient, is it? Now look at this C++ code :-

MC++
//int? q1 = null;
CNullable<int> q1;

//if (q1.HasValue)
//    Console.WriteLine(q1.Value);
//else
//    Console.WriteLine("It's null");

if(q1.HasValue)
    Console::WriteLine(q1.Value);
else
    Console::WriteLine("It's null");

//q1 = 99;

q1 = 99;

//q1++;

(*q1)++;

////int? q2 = q1;

CNullable<int> q2 = q1;

//if (q2 != null)
//{
//    q2 = null;
//}

if(q2 != nullptr)
{
    q2 = nullptr;
}

Well, that's a lot more closer to the C# code as far as ease-of-use is concerned, is it not? That's why I wrote my

MC++
CNullable<> 
template ref
class for C++/CLI.

Class Reference

MC++
template<typename T> ref class CodeProject::Extra::CNullable sealed : 
INullableValue

CNullable can be used to represent a value type (or simple native type) such that the type can be nulled. You can also compare it to nullptr using the == and != operators both of which have been overloaded.

Constructors

  • CNullable()

    The default constructor creates a CNullable object assigned to nullptr.

    MC++
    CNullable<char> x1;
  • CNullable(void* p)

    This overload handles the case where a nullptr is passed to the constructor. If the pointer passed is not a

    MC++
    nullptr
    
    , an InvalidOperationException gets thrown.

    MC++
    CNullable<double> x2(nullptr);
  • CNullable(const T% t)

    Constructs a CNullable object using the passed in template-argument type.

    MC++
    CNullable<int> x3(100);
  • CNullable(const CNullable% n)

    Copy constructor.

    MC++
    CNullable<int> x4(x3);

Operators

  • operator T()

    Converts to type T.

    Note - If the object is currently nulled, an

    MC++
    InvalidOperationException
    
    is thrown.

    MC++
    int y = x4;
  • void operator =(void* p)

    Assignment operator that handles the

    MC++
    nullptr
    
    case.

    Note - If

    MC++
    p
    
    is a non-null pointer, an
    MC++
    InvalidOperationException
    
    is thrown.

    MC++
    x3 = nullptr;
  • const T operator =(const T% t)

    Assignment operator for type T.

    MC++
    x4 = x3 = 13;
  • T% operator*()

    The underlying type is exposed by overloading the dereference operator.

    MC++
    x5 = (*x4)++;
  • bool operator ==(void* p)

    Allows

    MC++
    ==
    
    comparison with
    MC++
    nullptr
    
    .

    MC++
    if( x5 == nullptr )
    {
    }
  • bool operator !=(void* p)

    Allows

    MC++
    !=
    
    comparison with
    MC++
    nullptr
    
    .

    MC++
    if( x5 != nullptr )
    {
    }
  • operator String^()

    Gives the string representation of the type. Internally calls the

    MC++
    ToString
    
    method.

    MC++
    Console::WriteLine( (String^)x5 );

Methods

  • virtual String^ ToString() override

    Returns an empty string if the object is currently nulled, else calls

    MC++
    ToString
    
    on the underlying template-argument type member and returns that string.

    MC++
    Console::WriteLine( x5.ToString() );
  • Nullable<T> CreateNullable()

    Creates a new Nullable<T> object (useful to interact with .NET code that explicitly expects a Nullable<T>). Normally you'd be able to pass a CNullable object directly since it implements INullableValue.

    MC++
    Nullable<int> n1 = x5.CreateNullable();
  • static CNullable^ FromNullable(Nullable<T> n)

    Static method to create a new CNullable object from a Nullable<T> object.

    MC++
    CNullable<int> x6 = CNullable<int>::FromNullable(n1);

Properties

  • property virtual bool HasValue::get

    Implementation of INullableValue::HasValue. Indicates whether the value is valid or whether it's a null-value.

    MC++
    if(x6.HasValue)
  • property virtual Object^ Value::get

    Implementation of INullableValue::Value. If

    MC++
    HasValue
    
    is true, it returns the value of the internal template argument type member, otherwise throws an
    MC++
    InvalidOperationException
    
    exception.

    MC++
    Console::WriteLine(x6.Value);

History

  • May 18th, 2005 : Article first published on The Code Project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Nish Nishant
United States United States
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website : www.ganymedesoftwaresolutions.com

Comments and Discussions

 
QuestionCan you port it to C++? Pin
CP Visitor18-May-05 2:11
MemberCP Visitor18-May-05 2:11 
GeneralAnd if you're not using .Net... Pin
Stuart Dootson18-May-05 2:10
professionalStuart Dootson18-May-05 2:10 
You could consider using Boost.Optional[^]. In fact, Nish - I find the syntax that Boost.Optional presents is nicer than the C# syntax:

void SomeFunction(boost::optional<int> x)
{
   if (x)
   {
      std::cout << *x << "\n";
   }
   else
   {
      std::cout << "No value for x passed\n";
   }
}


You can see that Optional is kind of like a pointer.

Stuart Dootson

'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
GeneralRe: And if you're not using .Net... Pin
Nish Nishant18-May-05 6:16
sitebuilderNish Nishant18-May-05 6:16 
Generalfixed link Pin
peterchen14-Apr-11 9:33
Memberpeterchen14-Apr-11 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.