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
Related
How to pass std::vector<void*> * as std::vector<MyStruct*>* to a function?
The function declaration is void SetDataReferences(std::vector<MyStruct*>* pVector);
if I pass std::vector<void*> * as std::vector<MyStruct*>* I am getting below error
error C2664: 'CTagFilterComboBox::SetDataReferences' : cannot convert parameter 2 from 'std::vector<_Ty> *' to 'std::vector<_Ty> *'
How to resolve this?
Unfortunately std::vector<void*> is a completely different type to std::vector<MyStruct*>*, so no cast is going to work.
You need to rebuild the vector from scratch, and pass a pointer to that to your function, and hope that the static_cast on each element is valid.
One way, given a std::vector<void*>* foo;
std::vector<MyStruct*> temp;
for (void* p : *foo){
temp.push_back(static_cast<MyStruct*>(p));
}
If you don't need to modify the vector<void*> foo then you could just work with the underlying data, by changing your function signature to: void SetDataReferences(MyStruct*const* pVector, const size_t length) (remember that const is left associative, so this is a pointer-to-constant-pointers-to-MyStructs.) This can be called as follows:
SetDataReferences(reinterpret_cast<MyStruct*const*>(data(foo)), size(foo))
If you needed to modify the elements of vector<void*> foo (not the size) you still have the recourse of changing your function signature to: void SetDataReferences(MyStruct** pVector, const size_t length) This can be called as follows:
SetDataReferences(reinterpret_cast<int**>(data(foo)), size(foo))
Live Examples
If you need to modify vector<void*> foo that gets more complicated, you'll need to accept the vector<void*> as your function signature does in the question and cast the elements on use rather than as a group.
I got solution for my problem from this link
https://www.codeproject.com/Questions/1261243/How-to-pass-std-vector-void-as-std-vector-mystruct and it is working for me.
If I call SetDataReferences((std::vector<MyStruct*>*)(&p1)) it is working for me without getting any compiler error.
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.
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).
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.
I have a function that receives float** as an argument, and I tried to change it to take const float**.
The compiler (g++) didn't like it and issued :
invalid conversion from ‘float**’ to ‘const float**’
this makes no sense to me, I know (and verified) that I can pass char* to a function that takes const char*, so why not with const float**?
See Why am I getting an error converting a Foo** → const Foo**?
Because converting Foo** → const Foo** would be invalid and dangerous ... The reason the conversion from Foo** → const Foo** is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast
The reference goes on to give an example of how such an implicit conversion could allow me one to modify a const object without a cast.
This is a very tricky restriction. It is related to the aliasing rules of the language. Take a look at what the standards say, because I have faced this once before:
(Page 61)
[Note: if a program could assign a
pointer of type T** to a pointer of
type const T** (that is, if line //1
below was allowed), a program could
inadvertently modify a const object
(as it is done on line //2). For
example,
int main() {
const char c = 'c';
char* pc;
const char** pcc = &pc; //1: not allowed
*pcc = &c;
*pc = 'C'; //2: modifies a const object
}
—end note]
Other anwers have detailled why this is an error in C++.
Let me address the question behind your question. You wanted to state, in the interface of your function, that your function will not modify float values contained in the array. Nice intention, and enables that your function is called with const float ** arrays. The question behind your question would be, how to achieve this without resolving to ugly casts.
The correct way to achieve what you wanted is to change the type of your function parameter to const float * const *.
The additional const between the stars assures the compiler that your method will not try to store pointers to const float in the array, since this type declares that the pointer values are also const.
You can now call this function with float ** (which was the example in your question), const float **, and const float * const * arguments.
If you converted the parameter to const float** you could then store a const float* at the memory location where the parameter points to. But the calling function thinks that this memory location is supposed to contain a non-const float* and might later try to change this pointed-to float.
Therefore you cannot cast a float** to a const float**, it would allow you to store pointers to constants in locations where pointers to mutable values are expected.
For more details see the C++ FAQ Lite.