title: Functions That Will Only Accept Certain Argument Values (C++)
body: <p><em>Let me set the scene..</em>  </p>
  
  <p>You can open files in a specific mode like this:</p>
  
  <pre><code>#include &lt;fstream&gt;
  
  int main(){
  
      std::fstream myfile;
      myfile.open ("filename", std::ios::app);
  
      return 0;
  }
  </code></pre>
  
  <p>that second parameter is an enumerated type-<br>
  which is why you will get a compiler error attempting this:</p>
  
  <pre><code>#include &lt;fstream&gt;
  
  int main(){
  
      std::fstream myfile;
      myfile.open ("filename", std::ios::lksdjflskdjflksff);
  
      return 0;
  }
  </code></pre>
  
  <p><em>In this example, the class doesn't have to account for the second parameter being incorrect, and the programmer never has to worry about passing in a nonsensical value.</em></p>
  
  <p><strong>Question:</strong> <em>Is there a way to write functions that must take a particular type <strong>AND</strong> a particular value?</em>  </p>
  
  <p>Let's say I wanted to re-implement a File Handling class similar to the one above.
  The difference is I'm making the second parameter a char instead of an enumerated type.<br>
  How could I get something like this to work:</p>
  
  <pre><code>#include "MyFileHandler.h"
  
  int main(){
  
      MyFileHandler myfile1;
  
      myfile.open ("filename", 'a'); //GOOD: a stands for append
      myfile.open ("filename", 't'); //GOOD: t stands for truncate
      myfile.open ("filename", 'x'); //COMPILER ERROR: openmode can not be the value 'x'
  
      return 0;
  }
  </code></pre>
  
  <p>Going beyond this, can I get the compiler to test the validity of argument values through functional means?
  Example:</p>
  
  <pre><code>void IOnlyAcceptPrimeNumbers(const int &amp; primeNumber);
  int function(void);
  
  int main(){
  
      IOnlyAcceptPrimeNumbers(3);       //GOOD: 3 is prime
      IOnlyAcceptPrimeNumbers(7);       //GOOD: 7 is prime
      IOnlyAcceptPrimeNumbers(10);      //COMPILER ERROR: 10 is not prime
      IOnlyAcceptPrimeNumbers(10+1);    //GOOD: 11 is prime
      IOnlyAcceptPrimeNumbers(1+1+1+1); //COMPILER ERROR: 4 is not prime
      IOnlyAcceptPrimeNumbers(function()); //GOOD: can this somehow be done?
  
  
      return 0;
  }
  void IOnlyAcceptPrimeNumbers(const int &amp; primeNumber){return;}
  int function(void){return 7;}
  </code></pre>
  
  <p>I believe i've made it clear what I want to do and why I find it important.<br>
  Any solutions out there?</p>