name of a type

This is a discussion on name of a type within the C++ Programming forums, part of the General Programming Boards category; This is an oddity. I don't usually ask questions around here. My issue is this, using macros you can do ...

  1. #1
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    name of a type

    This is an oddity. I don't usually ask questions around here.

    My issue is this, using macros you can do the following...

    #define DOIT(typename) const char *typename ## _STR = #typename ;

    DOIT(ClassName)

    where DOIT(ClassName) will expand to the following...

    const char *ClassName_STR = "ClassName";


    My question is, can you do the equivalent with templates? Or some other way without using macros.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    another possible approach is typeid

    Kuphryn

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    thanks. that's a very good answer for the question that I asked. I'm afraid I asked the wrong question though.

    What I have now, that I wish to replace is a macro that in addition to what I said above, does this:

    DECLARE_A_STRUCT (structname,
    int blah1;
    int blah2;
    float blah3;
    )

    and it expands to:

    struct structname
    {
    int blah1;
    int blah2;
    float blah3;
    };
    const char *structname_str = "int blah1; int blah2; float blah3;";

    The basic idea here is to provide a string version of the declared struct. I hate it of course. Any ideas?
    Last edited by FillYourBrain; 08-11-2006 at 04:05 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,836
    The only way to do what you want involves macros. Or explicitly providing the string yourself eery time which, I presume, is what you wish to avoid.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's what I thought. It's a sad attempt by me to mimic some of the "reflection" features of other languages.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    5,836
    CORBA libraries (or ORBS) and, IIRC, other systems like DCOM support reflection capabilities. They way they achieve that in in C or C++ involves code generation (eg mapping an interface specification to C++) or hard-coded types with defined library support. That is probably overkill for what you want -- it's intended to support a generic interface to distributed objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 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