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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:47:08+00:00 2026-05-14T08:47:08+00:00

Introduction I’m relatively new to C++. I went through all the basic stuff and

  • 0

“Introduction”

I’m relatively new to C++. I went through all the basic stuff and managed to build 2-3 simple interpreters for my programming languages.

The first thing that gave and still gives me a headache: Implementing the type system of my language in C++

Think of that: Ruby, Python, PHP and Co. have a lot of built-in types which obviously are implemented in C.
So what I first tried was to make it possible to give a value in my language three possible types: Int, String and Nil.

I came up with this:

enum ValueType
{
     Int, String, Nil
};

class Value
{
 public:
  ValueType type;
  int intVal;
  string stringVal;
};

Yeah, wow, I know. It was extremely slow to pass this class around as the string allocator had to be called all the time.

Next time I’ve tried something similar to this:

enum ValueType
{
     Int, String, Nil
};

extern string stringTable[255];
class Value
{
 public:
  ValueType type;
  int index;
};

I would store all strings in stringTable and write their position to index. If the type of Value was Int, I just stored the integer in index, it wouldn’t make sense at all using an int index to access another int, or?

Anyways, the above gave me a headache too. After some time, accessing the string from the table here, referencing it there and copying it over there grew over my head – I lost control. I had to put the interpreter draft down.

Now: Okay, so C and C++ are statically typed.

  • How do the main implementations of the languages mentioned above handle the different types in their programs (fixnums, bignums, nums, strings, arrays, resources,…)?

  • What should I do to get maximum speed with many different available types?

  • How do the solutions compare to my simplified versions above?

  • 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-14T08:47:08+00:00Added an answer on May 14, 2026 at 8:47 am

    There are a couple of different things that you can do here. Different solutions have come up in time, and most of them require dynamic allocation of the actual datum (boost::variant can avoid using dynamically allocated memory for small objects –thanks @MSalters).

    Pure C approach:

    Store type information and a void pointer to memory that has to be interpreted according to the type information (usually an enum):

    enum type_t {
       integer,
       string,
       null
    };
    typedef struct variable {
       type_t type;
       void * datum;
    } variable_t;
    void init_int_variable( variable_t * var, int value )
    {
       var->type = integer;
       var->datum = malloc( sizeof(int) );
       *((int)var->datum) = value;
    }
    void fini_variable( variable_t var ) // optionally by pointer
    {
       free( var.datum );
    }
    

    In C++ you can improve this approach by using classes to simplify the usage, but more importantly you can go for more complex solutions and use existing libraries as boost::any or boost::variant that offer different solutions to the same problem.

    Both boost::any and boost::variant store the values in dynamically allocated memory, usually through a pointer to a virtual class in a hierarchy, and with operators that reinterpret (down casts) to the concrete types.

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

Sidebar

Related Questions

Introduction: I think Nape is a relatively new engine so some of you may
Introduction : I have prior experience in programming (C, C++, Java), however, this is
Introduction: I want to loop through XML files with flexible categories structure. Problem: I
Introduction elem.hidden is a new property that allows to hide elements and detect whether
Introduction I am programming a semantic web application in haskell. With hsparql http://hackage.haskell.org/package/hsparql I
Introduction Hello. I am a newbie to PHP programming. I have know the basics
With introduction of GObject introspection the way to access theme colors through widget.get_style() method
Introduction We all know these silly arguments objects of JavaScript functions. But why object?
Introduction In my current organisation, we have many desktop and web applications all feeding
Has the introduction of the .net framework made raw programming in COM and DCOM

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.