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 518127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:54:19+00:00 2026-05-13T07:54:19+00:00

I am wondering if type can be determined as runtime information in C++. (1)

  • 0

I am wondering if type can be determined as runtime information in C++.

(1) Although my question is quite general, for simplicity, I will start from a simple example:

 #include <stdio.h>  
 #include <iostream>  
 #include <cstring>  
 using namespace std;  
 int main(int argc, char * argv[])  
 {  
 if (strcmp(argv[1], "int")==0)   
 {  
     int t = 2;   
 }else if (strcmp(argv[1], "float")==0)  
 {  
     float t = 2.2; 
 } 
 cout << t << endl;  // error: ‘t’ was not declared in this scope
 return 0;  
 }

For this example, there are two questions:

(a) “argv[1] to t” is wrong, but can the type info in the C string argv[1] be converted to the actual type keyword? So we don’t need to check for every type by if-else clause and strcmp.

(b) how to make variable t defined inside the local scope of the if clause still valid outside. i.e how to “export” a local variable to the outside of its scope?

(2) Generally speaking, not specific to the simple example above, what are the usual ways to runtime determine types? It seems to me that there might be some ways:

(a) one can put the processing of the variable defined from the type inside the same scope its definition. e.g.

 #include <stdio.h>  
 #include <iostream>  
 #include <cstring>  
 using namespace std;  
 int main(int argc, char * argv[])  
 {  
 if (strcmp(argv[1], "int")==0)   
 {  
     int t = 2;   
     cout << t << endl; 
 }else if (strcmp(argv[1], "float")==0)  
 {  
     float t = 2.2; 
     cout << t << endl; 
 } 
 return 0;  
 }

And possibly use template function to make the common code for various types reusable.

(b) or one may use abstract class type and polymorphism to indirectly export the definition out, but I am not sure how exactly.

Thanks for your advice!

  • 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-13T07:54:19+00:00Added an answer on May 13, 2026 at 7:54 am

    1a: No, types are not objects or values in C++ (as they are, for example, in Python). You can, however, use various values selected by the value of argv[1].

    1b: Sorry, just can’t do that.

    2: dynamic_cast and typeid (both operators) are the only tools currently provided by the language to query type (not unusual, most languages have very few, but dedicated, tools for that), and using them solely to query type is often discouraged depending on situation (also not unusual among other languages).

    2a: Yes, and as that is the simple, obvious, and works here—there’s no reason to use anything else, but as it’s example code, let’s assume you need a different solution. You could call a function template instantiated on the right type, but as this is pretty much the same thing as the rest of 2a, I don’t go into it.

    2b: Example using a subclass template, just because it’s handy:

    struct Base {
      virtual ~Base() {}
      friend std::ostream& operator<<(std::ostream& s, Base const& v) {
        v._print(s);
        return s;
      }
    private:
      virtual void _print(std::ostream&) const = 0;
    };
    
    template<class T>
    struct Value : Base {
      T data;
      explicit
      Value(T const& data) : data(data) {}
    private:
      virtual void _print(std::ostream& s) const {
        s << data;
      }
    };
    

    Use:

    int main(int argc, char** argv) {
      using namespace std;
      auto_ptr<Base> p;
      string const type = argc > 1 ? argv[1] : "int";
      if (type == "int") {
        p.reset(new Value<int>(2));
      }
      else if (type == "float") {
        p.reset(new Value<double>(2.2));
      }
      cout << *p << '\n';
      return 0;
    }
    

    This is starting to merge the two types into one type, and they both present the same interface, Base, here. However, this doesn’t lend itself well to every solution, and a variant such as boost.variant can be better, particularly when the various types required are small in number and known well in advance.

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

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's a built in tag in cucumber: @wip (for Work… May 13, 2026 at 1:35 pm
  • Editorial Team
    Editorial Team added an answer Your program has a main with one call like this… May 13, 2026 at 1:35 pm
  • Editorial Team
    Editorial Team added an answer The global ignore list does not use paths. For this,… May 13, 2026 at 1:35 pm

Related Questions

I recently ran against a very interesting site that expresses a very interesting idea
As I understand it, once I've set up an RMI communications link between two
I was chatting with Sadek Drobi on twitter when be brought up that F#
thanks in advance for your help. I am wondering if there is a (design)
I am trying to do an ASP.net custom control for the Flow Player flv

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.