简体   繁体   中英

In C++ how to pass uint16_t variable to the function with an argument unsigned int &?

I have a variable uint16_t a=35; and I have a function

UINT Read(unsigned int& nVal);

How do I pass a to Read() as unsigned int& ?

If I pass like this

Read(a); 

I am getting the error below:

cannot convert parameter 1 from 'uint16_t' to 'unsigned int &

You will need to copy the value into a (named) temporary, call the function, and then copy the temp back (possibly after checking for overflow).

uint16_t a = 35;
...
unsigned int temp = a;
const unsigned int result = Read(temp);
// check for overflow here
a = temp;

Of course, if you can change the definition of a to be unsigned int , then that is much more straightforward (but I assume that is not possible for other reasons).

Since Read takes a reference to a non-const unsigned int , you cannot pass it a temporary. You must create an actual unsigned int somewhere to bind to this reference:

unsigned int au16 = a;
Read(au16);
a = au16; // don't forget to impact on a the changes made on au16

The other answers all give a solution, but I thought I'd delve a bit deeper in why you see the error in the first place.

What's happening is, for your compiler uint16_t and unsigned int are different types (it most probably implements unsigned int s with 32 bits) and uint16_t is convertible into unsigned int , so there is an implicit cast hidden in the function call.

This cast generates a temporary , that is, a nameless entity that is then passed down to the function as parameter. Since the function takes a non-const reference, there is the possibility that the function will modify the referenced variable. Since the referenced variable is not a , but the temporary, any change happening to it would be (invisibly) lost.

To prevent this, C++ forbids you to pass temporaries (or, more correctly, rvalue s , of which temporary is just one type) as non-const references.

Please note that, for const references, the problem of accidentally losing modifications doesn't exist, because you can't modify the referenced value. Thus, C++ doesn't forbid you passing rvalue s there.

The short answer is that you don't.

Create a variable of type unsigned int , copy the value to it, pass that variable to the function. If needed, after calling the function, copy the value back.

uint16_t a = 16;
unsigned temp_a = a;
Read(temp_a);

// check value of temp_a if needed

a = temp_a;

Attempting to pass a directly may work, if uint16_t and unsigned provided by your compiler are actually the same type. However, that is not guaranteed by the standards, so is not guaranteed by all compilers. Where uint16_t and unsigned are different types, passing a directly (if you can force the compiler to accept it) will yield undefined behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM