This question already has answers here:
Cannot convert from XXX** to const XXX** [duplicate]
(3 answers)
How to convert "pointer to pointer type" to const?
(2 answers)
Closed 4 years ago.
E.g. the following will fail to compile:
auto foo = [](const char**) {};
char* ptr = nullptr;
foo(&ptr);
Visual studio says that there's a conversion that loses qualifiers. Clang says cannot initialize a parameter of type 'const char **' with an rvalue of type 'char **". Perhaps my brain just isn't working today, but why is this dis-allowed? char* can implicitly convert to const char* after all
Oh wait, yes my brain isn't working today. foo can assign a const char* to the pointer, which is clearly bad.
Related
This question already has answers here:
Why isn't it legal to convert "pointer to pointer to non-const" to a "pointer to pointer to const"
(5 answers)
Closed 7 years ago.
I know that const char * p means we can't change the value which is pointed by p through p.
Now I'm writing a function which will take the parameters of the function main. Meaning that this function will take a char **. So I write this function like this:
void func(const char **);.
But when I pass the parameter of main to it, I get an error:
error C2664: 'void func(const char **)' : cannot convert argument 1 from 'char **' to 'const char **'
I just want to initialize a const pointer with a non-const pointer. This should work. If we do the opposite thing, we should get some error. But now I don't know why I get this error.
I just want to initialize a const pointer with a non-const pointer. This should be work.
That's not what you're trying to do, no.
You're trying to initialise a non-const pointer to pointer to const char, with a non-const pointer to pointer to char. That is never performed implicitly. Why? It's well documented elsewhere because it's not completely intuitive to all but, in short, if converting char** to const char** were allowed, then const-correctness would be violated.
It's not the same as converting char** to char** const, which is what you think you're trying to do.
It's unfortunate that main's arguments do not already have const strewn about them. Sorry. Fortunately, you can work around it with a const_cast:
void foo(const char** argv) {}
int main(int argc, char** argv)
{
foo(const_cast<const char**>(argv));
}
This is one of those situations in which using const_cast to hack around pre-existing silliness, contrary to the wishes of the type system, is actually okay.
This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 7 years ago.
How do I initialize a c-string in C++11?
I tried:
char* foo = {"bar"};
but I get:
ISO C++11 does not allow conversion from string literal to 'char *
Because it has to be pointer to const:
const char* foo = "bar";
Before C++11, you could make foo simply char*, even though that was deprecated since C++03. But C++11 removes the deprecation and simply makes it illegal.
The correct way to do it is;
char const* foo = {"bar"};
// ^^^^^ added const
The older C style (non const) was deprecated and now is removed from C++11.
This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
Closed 7 years ago.
I understand that the const keyword means that you can't change a variable's value so what does it mean if it used like this :
const char* const& message
The first const stays for the pointer reference itself cannot be changed, so you cannot do something like:
message = &something;
the second const is for the content of the pointer, so you cannot do something like:
*message = something;
This question already has answers here:
constness and pointers to pointers
(4 answers)
non-const pointer argument to a const double pointer parameter
(1 answer)
Closed 8 years ago.
I want to pass argv to another function, and can do it with no problems when I define the function like this:
void function(char** argv);
and call it from main with:
function(argv);
However, I would like to keep everything const where possible (I don't actually plan to change argv, or the value of either of the pointers). My problem is that as soon as I add the keyword const anywhere to argv in the function declaration I get conversion errors, e.g. this code
void function(const char** argv);
gives the compile error:
error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]
I've tried putting const in different places and get similar errors. Is there a way to pass argv while keeping the contents and pointers all constant?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const)
I have a function:
bool isCirclePolygonIntersection(const Point*, const int*, const Point*,
const Point**, const int*);
and I'm trying to call it like this:
isCirclePolygonIntersection(p, &r, poly_coord, poly, &poly_size)
where poly defined like this:
Point** poly = new Point*[poly_size];
and there is a compiler error when I'm trying to compile it:
error C2664: 'isCirclePolygonIntersection' : cannot convert parameter 4 from 'Point **' to 'const Point **'
1> Conversion loses qualifiers
from what I've learned is that you cannot give const argument to function when a function expects a non const argument, but it's fine otherwise.
Does anyone knows what is the problem??
Thanks.
You're correct; you can implicitly convert a T * to a const T *. However, you cannot implicitly convert a T ** to a const T **. See this from the C FAQ: http://c-faq.com/ansi/constmismatch.html.
An implicit conversion from Point** to const Point** would open a hole in the type system, hence there is no such conversion in C++.
For details, see Why am I getting an error converting a Foo** → Foo const**?
Changing const Point** to const Point * const * will fix your problem.
By the way, why the additional indirection? That is, why do you use an array of pointers? I would suggest a std::vector<Point> instead. Or is Point a polymorphic base class?
Parashift has a great explanation on why this is prohibited:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17