cannot pass 'const pointer const' to const ref


Thomas B.

Suppose you have a set of pointers (yes...):

std::set<SomeType*> myTypeContainer;

Then suppose you want to search this collection from a const method of SomeType:

bool SomeType::IsContainered() const
{
    return myTypeContainer.find(this) != myTypeContainer.end();
}

This doesn't work. The ptr in the method is a , thiswhich const SomeType *constI can't type find. The problem is findthat a const-ref is required, which in this case means treating the passed pointer as const, not the object it points to.

Is there a way to fix this (without changing the set template type)?

songyuanyao

As you said, a const member function cannot be implicitly converted to (i.e. a pointer to non-constthis ) when it becomes (i.e. a pointer to const ), which is the expected parameter type .const SomeType *SomeType *find

which you can use to perform explicit conversions.const_cast

bool SomeType::IsContainered() const
{
    return myTypeContainer.find(const_cast<SomeType *>(this)) != myTypeContainer.end();
}

It will be safe to modify without using the conversion result. instead of doing that.std::set::find

Related


cannot pass 'const pointer const' to const ref

Thomas B. Suppose you have a set of pointers (yes...): std::set<SomeType*> myTypeContainer; Then suppose you want to search this collection from a const method of SomeType: bool SomeType::IsContainered() const { return myTypeContainer.find(this) != myType

cannot pass 'const pointer const' to const ref

Thomas B. Suppose you have a set of pointers (yes...): std::set<SomeType*> myTypeContainer; Then suppose you want to search this collection from a const method of SomeType: bool SomeType::IsContainered() const { return myTypeContainer.find(this) != myType

Pass pointer to const ref function

Dean Why does the following work? #include <iostream> using namespace std; class PolyLine { public: PolyLine() = default; PolyLine(PolyLine * ptr) { std::cout << "Ctor called" << std::endl; } }; void function(const PolyLine& pt) { } int main() {

Pass pointer by value or const ref?

Barry In short, is it better to pass pointers by value or const reference? I wrote a simple function to explore: void foo(int* v) { std::cout << *v << endl; } and void foo(int* const& v) { std::cout << *v << endl; } I tried looking at the generated assembly,

Pass pointer by const reference

csguy Isn't passing pointers by const reference for optimization? predecessor. bool testHelper(const TreeNode*& p, const TreeNode*& q) { return false; } TreeNode* test(TreeNode* root, TreeNode* p, TreeNode* q) { recursiveHelper(p, q); } mistake: Line

pass a const pointer

catch_0x16 When using 3D/2D graphics libraries, I often find myself passing a pointer to the render window to each drawable class that draws itself onto the canvas. However, every time I do this, I worry that by passing a pointer to the render window, the obje

pointer to const (or pointer to const)

Wengers I have the following code: #include <iostream> int main(){ int v1 = 20; int *p1 = &v1; int **p2 = &p1; return 0; } What I'm trying to do here is point one pointer to another and it works fine in this case. I make p1 point to a cons

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

C++ pass by const pointer or by reference to const pointer

Taoist group I'm learning c++ and recently I read a book that recommends that you use references to const when possible (if the underlying object doesn't change). I have a question, you should pass a reference to a const pointer instead of a const pointer if p

C++ pass by const pointer or by reference to const pointer

Taoist group I'm learning c++ and recently I read a book that recommends that you use references to const when possible (if the underlying object doesn't change). I have a question, you should pass a reference to a const pointer instead of a const pointer if p

C++ pass by const pointer or by reference to const pointer

Taoist group I'm learning c++ and recently I read a book that recommends that you use references to const when possible (if the underlying object doesn't change). I have a question, you should pass a reference to a const pointer instead of a const pointer if p

c syntax to pass const pointer to const data to work

Ultrasonic Banana In the function declaration below the second parameter, is a const pointer to const data. ti_rc_t ti_uart_write_buffer(const ti_uart_t uart, const uint8_t *const data, uint32_t len); Below is sample code to call the function. Why is there on

Initialize const struct with const pointer

bomb I want to form a struct from a const parameter passed to a function. Since the parameter is const, I guess the struct must also be const. However, it doesn't work with pointers. Compile the following code (MinGW 4.9.2 32bit) struct structType_t { int

const reference vs pointer to const

michalt38 Why with a pointer to const I can change the value of the variable pointed to by the pointer, but not with a const reference? int a = 10; int * const ptr = &a; (*ptr)++; // OK const int & ref = a; ref++; // error Elias In your first example, you hav

Pass pointer to const int in recursive call

Neeraj Bansal How to pass pointers const intin recursive calls . I'm calculating Fibonacci recursively using the following code format, but I'm getting an error: error: lvalue must be unary '&' operand** #include <iostream> void fun(const int *n) { fu

Pass pointer to const int in recursive call

Neeraj Bansal How to pass pointers const intin recursive calls . I'm calculating Fibonacci recursively using the following code format, but I'm getting an error: error: lvalue must be unary '&' operand** #include <iostream> void fun(const int *n) { fu

Pass pointer to const int in recursive call

Neeraj Bansal How to pass pointers const intin recursive calls . I'm calculating Fibonacci recursively using the following code format, but I'm getting an error: error: lvalue must be unary '&' operand** #include <iostream> void fun(const int *n) { fu

Pass pointer to const int in recursive call

Neeraj Bansal How to pass pointers const intin recursive calls . I'm calculating Fibonacci recursively using the following code format, but I'm getting an error: error: lvalue must be unary '&' operand** #include <iostream> void fun(const int *n) { fu

Convert "pointer to const" to "pointer to const VLA"

Diapier In this code snippet, the large lookup table is easier to access using a pointer to the VLA : #pragma GCC diagnostic warning "-Wcast-qual" char lookup(int a, int b, int c, char const *raw, int x, int y, int z) { typedef char const (*DATA_PTR)[a][b

Convert "pointer to const" to "pointer to const VLA"

Diapier In this code snippet, the large lookup table is easier to access using a pointer to the VLA : #pragma GCC diagnostic warning "-Wcast-qual" char lookup(int a, int b, int c, char const *raw, int x, int y, int z) { typedef char const (*DATA_PTR)[a][b