views:

216

answers:

2

Hello everyone,

I think I am missing smth back in my theoretical background on this thing. I know there were similar posts but I still do not get it.

I have such a code:

void somefunc1(Word &Key)
{
    somefunc2(Key);
}

void somefunc2(char &char1)
{
    return;
}

compiler generates me an error here:

somefunc2(Key);

[BCC32 Error] Unit1.cpp(830): E2357 Reference initialized with 'unsigned short', needs lvalue of type 'char'

I found out that it is because of the ANSI 2003 regulation on C++ dealing with temporaries and references but I still do not get what is wrong here.

when I do c-style conversion:

somefunc2( *(char*)&Key )

it resolves an issue.

Can anyone hint me what is wrong and why is it wrong?

+8  A: 
 WORD &Key;

A reference is always an alias for some other object, and it must be initialized with an object that already exists. Thus, the above declaration is not valid. The following is instead correct:

 WORD &Key = alreadyExistingKey;

[The above is not relevant anymore, the question has changed.]

EDIT:

void somefunc1(Word &Key)
{
   somefunc2(Key);
}
void somefunc2(char &char1)
{
   return;
}

[BCC32 Error] Unit1.cpp(830): E2357 Reference initialized with 'unsigned short', needs lvalue of type 'char'

The compiler is telling you that somefunc2 is expecting [a reference, that is, an alias for] a char. But Key in somefunc1 is instead a Word, which I understand to be a typedef for unsigned short.

It seems to me that your "c-style" remedy is brutally reinterpreting &Key, which is the address of an unsigned short, as the address of a char. What you are passing to somefunc2 is therefore the first byte of Key, interpreted as a (signed) char. I guess that the result depends on endianness. I wouldn't rely on that code.

Federico Ramponi
Can you, please, also hint what would be a good thing to do in this case? These are just two functions I cannot change (do not have access to the source) and this sort of implicit conversion I am not a huge fan of but have to deal with. reinterpret_cast most probably would be the same brutal approach here I guess?
Andrew
@Andrew: If you don't have access to the source and the functions don't compiler together, what exactly do you have? What is the source that you have posted if not 'these functions'?
Charles Bailey
@Andrew: To recommend what you should do, we first need to know how the data is used by those functions. Assuming standard data sizes, you have a reference to 2 bytes of data that you're trying to pass into a function that takes a reference to only 1 byte. We don't know which byte should be passed. Furthermore, we don't know if somefunc2 modifies the referenced byte, and if it does then we don't know what should happen to the other byte of data.There are various ways to write somefunc1 depending on what its purpose actually is.
TheUndeadFish
+4  A: 

Temporaries cannot be bound to non constant references.

You should have written this:

void somefunc2(const char &char1)

{

return;

}

Prasoon Saurav
Great response! Clear and concise.It helped to understand the breach in my knowledge.Now it seems very obvious.
Andrew
Where (i.e. which variable/reference) is the temporary here?
mlvljr