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 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

Related Questions

I was wondering whether or not I can extend the Enum type in C#
I was wondering, why can't there be a void data type that is not
I can't find the type of problem I have and I was wondering if
I'm really blanking out here. I'm wondering why I can't declare a delegate type
I am wondering why scala can't infer the type of method parameters.I can see
Two function should be implemented, and I am wondering what type of validation I
3I was just wondering if this type of query is even possible. If so,
I was just wondering if a storage engine type existed that allowed you to
I was wondering if anyone knows how this type of layout works http://500px.com/ Or
I am wondering how I could create a custom data type to use within

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.