Thread: Put together function name from type in macro

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    10

    Put together function name from type in macro

    Hi!

    I'm doing some experimentation with C and trying some stuff out.
    Right now I am playing around with macros and I found the GCC typeof keyword which I found intriging.

    Anyway so I am trying to make a "initializer" macro which will more or less take any type of structure and call it's "construct function"

    More or less something like this:
    Code:
    SomeType foo;
    construct( foo ); // Becomes -> SomeType_Construct( &foo );
    Though I am having problem with pasting together the type and the _Construct and is wondering if anyone else know what to do or got a better idea?

    Doing this doesn't work as it tries to construct this string: typeof( object )_Construct( &object )
    Code:
    #define construct( object ) typeof( object )##_Construct( &object )
    If you got a better way you know to solve the same problem then of course I am interested in that as well.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That's not going to work.

    The `typeof' thing is not a macro to be expanded; it is an operator (keyword) like `sizeof'.

    The `typeof', like `sizeof', resolves during compilation proper and not the processing phase.

    Soma

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Rather than trying to fake constructors in C, I suggest you learn a little C++.
    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"

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Probably best. I had a bit of a think about this, as the post by the OP would have been quite a nice and simple trick if it worked. I couldn't think of anything much though - nothing that hasn't been covered in various places.

    Quote Originally Posted by Groogy
    Anyway so I am trying to make a "initializer" macro which will more or less take any type of structure and call it's "construct function"
    Yeah. C++. Definitely. Constructors. Automatically called when you create the struct. Failing that, you can do it your way with overloaded functions, define construct() functions with different types, but I wouldn't bother.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    The last C standard defines a new construct to do exactly that, without compiler extension like typeof(), but it's only supported by clang and gcc (?) so far.
    Code:
    #define construct(x) _Generic((x),\
        char: construct_char,\
        int: construct_int,\
        struct foo: construct_st_foo,\
        ...)(x)
    Google "c11 _Generic" for examples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What type is a macro constant?
    By Vespasian in forum C Programming
    Replies: 15
    Last Post: 09-08-2011, 11:10 AM
  2. Is it possible to set type for define macro?
    By 6tr6tr in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2008, 01:32 PM
  3. macro function
    By freeindy in forum C Programming
    Replies: 6
    Last Post: 11-08-2007, 05:04 AM
  4. Macro has same name as Function?
    By coolclu3 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 04:55 AM
  5. macro or function
    By studentc in forum C Programming
    Replies: 5
    Last Post: 06-02-2004, 01:40 PM