c++ Nullable implementation

Do you know a good c++ implementation of the Nullable Generic in c#?
Something like Nullable<class T>

Is there a good way to avoid using the .Value member to get the value?
Nullable<float> Number;
 
float x=Number.Value; //Can I avoid the '.Value' here somehow? and use x=Number? without custom casting staff?

Open in new window

HanochrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Not being a C# expert and going just by the docs I think something like below (with a few tweaks maybe) will model the behavior you are after.

>> Is there a good way to avoid using the .Value member to get the value?
I've overloaded operator *()

template < typename T>
class Nullable
{
public:
   Nullable(T * p = 0)
      :p_(p)
   {
   }
 
   bool HadValue() const
   {
      return p_ != 0;
   }
 
   T const & Value() const
   {
      return *p_;
   }
 
   T & Value()
   {
      return *p_;
   }
 
   T const & operator * () const
   {
      return *p_;
   }
 
   T & operator * ()
   {
      return *p_;
   }
 
   T * p_;
 
private:
};
 
int main()
{
   bool b = true;
 
   Nullable<bool> bnYes(&b);
   Nullable<bool> bnNo;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
I see evilrix already posted his suggestion. Mine's slightly different, so I'll post it too (it's a rough example, just to give an idea).


>> Is there a good way to avoid using the .Value member to get the value?

An overloaded cast operator like below should do the trick.
#include <iostream>
 
template <class T>
class Nullable {
  private :
    T data;
    bool null;
 
  public :
    Nullable(T d) : data(d), null(false) { }
    Nullable() : data(), null(true) { }
 
    bool hasValue() { return !null; }
    T value() { return data; }
    
    operator T() { return data; }
};
 
int main(void) {
  Nullable<float> numberA = Nullable<float>(5.0);
  Nullable<float> numberB = Nullable<float>();
  
  (numberA.hasValue()) ? (std::cout << numberA.value()) : (std::cout << "NULL"); std::cout << std::endl;
  (numberB.hasValue()) ? (std::cout << numberB.value()) : (std::cout << "NULL"); std::cout << std::endl;
  
  float value = numberA;
  std::cout << value << std::endl;
  
  return 0;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.