0

Given the following code:

template <class Func>
void f(Func func , int* param){
   func(/* how can I send "param" as const "int*" */);
}

How can I do it so that if f don't get the variable as const - so we will get error ?

Software_t
  • 576
  • 1
  • 4
  • 13

1 Answers1

1

If you want to make sure that f accepts a pointer to const-qualified int you can cast function argument appropriately:

f(static_cast<int const *>(param));

Alternatively, if you want to make sure that f accepts a reference to const-qualified pointer you can add const qualifier to function argument:

void f(Func f , int * const param)
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • I thought about it too, but it's not doing the opposite thing? Namely, `static_cast` don't remove the "const" from param? – Software_t Jun 29 '18 at 09:19
  • 1
    @Software_t It is `const_cast` that removes `const` – user7860670 Jun 29 '18 at 09:20
  • I am sorry, you right. Can you explain if I can do it with `dynamic_cast` in this case or not (and why) ? – Software_t Jun 29 '18 at 09:27
  • @Software_t: Sure, that would degenerate to a `static_cast` there. Imho, they should have added `no_cast` when they added all the explicit casts, which is enough here. – Deduplicator Jun 29 '18 at 09:30