cast at runtime

This is a discussion on cast at runtime within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have the following issue to solve. I have a lookup table containing enum types for datatypes in ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    cast at runtime

    Hi all,

    I have the following issue to solve.
    I have a lookup table containing enum types for datatypes in different languages

    std::string cTYPE | NUMPY type (Python) | Matlab Type
    double | NPY_DOUBLELTR | mxDOUBLE_CLASS
    .....

    What I want to do is to determine the size of a particular type:
    My program determines the corresponding types by looking them up th table above.
    Given mxDOUBLE_CLASS, I get

    std:air<std::string, int> p ; //storing string for cType and int for NUMPY constant


    how can I use sizeof with the given name p.first to determine the size of the particular type.
    In more detail, does

    sizeof("double")
    or
    sizeof(p.first)
    work?

    Can I use the name stored in the string p.first as a template parameter for

    some_function<p.first>(...)

    If so, how do I do it? I try to avoid writing thousands of if cases.

    Thank you in advance for your help,
    Thomas
    Last edited by thomas15; 04-29-2008 at 12:35 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    and?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    does and mean:
    the thread is unclear

    or
    thats a piece of cake

    or
    I have no clue?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You can use typeinfo to get a name for the dfferent types, but that name is different on different platforms and so it would be hard to code it into your application.

    Templates are evaluated at compile time, so you can't use a string variable as the type for them.

    Other than by using a macro I'm not sure there's a way to do that cleanly.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    ok, thats what I have expected...
    will start writing something arround it...

    thank you, but please let me know if somebody has an idea...

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Couldn't you just read the data types from the file and use some kind of if-else statements:
    Code:
    std::string dataTypeStr;
    dataTypeStr << file;
    
    if ( dataTypeStr == "int" )
    {
       ...
    }
    else if ( dataTypeStr == "double" )
    {
       ...
    }
    You might even be able to create some kind of parsing class that converts the string type & values into the appropriate types to make things easier if you need to do that a lot.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    If so, how do I do it? I try to avoid writing thousands of if cases.
    that's exactly what he wants to avoid doing

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by m37h0d View Post
    that's exactly what he wants to avoid doing
    Oops, missed that. But my other suggestion might help.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could see if Boost.Variant solves your problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by thomas15 View Post
    does and mean:
    the thread is unclear

    or
    thats a piece of cake

    or
    I have no clue?
    It means I saw your post before your edit, when you had practically no information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

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