a kind of template (or macro) to reuse parts of code...

Discussion in 'C++' started by A, Oct 14, 2011.

  1. A

    A Guest

    Can this be done (and how) in C++ in the following pseudo-code form:

    DEFINE_I_TO_1
    {
    int i=1;
    }

    int increaseby1()
    {
    DEFINE_I_TO_1
    return i+1;
    }

    int increaseby2()
    {
    DEFINE_I_TO_1
    return i+2;
    }

    In other words - I would like to keep all the scopes, definitions etc and
    just to insert a reusable piece of code - much like copy-pasting a block of
    template code.

    Of course, I could put it in a function, but I would like to know also if
    there is a way to do it this way because in certain uses this would make
    sense - like loops where timing is critical putting inline code would
    obviously be faster way than calling a function.

    I know C++ has macros and templates but from what I've seen they are not
    intended to be used like this. Or am I wrong?

    Is this possible and what is the easiest way to achieve this if possible?
     
    A, Oct 14, 2011
    #1
    1. Advertising

  2. On 14.10.2011 21:25, A wrote:
    > Can this be done (and how) in C++ in the following pseudo-code form:
    >
    > DEFINE_I_TO_1
    > {
    > int i=1;
    > }
    >
    > int increaseby1()
    > {
    > DEFINE_I_TO_1
    > return i+1;
    > }
    >
    > int increaseby2()
    > {
    > DEFINE_I_TO_1
    > return i+2;
    > }
    >
    > In other words - I would like to keep all the scopes, definitions etc and
    > just to insert a reusable piece of code - much like copy-pasting a block of
    > template code.
    >
    > Of course, I could put it in a function, but I would like to know also if
    > there is a way to do it this way because in certain uses this would make
    > sense - like loops where timing is critical putting inline code would
    > obviously be faster way than calling a function.
    >
    > I know C++ has macros and templates but from what I've seen they are not
    > intended to be used like this. Or am I wrong?
    >
    > Is this possible and what is the easiest way to achieve this if possible?


    The above sounds as if you're asking about how to define functions, but
    then in the text you indicate that you are familiar with that. So it
    does not make sense as given. You need to provide a more clear example.

    Read the FAQ item on how to post question.

    It is always a good idea to check the FAQ before asking.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Oct 14, 2011
    #2
    1. Advertising

  3. A

    A Guest

    > The above sounds as if you're asking about how to define functions, but
    > then in the text you indicate that you are familiar with that. So it does
    > not make sense as given. You need to provide a more clear example.


    If I call a function that would imply after compilation - storing registers
    on stack, a call to function, then returning registers from stack and
    return. All of that takes CPU cycles for time-critical loops. With inline
    code it would be compilers job to kind of "paste" a code defined in the
    template. And no, I don't really want to use inline assembly.

    If I modify the code in the template it is "pasted" on next compilation.
    Once, not 10 times in all 10 places where I use this let's call it macro or
    template.

    So that's what I'm looking a way for to do. Like I said, I know how to use
    functions.

    I hope it is more clear now...
     
    A, Oct 14, 2011
    #3
  4. A

    A Guest

    > DEFINE_I_TO_1
    > {
    > int i=1;
    > }
    > int increaseby1()
    > {
    > DEFINE_I_TO_1
    > return i+1;
    > }


    the capitals imply it is inline code and not a function call... see "i" is
    not defined in increaseby1() function, but the macro or whatever would
    define it in this pseudo-code case.
     
    A, Oct 14, 2011
    #4
  5. A

    Ian Collins Guest

    On 10/15/11 09:16 AM, A wrote:

    Please don't snip attributions, it is rude.

    >> The above sounds as if you're asking about how to define functions, but
    >> then in the text you indicate that you are familiar with that. So it does
    >> not make sense as given. You need to provide a more clear example.

    >
    > If I call a function that would imply after compilation - storing registers
    > on stack, a call to function, then returning registers from stack and
    > return. All of that takes CPU cycles for time-critical loops. With inline
    > code it would be compilers job to kind of "paste" a code defined in the
    > template. And no, I don't really want to use inline assembly.


    How do you know? Have you measured?

    C++ (and C these days) compilers are very good at inlining trivial
    functions.

    --
    Ian Collins
     
    Ian Collins, Oct 14, 2011
    #5
  6. A

    A Guest

    > C++ (and C these days) compilers are very good at inlining trivial
    > functions.


    OK, it is not just the speed, it is also the convenience and maintaining the
    code.

    Anyway, back to topic, is something like what I asked possible in a way to
    do with macros or templates or any other way except functions?
     
    A, Oct 14, 2011
    #6
  7. A

    Ian Collins Guest

    On 10/15/11 09:36 AM, A wrote:

    *Please don't snip attributions, it is rude.* I'll ignore you if you do
    it again.

    > Ian wrote:


    >> C++ (and C these days) compilers are very good at inlining trivial
    >> functions.

    >
    > OK, it is not just the speed, it is also the convenience and maintaining the
    > code.


    How is a macro list thing more convenient than a function?

    > Anyway, back to topic, is something like what I asked possible in a way to
    > do with macros or templates or any other way except functions?


    Why? You haven't provided any justification for this new construct.

    --
    Ian Collins
     
    Ian Collins, Oct 14, 2011
    #7
  8. On 14.10.2011 21:25, A wrote:
    > In other words - I would like to keep all the scopes, definitions etc and
    > just to insert a reusable piece of code - much like copy-pasting a block of
    > template code.
    >
    > Of course, I could put it in a function, but I would like to know also if
    > there is a way to do it this way because in certain uses this would make
    > sense - like loops where timing is critical putting inline code would
    > obviously be faster way than calling a function.


    You should have more confidence in modern compilers. Sometimes you will
    not recognize your original code in the generated assembly.

    C++11 improves things even more, e.g. with constexpr.

    > I know C++ has macros and templates but from what I've seen they are not
    > intended to be used like this. Or am I wrong?


    There is nothing wrong with functions for reusable code.

    > Is this possible and what is the easiest way to achieve this if possible?


    Did you measure the predicated impact of inline functions in optimized
    builds?


    Marcel
     
    Marcel Müller, Oct 14, 2011
    #8
  9. A

    Noah Roberts Guest

    On Oct 14, 1:36 pm, "A" <> wrote:
    > > C++ (and C these days) compilers are very good at inlining trivial
    > > functions.

    >
    > OK, it is not just the speed, it is also the convenience and maintaining the
    > code.
    >


    Because coming up with obscure macros that hide variable scoping
    issues is easier to maintain than functions.
     
    Noah Roberts, Oct 14, 2011
    #9
  10. On 10/14/11 3:25 PM, A wrote:
    > Can this be done (and how) in C++ in the following pseudo-code form:
    >
    > DEFINE_I_TO_1
    > {
    > int i=1;
    > }
    >
    > int increaseby1()
    > {
    > DEFINE_I_TO_1
    > return i+1;
    > }
    >
    > int increaseby2()
    > {
    > DEFINE_I_TO_1
    > return i+2;
    > }
    >
    > In other words - I would like to keep all the scopes, definitions etc and
    > just to insert a reusable piece of code - much like copy-pasting a block of
    > template code.
    >
    > Of course, I could put it in a function, but I would like to know also if
    > there is a way to do it this way because in certain uses this would make
    > sense - like loops where timing is critical putting inline code would
    > obviously be faster way than calling a function.
    >
    > I know C++ has macros and templates but from what I've seen they are not
    > intended to be used like this. Or am I wrong?
    >
    > Is this possible and what is the easiest way to achieve this if possible?
    >
    >


    First,
    the way you have defined DEFINE_I_TO_1 it generates a variable i with a
    local scope that then immediately ends. Your following examples then
    will generate errors (unless there is another i out side the scope of
    these snippets

    It does sound like you very much really want:

    #define DEFINE_I_TO_1 int i=1;

    inline int increaseby1()
    {
    DEFINE_I_TO_1
    return i+1;
    }

    inline int increaseby2()
    {
    DEFINE_I_TO_1
    return i+2;
    }
     
    Richard Damon, Oct 14, 2011
    #10
  11. A

    Jorgen Grahn Guest

    On Fri, 2011-10-14, A wrote:
    >> The above sounds as if you're asking about how to define functions, but
    >> then in the text you indicate that you are familiar with that. So it does
    >> not make sense as given. You need to provide a more clear example.

    >
    > If I call a function that would imply after compilation - storing registers
    > on stack, a call to function, then returning registers from stack and
    > return. All of that takes CPU cycles for time-critical loops. With inline
    > code it would be compilers job to kind of "paste" a code defined in the
    > template. And no, I don't really want to use inline assembly.


    You use the term "inline" over and over, but you seem to overlook
    inline functions. All the (relatively) costly things you mention above,
    plus the missed optimization opportunities, go away with inline
    functions.

    ....
    > So that's what I'm looking a way for to do. Like I said, I know how to use
    > functions.


    Not if you equate calling a function with heavy stack/register
    operations.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 15, 2011
    #11
  12. A

    A Guest

    > #define DEFINE_I_TO_1 int i=1;

    so it is a macro... this looks like something what i had in mind

    will something like this work for more than a single line of code?
     
    A, Oct 15, 2011
    #12
  13. A

    A Guest

    > *Please don't snip attributions, it is rude.* I'll ignore you if you do
    > it again.


    Sorry, don't have idea what you're talking about here. Just cutting out the
    relevant part for the answer. All newsreaders have in-reference-to header so
    it is groupped as it should be.

    >> How is a macro list thing more convenient than a function?


    Because in this particular case it would be more useful. I have overloaded
    function that calls something like this:

    - a block of always the same block of code here
    - something different
    - a different block of the same code here

    and it is repeated in overloaded functions over and over. if I make a simple
    change in one or the other block I have to copy-paste them in each
    overloaded function. maintaining this would be easier if this code is placed
    in a function or... a kind of macro like i asked for in above explanations.

    >> do with macros or templates or any other way except functions?

    > Why? You haven't provided any justification for this new construct.


    I am informing myself of other ways of doing things. I already explained I
    can do it with functions but in this particular case a macro thing would be
    way more practical. No solution fits purposes.
     
    A, Oct 15, 2011
    #13
  14. A

    A Guest

    I tried this (as an dumb example) and it seems to work...
    I think I'll be learning a bit more about macros.

    The only drawback I see so far is the fact I can't debug the CODEPART1 and
    CODEPART2 but except that it is working as intented.

    #define CODEPART1 \
    std::vector<int> abc(5); \
    for (int i = 0; i < abc.size(); i++) \
    { \
    abc = i; \
    }

    #define CODEPART2 \
    return abc[4];

    int overload(int a)
    {
    CODEPART1
    abc[0] = 0;
    CODEPART2
    }

    int overload(__int64 a)
    {
    CODEPART1
    abc[1] = 0;
    CODEPART2
    }
     
    A, Oct 15, 2011
    #14
  15. On 10/15/11 11:54 AM, A wrote:
    > I tried this (as an dumb example) and it seems to work...
    > I think I'll be learning a bit more about macros.
    >
    > The only drawback I see so far is the fact I can't debug the CODEPART1 and
    > CODEPART2 but except that it is working as intented.
    >
    > #define CODEPART1 \
    > std::vector<int> abc(5); \
    > for (int i = 0; i< abc.size(); i++) \
    > { \
    > abc = i; \
    > }
    >
    > #define CODEPART2 \
    > return abc[4];
    >
    > int overload(int a)
    > {
    > CODEPART1
    > abc[0] = 0;
    > CODEPART2
    > }
    >
    > int overload(__int64 a)
    > {
    > CODEPART1
    > abc[1] = 0;
    > CODEPART2
    > }
    >
    >


    The limitations on debugging are a function of the debugger. A good
    debugger will at least let you switch into an assembly view of the code
    to see what is happening.

    If you are having trouble with a macro expansion, replacing the macro
    with its expanded text temporarily normally will make it easier to debug
    (and then if you needed to change the code, go back and fix the original
    macro to match, but ideally you would have debugged the snippet as
    independent code before pushing it into a macro.
     
    Richard Damon, Oct 15, 2011
    #15
  16. A

    Jorgen Grahn Guest

    On Sat, 2011-10-15, A wrote:
    >> *Please don't snip attributions, it is rude.* I'll ignore you if you do
    >> it again.

    >
    > Sorry, don't have idea what you're talking about here.


    What you just did -- skip the line "Ian Collins wrote:". It's that
    thing which lets people look at your posting *only* and know who wrote
    what.

    The rest of your quoting seems fine to me.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 15, 2011
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    5
    Views:
    549
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    427
    Hylander
    Feb 26, 2004
  3. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,040
    Malcolm
    Feb 12, 2006
  4. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    543
    Dave Thompson
    Dec 18, 2006
  5. Replies:
    3
    Views:
    609
    Keith Thompson
    Mar 31, 2007
Loading...

Share This Page