Type definitions within conditionals?

This is a discussion on Type definitions within conditionals? within the C++ Programming forums, part of the General Programming Boards category; Is this possible? void definer(int bitrate) { if (bitrate = 8) typedef char MY_TYPE; if (bitrate = 32) typedef signed ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Question Type definitions within conditionals?

    Is this possible?

    void definer(int bitrate)
    {
    if (bitrate = 8) typedef char MY_TYPE;
    if (bitrate = 32) typedef signed long MY_TYPE;
    }

    Then, MY_TYPE would be used in the main() function.

    I'm making an audio app using the RtAudio classes, which has numerous functions that require the size of each piece of audio data. At compile time, I could use #ifdef BITRATE_8 #define ... #endif, but I need this to be at runtime. Even within the definer function, if I try to use MY_TYPE later, GCC gives an error saying MY_TYPE is not defined in the scope. Any ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Use a union, something like
    Code:
    struct mytype {
      enum { B8, B32 } bitrate;
      union {
        unsigned char b8_value;
        unsigned long int b32_value;
      } v;
    };
    Then you can say things like
    Code:
    struct mytype foo, bar;
    foo.bitrate = B8;
    foo.v.b8_value = 123;
    
    bar.bitrate = B32;
    bar.v.b32_value = 123456;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    Quote Originally Posted by Salem
    Then you can say things like
    Code:
    struct mytype foo, bar;
    foo.bitrate = B8;
    foo.v.b8_value = 123;
    
    bar.bitrate = B32;
    bar.v.b32_value = 123456;
    Thanks! But the point is that the later functions shouldn't have to check whether the bitrate is 8 or 32 ... they should just be able to access "bar.v.value" or something like that.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    I suppose you could create a bunch of template functions, and instantiate B8 and B32 versions of each of those functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    If you put the union in a class with a static member indicating the current bit rate, with suitable functions and operator overloading, you can then use variables of that type without having to think about checking the bitrate each time (it being done within the class)...

    You cannot dynamically change types in your programme - typedefs are 'hardwired' in at compile/link time - any runtime changes would need to be coded in to your application.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21