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

  • SEARCH
  • Home
  • 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 7933753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:25:20+00:00 2026-06-03T21:25:20+00:00

I was working with some old code (a text game, as it happens) and

  • 0

I was working with some old code (a text game, as it happens) and wanted to replace the pattern

strcasecmp(variable, "something") == 0 || strcasecmp(variable, "something else") == 0

with something nicer, like

in_list(variable, "something", "something else")

I thought a variadic function would be appropriate. But when I looked at the manpage I saw that there’s no way to tell when you’ve run out of arguments (calling va_arg when you have results in undefined behavior). So how can I handle this?

Maybe there’s some way to get around this limitation. Maybe I can #define some kind of sentinel at the end of the list so I can check for that, though it seems inelegant. I suppose I could just replace it with a family of macros with 1, 2, … arguments up to some reasonable limit, though this feels like a hack.

What’s the right way to do this? Suppose I’m not willing to rewrite the program to use the string type and that I’m stuck with char*s.

  • 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-06-03T21:25:23+00:00Added an answer on June 3, 2026 at 9:25 pm

    Assuming you can use C++11 features, I’d have the function take a collection type (e.g., std::set) that supports initializer lists, so you could use something like:

    in_list(variable, {"something", "something else", "yet a third thing"});
    

    Edit: Here’s a quick demo:

    #include <string>
    #include <set>
    #include <iostream>
    
    bool in_list(std::string const &value, std::set<std::string> const &list) {
        return list.find(value) != list.end();
    }
    
    int main(){
        std::cout << std::boolalpha << in_list("true", {"this", "is", "a", "true", "statement"}) << "\n";
    
        std::cout << in_list("false", {"this", "is", "a", "true", "statement"});
        return 0;
    }
    

    This compiles cleanly with g++ 4.7.0, and produces the expected output:

    true
    false
    

    And yes, absent a reason to do otherwise, std::set would be a reasonable choice for the job at hand. As far as your concern over char * vs. std::string goes: std::string supports implicit conversion from char *, so you can pass a char * to the function (as I’ve done above) and it’ll be converted to std::string automatically. In other words, (most) other code can just pass char *, and not worry about the minor detail that this code views it as std::string.

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

Sidebar

Related Questions

I am working with some old code and it defines a global variable like
The project that I've been working on involves porting some old code. Right now
I'm working on bringing some old code from 1998 up to the 21st century.
I'm working with some old PHP code that has a lot of the following:
I am working on editing some old C++ code that uses global arrays defined
I'm currently working on some quite old C++ code and often find things like
I am working on some OLD (as in older than me) C code that
Trying to update some old code from live() to use on() and something is
I'm working with some old code (and frankly I don't know half of what
I'm working on some old AJAX code, written in the dark dark days before

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.