Expand _In_ to const

458 views Asked by At

Except for the fact, that it would probably confuse every windows c++ developer out there,

wouldn't it be prudent to expand _In_ to a mandatory const (if not already present) to ensure const correctness?

So

int DoSomething( _In_ int * pInput);

becomes

int DoSomething( const int * pInput);

Obviously _[In]Out_ should still expand to nothing.


Edit: Obviously the first problem is that, this only makes sense when expanding in front of a pointer or a reference parameter.

So maybe a simpke macro expansion will not suffice. I don't want to abandon the notion of enforcing const just yet. The motto is: We already have a SAL notation that tells us what parameter is ro and what not, let's make some use of it.

2

There are 2 answers

0
Tony Delroy On

Assuming it's a macro expansion, that wouldn't work for values copied into parameters, i.e.

void f( _In_ X x) { g(++x); // ok to modify }
0
Sebastian Redl On

You should train your programmers to use const for pointer/reference parameters that are intended to be immutable, even if the SAL annotation is used.

Here are some problems with expanding _In_ to const:

  • Programmers who haven't seen SAL annotations before are extra confused.
  • These programmers will then write _In const int* and possibly get a compiler error, depending on whether the compiler diagnoses double const.
  • It's annoying for by-value parameters that the programmer wants to modify inside the function.
  • It's catastrophic for COM interface pointers. COM interfaces never have const members (because that's a very C++-specific thing). This means that a pointer to a const interface is absolutely useless. But you will still, quite often, pass _In_ or _In_opt_ parameters of interface type.