Struct Redefinition

This is a discussion on Struct Redefinition within the C++ Programming forums, part of the General Programming Boards category; Hi, does there happen to be a way to concatenate random or unique constants (such as __FILE__ and __LINE__) to ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Smile Struct Redefinition

    Hi, does there happen to be a way to concatenate random or unique constants (such as __FILE__ and __LINE__) to form the name of a struct? So you're guaranteed unique struct's and no redefinition errors?

    Code:
    #define CREATE_STRUCT() struct s##__LINE__ {}
    
    int main()
    {
        CREATE_STRUCT() s1;
        CREATE_STRUCT() s2; //redefinition error
    }
    I just want to know if it's possible. Thanks
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    I imagine you can, but how would you refer to it again later on?

    What problem are you trying to solve?
    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
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Salem View Post
    What problem are you trying to solve?
    Macro to create a randomly defined struct which avoids the redefinition error shown above. I guess it would be essentially the exact same thing as an anonymous struct, except you can define a constructor.

    Quote Originally Posted by Salem View Post
    I imagine you can, but how would you refer to it again later on?
    I suppose you couldn't (it's not necessarily necessary however). That's partly why I'm not actually using this in any projects. I was just wondering if this sort of thing is possible (for the future).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Doesn't the error message mention the name of the struct? You can also probably let the compiler output preprocessed file (-E with GCC).

    I'm not much of a macro user, but some experimentation showed that this might work (as I understand the middle macro is needed to get __LINE__ replaced with the number.

    Code:
    #define CREATE_STRUCT() CREATE_STRUCT_IMP1(__LINE__)
    #define CREATE_STRUCT_IMP1(id) CREATE_STRUCT_IMP2(id)
    #define CREATE_STRUCT_IMP2(id) struct s##id {}
    
    int main()
    {
        CREATE_STRUCT() s1;
        CREATE_STRUCT() s2;
    }
    It would be interesting to know indeed, what kind of problem this is supposed to solve.
    Last edited by anon; 08-04-2009 at 07:24 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You got it, nice job anon! Thanks guys!

    Quote Originally Posted by anon View Post
    It would be interesting to know indeed, what kind of problem this is supposed to solve.
    Originally it was intended to shorten my macro so that I wasn't forced to come up with my own struct names (one of the commonly recommended tips is to choose specific unique struct names when defining them locally), and could just have the macro randomly generate one. The reason not to use an anonymous struct would be because you cannot have a constructor, and I required one. Now the only problem with this is that you can't get the type of the class, like if you need to pass one of the struct's functions as a parameter (boost::bind/boost::function/etc.). Hence why I have no use for this at the moment, but I was interested to see if it was even possible considering the time I spent on it, and I feel knowing how to convert macro strings into definitions may come in handy in the future.

    Callbacks
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    Have you considered defining the struct in an anonymous namespace? This would be the appropriate approach if you want to define a helper struct in a source file to serve as implementation detail.
    Last edited by laserlight; 08-04-2009 at 10:09 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM

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