Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:21:49+00:00 2026-05-30T01:21:49+00:00

Let me set the scene.. You can open files in a specific mode like

  • 0

Let me set the scene..

You can open files in a specific mode like this:

#include <fstream>

int main(){

    std::fstream myfile;
    myfile.open ("filename", std::ios::app);

    return 0;
}

that second parameter is an enumerated type-
which is why you will get a compiler error attempting this:

#include <fstream>

int main(){

    std::fstream myfile;
    myfile.open ("filename", std::ios::lksdjflskdjflksff);

    return 0;
}

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.

Question: Is there a way to write functions that must take a particular type AND a particular value?

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.
How could I get something like this to work:

#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;
}

Going beyond this, can I get the compiler to test the validity of argument values through functional means?
Example:

void IOnlyAcceptPrimeNumbers(const int & 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 & primeNumber){return;}
int function(void){return 7;}

I believe i’ve made it clear what I want to do and why I find it important.
Any solutions out there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:21:52+00:00Added an answer on May 30, 2026 at 1:21 am

    If you want compile-time checked values, you could write templates rather than function arguments:

    template <char> void foo(std::string const &);      // no implementation
    
    template <> void foo<'a'>(std::string const & s) { /* ... */ } 
    template <> void foo<'b'>(std::string const & s) { /* ... */ }
    

    Usage:

    foo<'a'>("hello world");   // OK
    foo<'z'>("dlrow olleh");   // Linker error, `foo<'z'>` not defined.
    

    If you want an actual compiler error rather than just a linker error, you could add a static_assert(false) into the primary template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is maybe unusual so let me set the scene: We have an SVN
This is quite an interesting question so let me set the scene. I work
This is my quandary. Let me just set the scene: I have a grid
Let me set the scene: I'm a PHP developer that needs to take info
Firstly, let me set out what I'd like to do. Assume I have three
Let say I would like to set nested style for development and compressed for
Let's set: i = 1; condition = i < 2; // Users makes this
I have an ImageView on my scene that I would like to set the
Let me set the stage here. I'm a very junior developer who's recently made
I'm creating a object with JDO and let the GAE set the Key for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.