0

I have:

const char  *ptr = "int";

If I have to declare using ptr:

int a;

ptr can point to anything arbitrary char, string, <anythihg>. I have to delcare a variable of that type what ptr is pointing at. Is it possible?

codedoc
  • 2,079
  • 2
  • 11
  • 15
  • 1
    You really should read up on C++. The fact that you're not using `std::string` suggests that you're unfamiliar with basic C++. – MSalters Jun 08 '15 at 08:42
  • Are you simply talking about a `typedef`? (i.e. `typedef ptr int;` then `ptr a = 5` (or whatever value)? – David C. Rankin Jun 08 '15 at 08:47
  • See this question: http://stackoverflow.com/questions/1096700/instantiate-class-from-name – samgak Jun 08 '15 at 08:48
  • 1
    What do you want to use this newly created variable `a` for? I mean, even something as silly as `if (!strcmp(ptr, "int")) int a; else if (!strcmp(ptr, "char")) char a;` can't work in C or C++. – Mr Lister Jun 08 '15 at 08:57
  • @DavidC.Rankin while you're very right, that kind of statement is confusing at it's best. Just saying. :-) – Sourav Ghosh Jun 08 '15 at 09:04
  • Thank you for the responses. If I change the scenario as following: enum typedef UserType { int, float, double } UserType; and I have a value, say 1, is it possible to declare "float var;" as the value I have is 1? – codedoc Jun 08 '15 at 09:52
  • @codedoc If int, float and double values is all you're concerned about, just use double for a. doubles can contain all kinds of values, even all int values, without loss of precision. – Mr Lister Jun 08 '15 at 10:42
  • @MrLister: Formally, there's no guarantee - a 64 bits `int` will have values that cannot be represented by a 64 bits double`. But most systems have 32 bits ints. – MSalters Jun 08 '15 at 10:54

2 Answers2

3

No. C++ is not an interpreted language. "int" has a meaning to the compiler, but at runtime there's nothing which understands "int".

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Technically speaking, there's nothing preventing C++ from being interpreted (there are actually C++ interpreters in existence), and even if there were, this question doesn't seem to have any relevance to the method of translation anyway. – autistic Jun 08 '15 at 08:49
  • While C++ allows interpretation (trivial - any langauge does), C++ has no constructs which expose such an interpreter. That's why I'm saying it's not an interpreted _language_, even if there are interpreting _implementations_. Javascript (the language) for comparison has `eval` which makes it an interpreted language, even if some Javascript implementations use compilation techniques. – MSalters Jun 08 '15 at 08:53
  • Fair enough. I understood what you're saying before you felt the need to elaborate. I fear you've missed what I was saying, however. Note the emphasis here: C++ would require interpretation to implement dynamic typing, however *an answer to this question needn't involve dynamic typing*. Type inference (*which C++ does have*) could just as easily be used (and in fact, *should* be used)... – autistic Jun 08 '15 at 09:01
  • @undefinedbehaviour: I don't see how type inference is going to synthesize `std::map>`. The set of types in a C++ program is finite. – MSalters Jun 08 '15 at 09:03
  • The question is asking how to declare a variable of the same type that `ptr` points at... How is `std::map<...>` relevant? – autistic Jun 08 '15 at 09:07
  • ... and at that, the type that `ptr` points at is also a finite set (comprising of 1 element)... thus making type inference a suitable option. – autistic Jun 08 '15 at 09:08
  • @undefinedbehaviour: You seem to be missing the point: `const char* ptr = "std::map";`. It's not what type `ptr` has - after all, in the example given it doesn't have type `int`. – MSalters Jun 08 '15 at 09:13
  • If I change the scenario as following: enum typedef UserType { int, float, double } UserType; and I have a value, say 1, is it possible to declare "float var;" as the value I have is 1? – codedoc Jun 08 '15 at 10:10
  • @codedoc: Not precisely like that, but it's a much more realistic scenario. Check the Boost library, in particular boost::variant. – MSalters Jun 08 '15 at 10:53
2

It is advisable that you choose a language based on the way you'd like to express yourself.

What you have described is not a feature in standard C, and any extensions that you have to rely upon will lock you into a particular vendor, in which case it they might decide at some point to stop supporting it... What a mess that would be, right?

In the realm of C++, you might find helpful templates, decltype, the auto keyword, or potentially other options?

autistic
  • 1
  • 3
  • 35
  • 80