Is It Possible to Return a String from a Function ?

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Sun, 23 Jul 2000 04:00:00





> > [...]

> > > Is it not a good idea to always set pointers to NULL?

> > It depends. "Always" isn't a useful word in computer programming. Also,
0 is
> > to be preferred to NULL. Who knows what some dork has set NULL to in a
> > header file somewhere? Whereas the definition of 0 as the null pointer
is
> > guaranteed by the language.

>     NULL is the proper way to assign a pointer a value that does not
> point to anything.
>     0 is an integer. Although compilers generously convert the integer
> to a NULL for us, it adds a confusion factor that is unnecessary. It is
> not always intuitively obvious from a code listing that 0 is meant to be
> NULL. Also using a 0 creates a "magic number," a raw value whose meaning
> is not obvious.

Right.  And what I was talking about is the following.  If we had NULL
defined in the language as a special case (a null-pointer constant that
can be converted to a pointer to any type implicitly), then we won't
have this trouble here:

#include <iostream>

void foo(char*)
{
    std::cout << "foo(char*)\n";

Quote:}

void foo(int)
{
    std::cout << "foo(int)\n";

Quote:}

int main()
{
    foo(NULL);  // prints "foo(int)"

Quote:}

Passing 0 would mean a call to foo(int) without a doubt in this case,
and I think that such a change in the Standard won't break any
existing code.  We can also allow implicit conversion if (int)0 into
a null-pointer value.  I do sincerely hope that it will make many
programmers' lives easier.

Any word from the circles close to the C++ Standard Committee?

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Is It Possible to Return a String from a Function ?

Post by David Harmo » Mon, 24 Jul 2000 04:00:00


On Fri, 21 Jul 2000 12:50:06 -0700 in comp.lang.c++,


>OK.  I see.  However, it still escapes me WHY C++ can't have a null
>pointer constant as a special case

There is no reason.  Null pointers are a special case anyway.  They are
defined as any const expression with a value of zero and a type from the
approved list.  The committee just decided to omit (void *) from the
approved list, for no good reason that I know of.  Including it would
give greater C compatibility, greater type safety (Since (void *)(0)
would not be mistakable for some int value in the wrong context) and
would not interfere with the conversion rules for pointers any more than
the current definition of NULL interferes with conversion rules for int.

 
 
 

Is It Possible to Return a String from a Function ?

Post by choi » Tue, 25 Jul 2000 04:00:00


I copied and pasted the following code and have tested in Linux(Redhat 6.1).
Strangely, output was "foo(char*)".
    $> g++ t.cpp
    $> ./a.out
         foo(char*)
    $>



Quote:

> #include <iostream>

> void foo(char*)
> {
>     std::cout << "foo(char*)\n";
> }

> void foo(int)
> {
>     std::cout << "foo(int)\n";
> }

> int main()
> {
>     foo(NULL);  // prints "foo(int)"
> }

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Tue, 25 Jul 2000 04:00:00



> On Fri, 21 Jul 2000 12:50:06 -0700 in comp.lang.c++,

> >OK.  I see.  However, it still escapes me WHY C++ can't have a null
> >pointer constant as a special case

> There is no reason.  Null pointers are a special case anyway.  They
are
> defined as any const expression with a value of zero and a type from

the
            ^^^^
Not "any".  (4.10) "Null pointer constant is an integral constant
expression (5.19) rvalue of integer type that evaluates to zero".
Integer type.

Quote:> approved list.  The committee just decided to omit (void *) from the
> approved list, for no good reason that I know of.  Including it would
> give greater C compatibility, greater type safety (Since (void *)(0)
> would not be mistakable for some int value in the wrong context) and
> would not interfere with the conversion rules for pointers any more
than
> the current definition of NULL interferes with conversion rules for
int.

That's what I meant.  Care to join me in lobbying the Committee?  Of
course, it would make sense to check in the list of suggestions... Do
you have any idea where such list is?  I remember seeing it on the Web
somewhere.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Is It Possible to Return a String from a Function ?

Post by Victor Bazaro » Tue, 25 Jul 2000 04:00:00



> I copied and pasted the following code and have tested in Linux(Redhat
6.1).
> Strangely, output was "foo(char*)".
>     $> g++ t.cpp
>     $> ./a.out
>          foo(char*)
>     $>



Well, it shouldn't have been, if I read the Standard correctly.  NULL
MUST not be defined as (void*)0.  It is most likely defined as 0.  And
conversion rules should have forced the compiler to interpret it the
shortest way possible (int over char*).

But that kind of confusion is exactly what I was talking about.  And
although there is enough information in the Standard to know what the
output is going to be, some will still ge confused (like gcc in that
case).  It can be corrected and it should be corrected.


> > #include <iostream>

> > void foo(char*)
> > {
> >     std::cout << "foo(char*)\n";
> > }

> > void foo(int)
> > {
> >     std::cout << "foo(int)\n";
> > }

> > int main()
> > {
> >     foo(NULL);  // prints "foo(int)"
> > }

Victor
--
Please remove capital A's from my address when replying by mail
 
 
 

Is It Possible to Return a String from a Function ?

Post by Mark Wilde » Tue, 25 Jul 2000 04:00:00



>     0 is an integer. Although compilers generously convert the integer
> to a NULL for us

There's nothing "generous" about it, if it's part of the definition of the
language.

Quote:>, it adds a confusion factor that is unnecessary. It is
> not always intuitively obvious from a code listing that 0 is meant to be
> NULL.

I've never seen any code where this would make a difference.

Quote:>Also using a 0 creates a "magic number," a raw value whose meaning
> is not obvious.

I don't think 0 (or 1) are magic numbers, though any others are. And if the
language defines that 0 can be cast to the null pointer, that's all I need
to know to avoid confusion.

But really, this is just an anti-C prejudice of mine. I don't like all
uppercase identifiers. :)