Static variable not maintaining it's value

I've been toying around with a multitude of topics including function templates, exceptions, making use of files, and in turn, using a static variable for a specific function. I've noticed that it doesn't maintain it's value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>

/*This function will take ofstream and ifstream
as arguments and see if the file is open*/

template<typename T>
bool is_openCheck(T& a)
{
     static int n = 0;
     n++;
     std::cout << "n is equal to " << n << std::endl;     /*This is to show that n isn't
                                                          maintaining its value throughout*/

     try
     {
          if (!a.is_open())
          {
               throw 1;
          }
     }
     catch(int x)
     {
          std::cout << "Error code " << x << ": File number " << n << " not found" << std::endl;
          std::cout << "Critical error, program will close" << std::endl;
          system("pause");
          return false;
     }

     return true;
}

int main()
{
     std::ofstream openFile("test.txt", std::ios::in);
     std::ofstream openFile2("test2.txt", std::ios::in);
     std::ifstream openFile3("test3.txt");


     if ( (is_openCheck<std::ofstream>(openFile)) == false)
     {
          return -1;
     }

     if ( (is_openCheck<std::ofstream>(openFile2)) == false)
     {
          return -1;
     }
     if ( (is_openCheck<std::ifstream>(openFile3)) == false)
     {
          return -1;
     }

     system("pause");
     return 0;
}


The console window will proceed to output:
n is equal to 1
n is equal to 2
n is equal to 1
Press any key to continue . . .


It works fine until up until the third line of output, where I'd expect it to say "n is equal to 3".

Does it re-initialize n because the argument being taken is of different type than the previous two times the function was called? And if not, then what is wrong, and is there a way to adjust the code in order for it to work according to my expectations?

Thank you in advance :)
The reason is that is_openCheck<std::ifstream>(...) and is_openCheck<std::ofstream>(...) are entirely different functions.

As if you write is_openCheck_std_ofstream(...) vs is_openCheck_std_ifstream(...)
I see. Thanks for your help. I'll make a local variable in main and pass it as an argument to the function so that it remains constant.
You could also try a functor class something like this if you wanted to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <string>

class OpenCheck
{
public:
    template<typename T>
    bool operator()(T& a);

private:
    int n = 0;
};

template<typename T>
bool OpenCheck::operator()(T& a)
{
     n++;
     std::cout << "n is equal to " << n << std::endl;     /*This is to show that n isn't
                                                          maintaining its value throughout*/
     try
     {
          if (!a.is_open())
          {
               throw 1;
          }
     }
     catch(int x)
     {
          std::cout << "Error code " << x << ": File number " << n << " not found" << std::endl;
          std::cout << "Critical error, program will close" << std::endl;
          return false;
     }

     return true;
}


int main()
{
     std::ofstream openFile("test.txt", std::ios::in);
     std::ofstream openFile2("test2.txt", std::ios::in);
     std::ifstream openFile3("test3.txt");

     OpenCheck is_openCheck;

     if ( (is_openCheck(openFile)) == false)
     {
          return -1;
     }

     if ( (is_openCheck(openFile2)) == false)
     {
          return -1;
     }
     if ( (is_openCheck(openFile3)) == false)
     {
          return -1;
     }

     return 0;
}
Registered users can post here. Sign in or register to post.