convert unsigned int *' to 'unsigned int *& - c++

I have a function which gets 'unsigned int *& argument
The parameter I want to transfer in my code is located in the std::vector<unsigned int> data
So,what I do is : I transfer the following parameter &data[0]
But get the compilation error:
unsigned int *' to 'unsigned int *&
What can be a work around?
Thanks,

&data[0] is an rvalue and cannot be bound to non-const reference.
You can make it work this way:
unsigned int *ptr = &data[0];
func(ptr);
But possibly it's better to just change the signature of your function to
void foo(unsigned int &val); //or any other return type
There is a sense of passing a reference to a pointer in case you want to make a pointer point somewhere else. But I don't see a reason to do so in your case

The expression &data[0] yields indeed an r-value, which your function cannot accept.
A simple work-around if you don't want to alter your function (make sure you understand the reasons it requires a reference):
unsigned int* ptr = &data[0];
func(ptr);

Write
unsigned int *argument = &data[0];
call_function(argument);
Be aware that the function may be expecting to change the value of argument, so it may expect you to then write:
data.assign(argument, argument + new_length);

&data[0] is a rvalue, but the function parameter type is non-constant reference to a pointer. A non-const reference can't be bound to an rvalue.
It's hard to suggest a good workaround until OP provides more context for his problem.

Related

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 ints 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, rvalues, 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 rvalues there.

What C-data type do I have to use?

I have to call an external function in the head of which there is something like
void asd(unsigned int *&name)
My question is now what type of variable is name? I know it should function as an array but I am a bit confused, as passing a simple unsigned-int-array did not work.
edit:
Due to the kind help of you I tried:
unsigned int* name[];
asd(feld);
which gives me an error like "storage size of name isn't known" but when I write
unsigned int* name[10];
asd(feld);
I get "invalid initialization of non-const reference of type unsigned int& from a temporary of type int". Now I get really confused.
name is a reference to a pointer to unsigned int, so it will accept unsigned int* lvalues. For example,
unsigned int* p = nullptr;
asd(p);
The fact that is is being passed by reference means that the function asd can change the value of the pointer passed to it, and this change is seen by the caller.

equal_range and 2 overloads have no legal conversion for 'this' pointer

Here is this simple code
#include <map>
class MyMap : public std::multimap<int*, int*>
{
public:
void foo(const int* bar) const
{
equal_range(bar);
}
};
int main()
{
MyMap myMap;
int number;
myMap.foo(&number);
return 0;
}
It doesn't compile, and give the following error
error C2663: 'std::_Tree<_Traits>::equal_range' : 2 overloads have no legal conversion for 'this' pointer
I have seen many topic about this error, and it seems that it is a const issue. It compiles fine if I turn foo(const int* bar) into foo(int* bar).
Problem is, I don't see how foo content is supposed to change anything to my MyMap object. std::multimap proposes a const version of equal_range:
http://www.cplusplus.com/reference/map/multimap/equal_range/
What is my problem?
Thank you
Check the definition of equal_range:
pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
It expects a constant reference to key_type: const key_type& k.
What you were trying to supply was a pointer to a constant integer: const int* bar
Why doesn't this work even though both values are const?
A constant reference to an integer const int& foo means that you cannot let foo refer to another integer, but it is allowed to change the value of the referenced integer.
A pointer to a constant integer const int* foo means that you can let foo point to another integer, but you cannot change the value of the integer it points to.
What the map actually expects is a const int*& k, but the map will automatically convert this if you supply a int* only (without the const).
[Edit]
Also note that a MyMap object still cannot be changed by your foo function even if you change const int* to int* as there still is another const at the end of your foo function. This const at the very end declares the function as constant meaning that it cannot modify the current object in which it is executed. If it was trying to modify it or call anything that could potentially modify it, you would get a compiler error. (Disclaimer: There are ways to modify a class from within a const function anyway but that's another topic.)
If fact the right compiler message can give you the answer. For one of the two overloads (the const one):
/usr/local/include/c++/v1/map:1836:41: note: candidate function not viable: 1st argument ('const int *')
would lose const qualifier
const int* is a pointer to a const int. The method expects a const key_type&, i.e. const int*& argument, that is a const reference to a (non-const) int*.
When confused, prefer to write const int* as int const*, which is the same thing. Then you'll see the difference to const int*& more clearly.
For your code to work, use int* instead of const int*.
I believe that the problem has to do with the mismatch on the key_type.
On one hand you have a multimap where key_type=int* while on the other hand you are passing a key_type=const int* thus attempting to drop the const qualifier on the key_type. I was confused by this too because I was expanding the key_type in my mind to get const int*& which should be compatible. However, the mismatch happens earlier on the key_type itself. At least that's the only logical explanation I could think of.
My suggestion would be to make the key_type const int* and keep your function parameter as is. After all, why would you need a pointer to a mutable value as a key to a map ?

Why cant I pass a non const pointer to a function taking a reference to a pointer to a const as its argument

Here's a code snippet that hopefully conveys what I'm trying to do:
void updatePointer(const int*& i)
{
i++;
}
int main() {
int array[5];
int* arrayPtr = array;
updatePointer(arrayPtr );
return 0;
}
This gives compiler error:
prog.cpp: In function ‘int main()’:
prog.cpp:16: error: invalid initialization of reference of type ‘const int*&’ from
expression of type ‘int*’
prog.cpp:5: error: in passing argument 1 of ‘void updatePointer(const int*&)’
Supposing that you could do it, you could write the following:
const int c = 0;
void updatePointer(const int* &i) {
i = &c;
}
int main() {
int *ptr;
updatePointer(ptr);
*ptr = 1; // attempt to modify the const object c, undefined behavior
}
The purpose of const is to ensure that user code cannot attempt to modify a const object unless it contains a const-cast (or equivalent). So the compiler has to refuse this code. Forbidding a const int*& from binding to an int* is the only place in the code above that's reasonable for the compiler to refuse: every other line is fine.
It's the same reason you can't implicitly convert int** to const int **.
Aside from the motivation in terms of const-safety, you can think if it in terms of int* being a different type from const int*, that just so happens to be convertible to it. Likewise, you can convert int to double, but a double& can't bind to an int lvalue. That's not the full reason, because actually int* and const int* have the same size and representation, whereas int and double don't. So there could be a special-case to allow it if not for the fact that it would break the const system.
The reason that C++ has both const and non-const overloads for strchr is related to this issue: your function updatePointer modifies its input rather than returning the updated value, but the principle is similar. The C-style single strchr allows you to "launder" a pointer-to-const into a pointer-to-non-const without a cast, and it's a hole in the const system. C++ (a) has overloading and (b) has a stricter type system than C, so it closes that hole.
If you want your real function updatePointer to work like strchr -- examine the data pointed to and compute a new value for the pointer, then you're in the same situation that strchr is. That's regardless of what it does with the new value (return it in the case of strchr, write it back in the case of updatePointer), because the issue is that you want the new pointer to have the same const-qualification as the input. You need to provide either const- and non-const overloads or a function template.
If you only need your real function updatePointer to move a pointer by a certain distance, regardless of the data pointed to, you could use std::advance instead.
What you wrote is a function taking a reference to a pointer to a const int. What you're asking for would be
updatePointer(int* const & i);
However this doesn't make much sense. Passing a reference to a pointer seems to imply that you intend to modify the pointer, but you cannot do it because it is declared const. As it is you'd obtain the same effect by just passing your pointer as in
updatePointer(int* i);
Found this
Copied here in case the link breaks in future:
The reasoning is a little awkward to comes to grips with. The main question is:
Since a "const int&" can be bound to an "int", why can't a "const int*&" be bound to a "int*"?
Basically, once you add a level of indirection (a pointer) then the rules change. With just a single level of indirection (as in a single *), the rule can be stated as:
A reference to a pointer to a cv-qualified type can be bound to anything of that same type whose cv-qualifications are less than or equal to that of the pointer (which is a reference).
(Read that a few times.)
So the reason a "const int*&" can't be bound to a "int*" is because "const int*" and "int*" are two different types (underlined part of the rule is broken).

Function argument type followed by *&

I have some code written by someone else in which some functions take arguments whose data types are followed by a *&. I'm used to functions taking one or the other, e.g. taking a "double *" or a "double &" but not both. It would have thought they would just cancel out.
Here's an example, and this is all from their code which supposedly works:
In a header file there's a function declared as:
void someClass::foo(const unsigned int*& ubuff);
Then in my main file, there's a pointer to some UINTs declared and initialized as:
unsigned int* pbuff = new UINT[n];
Then, the function someClass::foo is called as:
foo(pbuff);
When I compile, I get the error "cannot convert parameter 1 from 'unsigned int *' to 'const unsigned int *&'". If I change my function call to the following, it compiles ok:
foo((const unsigned int *&)(pbuff));
So, my questions are:
What is the purpose of the *&? I.e., how is that different from * or & by themselves, and why do they not just cancel themselves out? And what does that make the function foo expect? An address? A value? The address of an address? It's confusing.
Is the fix of simply casting a "unsigned int*" to a "const unsigned int*&" ok or do I need to do something else?
Just another quick example, and this is from completely within a cpp file distributed by someone else, so I assume this compiled for them. They have a function call as
klabels = new int[sz];
EnforceLabelConnectivity(klabels, m_width, m_height, nlabels, numlabels, double(sz)/double(STEP*STEP));
When I try to build, I get an error "'SLIC::EnforceLabelConnectivity' : cannot convert parameter 1 from 'int *' to 'const int *&'".
Thanks as always.
For a type T, T *& means "a reference to a pointer to a T." The calling function should pass a pointer to a T. As to the const modifier, try foo((const unsigned int *)pBuff); or else declare pBuff to be a pointer to a const int to start with.
Well, I tried casting to a (const unsigned int *), and that did not work (wouldn't build). In the end, I had to fully cast to a (const unsigned int *&) in the foo function example in my original post. Hope this helps. Thanks.
As an aside, the reason I did not go the route of making pbuff const from the beginning in my main code is that it is passed to another function later on which does modify it, so I don't think it would work to have it made const when declared.
Pointers and references are really differents, but using a reference to a pointer is kind of useless.
This is not ok, pbuff is not const that's why your code doesn't compile. Use a const pbuff or change your prototype, casting like this is really ugly