Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are 'const foo**' and 'foo** const' the same thing? [duplicate]

Tags:

c++

I know that for a type foo, the function declaration for bar

void bar(const foo*) and void bar(foo* const)

are the same thing. But that about double indirection? That is, are

void bar(const foo**) and void bar(foo** const)

also the same thing? If not, then what do I need to do to void bar(foo** const) to put the const first?

like image 385
P45 Imminent Avatar asked Aug 17 '26 13:08

P45 Imminent


1 Answers

Case 1

Here we consider

void bar(const foo*); #1
void bar(foo* const)  #2

In #1 we've a pointer to a const foo while in #2 we've a const pointer to a nonconst foo.

You could use std::is_same to confirm that they're different:

std::cout << std::is_same<const foo*, foo* const>::value << ' ';   //false

Case 2

Here we consider

void bar(const foo**); #3
void bar(foo** const); #4

In #3 we've a pointer to a pointer to a const foo while in #4 we've a const pointer to a nonconst pointer to a nonconst foo.

std::cout << std::is_same<const foo**, foo** const>::value << ' '; //false

Moreover, note that const foo * is the same as foo const * but this case you don't have in your given examples.

std::cout << std::is_same<const foo *, foo const *>::value << ' '; //true
like image 74
Anoop Rana Avatar answered Aug 19 '26 03:08

Anoop Rana