Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there any UB lurking around in here?

22 views
Skip to first unread message

Chris M. Thomasson

unread,
Oct 16, 2022, 12:48:12 AM10/16/22
to
Is this code okay, or busted in some way? In reference to the following
thread:

(static array of reference_wrapper)
https://groups.google.com/g/comp.lang.c++/c/_RszdtnypkE

Well, what horrors lie below? ;^)
______________________________
#include <iostream>
#include <functional>


struct render
{
int m_foo;
};


static render g_farm_concrete[] = {
{ 0 },
{ 1 },
{ 2 }
};


static const std::reference_wrapper<const render> g_farm_refs[] = {
g_farm_concrete[0],
g_farm_concrete[1],
g_farm_concrete[2]
};


int main()
{
std::cout << "g_farm_concrete[0]" << &g_farm_concrete[0] << "\n";
std::cout << "g_farm_concrete[1]" << &g_farm_concrete[1] << "\n";
std::cout << "g_farm_concrete[2]" << &g_farm_concrete[2] << "\n";

std::cout << "\n";

std::cout << "g_farm_refs[0]" << &g_farm_refs[0].get().m_foo << "\n";
std::cout << "g_farm_refs[1]" << &g_farm_refs[1].get().m_foo << "\n";
std::cout << "g_farm_refs[2]" << &g_farm_refs[2].get().m_foo << "\n";

return 0;
}
______________________________


I get:


g_farm_concrete[0]0x562b8e2d9010
g_farm_concrete[1]0x562b8e2d9014
g_farm_concrete[2]0x562b8e2d9018

g_farm_refs[0]0x562b8e2d9010
g_farm_refs[1]0x562b8e2d9014
g_farm_refs[2]0x562b8e2d9018


Just lucky?

Andrey Tarasevich

unread,
Oct 16, 2022, 1:55:16 AM10/16/22
to
On 10/15/2022 9:47 PM, Chris M. Thomasson wrote:
> Is this code okay, or busted in some way? In reference to the following
> thread:
>
> (static array of reference_wrapper)
> https://groups.google.com/g/comp.lang.c++/c/_RszdtnypkE
>
> Well, what horrors lie below? ;^)

Why would there be? I don't see any potential for any problems there.

--
Best regards,
Andrey

Chris M. Thomasson

unread,
Oct 16, 2022, 2:00:50 AM10/16/22
to
Needing to use indirection wrt not being able to have a raw array of
references.
________________________
#include <iostream>

struct foo
{
int m_a;
};

static foo g_foo[] = { { 0 }, { 1 } };
static foo& g_foo_ref[] = { g_foo[0], g_foo[1] };

int main()
{
return 0;
}
________________________

No compile for you! Common Man... ;^)

Andrey Tarasevich

unread,
Oct 16, 2022, 2:05:39 AM10/16/22
to
On 10/15/2022 11:00 PM, Chris M. Thomasson wrote:
> On 10/15/2022 10:53 PM, Andrey Tarasevich wrote:
>> On 10/15/2022 9:47 PM, Chris M. Thomasson wrote:
>>> Is this code okay, or busted in some way? In reference to the
>>> following thread:
>>>
>>> (static array of reference_wrapper)
>>> https://groups.google.com/g/comp.lang.c++/c/_RszdtnypkE
>>>
>>> Well, what horrors lie below? ;^)
>>
>> Why would there be? I don't see any potential for any problems there.
>>
>
> Needing to use indirection wrt not being able to have a raw array of
> references.

But that's what `std::reference_wrapper<>` is for.

--
Best regards,
Andrey



Chris M. Thomasson

unread,
Oct 16, 2022, 2:51:17 AM10/16/22
to
It works for sure. However, it seems like:

static foo g_foo[] = { { 0 }, { 1 } };
static foo& g_foo_ref[] = { g_foo[0], g_foo[1] };

should work as well, but it does not. I forgot the reason why.

Andrey Tarasevich

unread,
Oct 16, 2022, 2:57:18 AM10/16/22
to
The reason for "there shall be no no arrays of references"?

Probably because it would open a lot of unpleasant cans of worms.
Reference is not an object type in C++. It is unspecified whether it
occupies storage. This does not play well with arrays, which require
types of definitive size, which support address arithmetic.

--
Best regards,
Andrey

Chris M. Thomasson

unread,
Oct 16, 2022, 3:18:08 PM10/16/22
to
On 10/15/2022 11:56 PM, Andrey Tarasevich wrote:
> On 10/15/2022 11:50 PM, Chris M. Thomasson wrote:
>> On 10/15/2022 11:05 PM, Andrey Tarasevich wrote:
>>> On 10/15/2022 11:00 PM, Chris M. Thomasson wrote:
>>>> On 10/15/2022 10:53 PM, Andrey Tarasevich wrote:
>>>>> On 10/15/2022 9:47 PM, Chris M. Thomasson wrote:
>>>>>> Is this code okay, or busted in some way? In reference to the
>>>>>> following thread:
>>>>>>
>>>>>> (static array of reference_wrapper)
>>>>>> https://groups.google.com/g/comp.lang.c++/c/_RszdtnypkE
>>>>>>
>>>>>> Well, what horrors lie below? ;^)
>>>>>
>>>>> Why would there be? I don't see any potential for any problems there.
>>>>>
>>>>
>>>> Needing to use indirection wrt not being able to have a raw array of
>>>> references.
>>>
>>> But that's what `std::reference_wrapper<>` is for.
>>>
>>
>> It works for sure. However, it seems like:
>>
>> static foo g_foo[] = { { 0 }, { 1 } };
>> static foo& g_foo_ref[] = { g_foo[0], g_foo[1] };
>>
>> should work as well, but it does not. I forgot the reason why.
>
> The reason for "there shall be no no arrays of references"?

Indeed.


> Probably because it would open a lot of unpleasant cans of worms.
> Reference is not an object type in C++. It is unspecified whether it
> occupies storage. This does not play well with arrays, which require
> types of definitive size, which support address arithmetic.

Yup. That was it! Thanks for refreshing my memory Andrey. Btw, I was
never really a C++ guy, so to speak. C is my main language.

0 new messages