Thread: Dynamic variable type definition

  1. #1
    Registered User
    Join Date
    Jan 2011

    Dynamic variable type definition


    Question: Is there a way do dynamically define a variable?


    given a number i decide will my next number be defined as 16, 32 or 64 bit integer.

    in c i can do that with helper functions and macros. I probably can do the same thing here in c++, but is there maybe an alternative c++ way?

    int a = 300;
    if a> 255
      // b should be defined as 32 bit integer
    if a > 2^32
      // b should be defined as 64 bit integer
    // initialize b 
    // then pass this b into some function template that can handle 32 and 64 bit integers
    thank you


  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    The standard way is a union
    union integerX
        int16 i16;
        int32 i32;
        int64 i64;
    You can look here

    You allocate memory as the biggest member, so 64 bits. If you want to save memory there are other workarounds but I guess your intent is just to have the correct type.

  3. #3
    Registered User
    Join Date
    Apr 2006
    yes, provided that a is a constant.

    //untested, may have typos
    #include <type_traits>
    typedef std::conditional<  DETERMINING_QUANTITY < 4294967296ll, int, long long>::type ConditionalType;
    ConditionalType conditionally_typed_variable;
    void foo(int x);
    void foo(long long x);
    int main(){
      //this calls the right version of foo
    EDIT: If a is not a constant, then something like this may work:

    void foo(int x);
    void foo(long long x);
    int main(){
      int determining_quantity = 300;
      long long determining_variable;
      if(determining_quantity <4294967296ll )
    Last edited by King Mir; 12-12-2012 at 07:23 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    It would be great if you would show us how you do it in C as it would leave no doubt as to what you actually want to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int first(int & value) {} type function definition
    By mahaju in forum C++ Programming
    Replies: 13
    Last Post: 01-14-2012, 09:32 AM
  2. missing type definition
    By roshni in forum C Programming
    Replies: 2
    Last Post: 06-17-2011, 08:13 AM
  3. confused with new type of int definition
    By devilofwar in forum C Programming
    Replies: 2
    Last Post: 12-07-2010, 03:39 PM
  4. Type definition issues?
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2010, 07:17 PM
  5. Dynamic Libraries and definition files
    By IconTree in forum Linux Programming
    Replies: 1
    Last Post: 08-10-2005, 01:51 PM