When is the address of a global variable passed as a template argument?

I recently watched a video from Microsoft explaining some of the new features of C ++ 17, and I was intrigued to find a C ++ 14 function (and maybe even before that?) That allows you to use the address of a global variable as a constant expression for template argument.

This allows you to use the following code:

#include <iostream>
int g_iTest = 5;

template <int* Addr>
struct S {
    static int TestAdd( int iTest ) {
         *Addr = iTest + *Addr;
         return *Addr;
    }
};

int main() {
    S<&g_iTest> s;
    std::cout << s.TestAdd( 5 ) << std::endl;
}

10. , , , , . , , extern int g_iTest, ?

?

+4
2

, ++ 98.

, .

. , . , extern, , , ++ 11 ( ).

0

.

.

  • static.
  • extern, .
  • static, .
  • / ( , ).
  • template.

, . . ( , : , , ..).

, 5 .

4 , . API. , , , ( , ).

1 2 3 , , - , .

5 , ( ). .

template, . API, API , , API.

0

All Articles