Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Returning Nulls in Templates

Reply
Thread Tools

Re: Returning Nulls in Templates

 
 
Öö Tiib
Guest
Posts: n/a
 
      03-27-2011
On Mar 27, 5:24*am, Ruben Safir <(E-Mail Removed)> wrote:
> On Sat, 26 Mar 2011 06:08:12 -0700, James Kanze wrote:
> > As would string(). *The real question is whether he wants an empty
> > string, or a special null value, indicating no value present

>
> My real question is beginning to be why doesn't C++ have a universal
> NULL value that every type can take as a value. *I know that this sounds
> like a bit of sour grapes but it is esacaping me at this point why so
> much thought has to go into returning a NULL 00000000 from a function
> template when you don't know the datatypes before it is instantiated.
>
> sigh...


Because C++ is not strictly OOP language. C++ is multi-paradigm
language and lot of people using it for something do not care about
OOP. So all types are not derived from some nullable "object" class
and all these people do not pay for something they don't use.

Other issue is that there are so lot of meanings of that "null",
"zero", "false", "unknown", "empty", "not available", "failed",
"default", "not set" and so on in different contexts of whole software
industry where C++ is used. Even some fundamental types have several
values carrying different nuances of nullness (float may be 0, NA, -
INF and +INF).

Assigning NULL to instance of Person class is therefore ambiguous and
confusing in C++. People assign Person() instead and say "default-
constructed" in contracts of their interfaces. If the value of default-
constructed object does not carry the meaning in their context then
they use some boost:ptional<T>, Fallible<T>, Nullable<T> to be
precise. If they do not want to be intrusive with such custom types
then they use std:air<T,bool> and describe precise meaning in
contract documentation.

 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      03-28-2011
On Mar 28, 12:46*am, Ruben Safir <(E-Mail Removed)> wrote:
> On Sun, 27 Mar 2011 01:43:08 -0700, Öö Tiib wrote:
> > If they do not want to be intrusive with such custom types then they use
> > std:air<T,bool> and describe precise meaning in contract
> > documentation.

>
> Thank you. *Can you extrapolate on this?


Well ... you may return std::make_pair(T(),false) when you don't have
anything to return and std::make_pair(value,true) when you have a
'value' to return. The cases when and why it may happen that you don't
have anything to return you write into documentation.
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      03-28-2011
On Mar 28, 7:50*pm, Leigh Johnston <(E-Mail Removed)> wrote:
> On 28/03/2011 17:39, Öö Tiib wrote:
>
> > On Mar 28, 12:46 am, Ruben Safir<(E-Mail Removed)> *wrote:
> >> On Sun, 27 Mar 2011 01:43:08 -0700, Öö Tiib wrote:
> >>> If they do not want to be intrusive with such custom types then they use
> >>> std:air<T,bool> *and describe precise meaning in contract
> >>> documentation.

>
> >> Thank you. *Can you extrapolate on this?

>
> > Well ... you may return std::make_pair(T(),false) when you don't have
> > anything to return and std::make_pair(value,true) when you have a
> > 'value' to return. The cases when and why it may happen that you don't
> > have anything to return you write into documentation.

>
> Better would be to use optional variables such as what Boost.Optional
> provides as they don't require you to default construct the value type.


Indeed i also said it initially. It is worth to note however that
boost:ptional is quite significant dependency. So i initially
replied that pair is less intrusive option and so he wanted me to
extrapolate on usage of that pair.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-28-2011
On Mar 27, 3:24 am, Ruben Safir <(E-Mail Removed)> wrote:
> On Sat, 26 Mar 2011 06:08:12 -0700, James Kanze wrote:
> > As would string(). The real question is whether he wants an empty
> > string, or a special null value, indicating no value present


> My real question is beginning to be why doesn't C++ have a universal
> NULL value that every type can take as a value.


Because it would add to the cost of most types, isn't necessary
in general, and is easy to implement if you need it.

> I know that this sounds like a bit of sour grapes but it is
> esacaping me at this point why so much thought has to go into
> returning a NULL 00000000 from a function template when you
> don't know the datatypes before it is instantiated.


For historical reasons, pointers are special. They do have a
special null value.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-28-2011
On Mar 28, 5:39 pm, Öö Tiib <(E-Mail Removed)> wrote:
> On Mar 28, 12:46 am, Ruben Safir <(E-Mail Removed)> wrote:


> > On Sun, 27 Mar 2011 01:43:08 -0700, Öö Tiib wrote:
> > > If they do not want to be intrusive with such custom types
> > > then they use std:air<T,bool> and describe precise
> > > meaning in contract documentation.


> > Thank you. Can you extrapolate on this?


> Well ... you may return std::make_pair(T(),false) when you don't have
> anything to return and std::make_pair(value,true) when you have a
> 'value' to return. The cases when and why it may happen that you don't
> have anything to return you write into documentation.


In practice, every place I've worked at has had a Fallible type,
which didn't require a default constructor.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-28-2011
On Mar 28, 6:15 pm, Leigh Johnston <(E-Mail Removed)> wrote:
> On 28/03/2011 18:08, Öö Tiib wrote:


[...]
> It is a fairly trivial exercise to roll your own optional
> variable class (I did as I don't use Boost).


Once you've done so, yes. For someone who's never done it,
there are a few things that can go wrong, and which require
non-trivial solutions in C++03, at least if you want to do it
efficiently (no dynamic allocation, no more memory than is
needed).

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Returning Nulls in Templates Alf P. Steinbach /Usenet C++ 7 03-26-2011 12:59 PM
Hash enumeration returning nulls? cczona Ruby 1 03-26-2010 04:51 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
Problem with nulls Bob ASP .Net 0 07-02-2003 07:11 PM



Advertisments