0

As I understood if (https://learn.microsoft.com/en-us/cpp/cpp/noalias?view=vs-2019) __declspec(noalias) means that the function only modifies memory inside her body or through the parameters, so its not modifying static variables, or memory throught double pointers, is that correct?


static int g = 3;

class Test
{
   int x;

  Test& __declspec(noalias) operator +(const int b) //is noalias correct?
  {
    x += b;
    return *this;
  }

  void __declspec(noalias) test2(int& x) { //correct here?
   x = 3;
  }

  void __declspec(noalias) test3(int** x) { //not correct here!?

   *x = 5;
  }
}

1 Answers1

0

Given something like:

extern int x;
extern int bar(void);

int foo(void)
{
  if (x)
    bar();
  return x;
}

a compiler that knows nothing about bar() would need to generate code that allows for the possibility that it might change the value of x, and would thus have to load the value of x both before and after the function call. While some systems use so-called "link time optimization" to defer code generation of a function until after any function it calls have been analyzed to see what external objects, if any, they might access, MS uses a simpler approach of simply allowing function prototypes to say that they don't access any outside objects which the calling code might want to cache. This is a crude approach, but allows compilers to reap low hanging fruit cheaply and easily.

supercat
  • 77,689
  • 9
  • 166
  • 211