3

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?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • In the second case, they are different. `const foo**` is a pointer to a pointer to a const `foo` while `foo** const` is a const pointer to a pointer to a nonconst `foo`. OTOH `const foo*` is a pointer to a const `foo` while `foo* const` is a const pointer to a nonconst `foo`. – Jason Jun 24 '22 at 12:51
  • @AnoopRana: Sorry but I had a typo in the first one. – P45 Imminent Jun 24 '22 at 12:52
  • 1
    `const foo *` is the same as `foo const *`, but neither is the same as `foo * const`. – franji1 Jun 24 '22 at 12:56
  • C++ FAQ [Why am I getting an error converting a Foo** → const Foo**?](https://isocpp.org/wiki/faq/const-correctness#constptrptr-conversion) – Eljay Jun 24 '22 at 13:03
  • These *declarations* are the same thing: `void bar(foo*);` and `void bar(foo* const);` The `const` doesn't affect the signature. – Eljay Jun 24 '22 at 13:05

1 Answers1

3

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
Jason
  • 36,170
  • 5
  • 26
  • 60