Converting references To bool

Hi

A pointer can be (implicitly) converted to a bool easily:

1
2
3
4
5
6
7
8
9
10
11
12
bool convptr(int* p)
{
    /// Equivalent to p != nullptr
    if (p)  /// implicit conversion of a pointer to a bool
    {
        return true;
    }
    else
    {
        return false;
    }
}


The above code works correctly when the convptr() function is called with a pointer which is (a) initialized (b) un-initialized:

1
2
3
4
5
6
7
8
9
10
int i;

int main()
{
   int* pit1 = &i;
   bool bit1 = convptr(pit1);   // true

   int* pif2 = nullptr;
   bool bif2 = convptr(pif2);	// false
}


There's no problem above.


However, when the concept is analogously extended to references, it doesn't work:

1
2
3
4
5
6
7
8
9
10
11
12
bool convref(int& r)
{
    /// Equivalent to r != NULL
    if (r)
    {
        return true;
    }
    else
    {
        return false;
    }
}


When the convref function is called with an initialized reference, it still returns false:

1
2
   int& rit1 = i;
   bool brit1 = convref(rit1);	/// true expected, but gets false. 


Therefore, I'd like to ask:
1) Can references be converted to bool? If so, how?
2) How do you initialize a reference to null? A pointer can be set to nullptr, but that won't work with a reference.

Thanks
A reference is another name for something.

A reference-to-an-int effectively IS that int.


1) Can references be converted to bool? If so, how?

Can an int be converted to a bool? That's how you convert a reference-to-an-int to a bool.
Can a double be converted to a bool? That's how you convert a reference-to-a-double to a bool.
Can a string be converted to a bool? That's how you convert a reference-to-a-string to a bool.
Can any type be converted to a bool? That's how you convert a reference-to-an-any-type to a bool.

2) How do you initialize a reference to null?

You can't.

A reference is another name for something.
How can you initialise an int to null? You can't.
How can you initialise a double to null? You can't.
How can you initialise a string to null? You can't.


When you "create" a reference, you're not creating a new object. You're saying that this object that already exists has another name.

1
2
int i;  // this creates an int, called i
int& p = i;  // this does not create an int. It says that the int that already exists, named i, now has ANOTHER name. It's called i, and it's also called p 



The compiler may or may not choose to create extra pointers for itself to help it keep track of references, but that's up to the compiler. A reference is NOT some kind of fancy auto-dereferencing pointer. It's another name for some object.
Last edited on
Thanks for your reply. Your answer is spot-on.

This means there was nothing wrong with either the convref() function that I posted earlier or its invocation. The reason convref() returned a false when invoked with rit1, was that rit1 is a reference to i. Since i was not explicitly initialized, it was implicitly initialized to 0. Therefore convref() returned a false as expected.

We can verify this by also invoking convref() with another reference to an int that is now initialized to a non-zero value. convref() should then return true. The following invocation does just that:

1
2
3
4
5
6
7
8
9
10
int t = 1, f = 0;

int main()
{
   int& rit1 = t;
   bool brit1 = convref(rit1);		// true

   int& rif2 = f;
   bool brif2 = convref(rif2);		// false
}
Since i was not explicitly initialized, it was implicitly initialized to 0.

No. Since i was not explicitly initialized, its value remained uninitialized and thus undefined. The compiler/OS implementation is free to do nothing or anything. IIRC, MS compiler does add "set to 0" instruction, if you do a debug-build. In release-build it does not.
i is a global variable, so it is safe to assume that it is implicitly initialized to 0.

It's good to remember, though, that the rules are different here for global-vs-local.
Topic archived. No new replies allowed.