Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. You ever produce code you know is stupid, but you don't know a better way to do it?

You ever produce code you know is stupid, but you don't know a better way to do it?

Scheduled Pinned Locked Moved The Lounge
announcementc++databasecom
20 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H honey the codewitch

    Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

    rgb_pixel<24> foo;
    foo.channel(63);

    so you could set multiple channels at once.

    rgb_pixel<24> foo;
    foo.channel(63,31,47);

    You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

    // sets the integer channel value by name
    template
    constexpr inline void channel(typename channel_by_index::value>::int_type value) {
    constexpr const int index = channel_index_by_name::value;
    channel(value);
    }
    // sets the integer channel values by name
    template
    constexpr inline void channel(typename channel_by_index::value>::int_type value1,
    typename channel_by_index::value>::int_type value2) {
    constexpr const int index1 = channel_index_by_name::value;
    channel(value1);
    constexpr const int index2 = channel_index_by_name::value;
    channel(value2);
    }
    // sets the integer channel values by name
    template
    constexpr inline void channel(typename channel_by_index::value>::int_type value1,
    typename channel_by_index::value>::int_type value2,
    typename channel_by_index::value>::int_type value3) {
    constexpr const int index1 = channel_index_by_name

    S Offline
    S Offline
    swampwiz
    wrote on last edited by
    #3

    Yes, this is part & parcel of programming. :) Back when I was slinging code, I used to read books from Scott Meyer ("Effective C++") et al to bring my level of coding practice to that of a fine craftsman. Did that craftsmanship keep me employable? NO Do employers give a rat's a33 about such craftsmanship? H3LL NO Indeed, the reason that Java has become the default application development is because all its belts & suspenders make it so that the product that poor coders put out will not (usually) cause crashes and other failures, since the compiler ensures that everything is neatly belted up. I'd like to say something similar about JavaScript, but that monster makes Java look professorial.

    1 Reply Last reply
    0
    • H honey the codewitch

      Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

      rgb_pixel<24> foo;
      foo.channel(63);

      so you could set multiple channels at once.

      rgb_pixel<24> foo;
      foo.channel(63,31,47);

      You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

      // sets the integer channel value by name
      template
      constexpr inline void channel(typename channel_by_index::value>::int_type value) {
      constexpr const int index = channel_index_by_name::value;
      channel(value);
      }
      // sets the integer channel values by name
      template
      constexpr inline void channel(typename channel_by_index::value>::int_type value1,
      typename channel_by_index::value>::int_type value2) {
      constexpr const int index1 = channel_index_by_name::value;
      channel(value1);
      constexpr const int index2 = channel_index_by_name::value;
      channel(value2);
      }
      // sets the integer channel values by name
      template
      constexpr inline void channel(typename channel_by_index::value>::int_type value1,
      typename channel_by_index::value>::int_type value2,
      typename channel_by_index::value>::int_type value3) {
      constexpr const int index1 = channel_index_by_name

      E Offline
      E Offline
      englebart
      wrote on last edited by
      #4

      It looks like you created a template tuple. That proves you do understand tuples! (I am mostly lost)๐Ÿ˜Š

      H 1 Reply Last reply
      0
      • E englebart

        It looks like you created a template tuple. That proves you do understand tuples! (I am mostly lost)๐Ÿ˜Š

        H Offline
        H Offline
        honey the codewitch
        wrote on last edited by
        #5

        My first impression was "tuple? I don't think you read my code right" And then I realized, you're just looking at my pixel<> template as if it were a tuple, and that makes sense because you can access the channels off of it as though they were tuple fields. I never looked at it that way, but you're absolutely right. :laugh: Maybe you understand more of it than you think?

        Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

        E 1 Reply Last reply
        0
        • H honey the codewitch

          My first impression was "tuple? I don't think you read my code right" And then I realized, you're just looking at my pixel<> template as if it were a tuple, and that makes sense because you can access the channels off of it as though they were tuple fields. I never looked at it that way, but you're absolutely right. :laugh: Maybe you understand more of it than you think?

          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

          E Offline
          E Offline
          englebart
          wrote on last edited by
          #6

          I was referring to this clause.

          template

          I have only ever used tuples in Python: (anyValue1, anyValue2,โ€ฆ) slap some items together so you can track them with a single reference. Great for returning multiple pieces of information from a function.

          H 1 Reply Last reply
          0
          • E englebart

            I was referring to this clause.

            template

            I have only ever used tuples in Python: (anyValue1, anyValue2,โ€ฆ) slap some items together so you can track them with a single reference. Great for returning multiple pieces of information from a function.

            H Offline
            H Offline
            honey the codewitch
            wrote on last edited by
            #7

            Ah, those are simply template arguments in C++. :)

            Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

            1 Reply Last reply
            0
            • H honey the codewitch

              Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

              rgb_pixel<24> foo;
              foo.channel(63);

              so you could set multiple channels at once.

              rgb_pixel<24> foo;
              foo.channel(63,31,47);

              You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

              // sets the integer channel value by name
              template
              constexpr inline void channel(typename channel_by_index::value>::int_type value) {
              constexpr const int index = channel_index_by_name::value;
              channel(value);
              }
              // sets the integer channel values by name
              template
              constexpr inline void channel(typename channel_by_index::value>::int_type value1,
              typename channel_by_index::value>::int_type value2) {
              constexpr const int index1 = channel_index_by_name::value;
              channel(value1);
              constexpr const int index2 = channel_index_by_name::value;
              channel(value2);
              }
              // sets the integer channel values by name
              template
              constexpr inline void channel(typename channel_by_index::value>::int_type value1,
              typename channel_by_index::value>::int_type value2,
              typename channel_by_index::value>::int_type value3) {
              constexpr const int index1 = channel_index_by_name

              J Offline
              J Offline
              Josef Schroettle
              wrote on last edited by
              #8

              Hi, you wrote that you are already using a parameter pack in the function rgbpixel::channel. Where would that be? Please post the signature(s) of rgbpixel::channel. Is your code somewhere so I could have a look?

              Josef Schroettle

              H 1 Reply Last reply
              0
              • J Josef Schroettle

                Hi, you wrote that you are already using a parameter pack in the function rgbpixel::channel. Where would that be? Please post the signature(s) of rgbpixel::channel. Is your code somewhere so I could have a look?

                Josef Schroettle

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #9

                Sure gfx/include/gfx_pixel.hpp at master ยท codewitch-honey-crisis/gfx ยท GitHub[^] Pixel is at line 504

                template
                struct pixel {
                ...

                This parameter pack is effectively passed through to each of the metadata query templates, as well as any function that access pixel data, like the implementation of channel<>()

                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                1 Reply Last reply
                0
                • H honey the codewitch

                  Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                  rgb_pixel<24> foo;
                  foo.channel(63);

                  so you could set multiple channels at once.

                  rgb_pixel<24> foo;
                  foo.channel(63,31,47);

                  You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                  // sets the integer channel value by name
                  template
                  constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                  constexpr const int index = channel_index_by_name::value;
                  channel(value);
                  }
                  // sets the integer channel values by name
                  template
                  constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                  typename channel_by_index::value>::int_type value2) {
                  constexpr const int index1 = channel_index_by_name::value;
                  channel(value1);
                  constexpr const int index2 = channel_index_by_name::value;
                  channel(value2);
                  }
                  // sets the integer channel values by name
                  template
                  constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                  typename channel_by_index::value>::int_type value2,
                  typename channel_by_index::value>::int_type value3) {
                  constexpr const int index1 = channel_index_by_name

                  L Offline
                  L Offline
                  lilly william
                  wrote on last edited by
                  #10

                  Yes, it's common to produce code that feels suboptimal, especially when you're learning or exploring new programming concepts. Here are a few steps to improve such code: 1. **Refactor Gradually**: Break down complex functions into smaller, manageable pieces. This improves readability and makes debugging easier. 2. **Seek Feedback**: Share your code with peers or mentors. Platforms like Stack Overflow are great for getting constructive feedback. 3. **Learn Best Practices**: Study design patterns and https://cotonmode.co.uk/collections/throws[^] best practices in coding. Books like "Clean Code" by Robert C. Martin are excellent resources. 4. **Use Tools**: Leverage code analysis tools like linters and static analyzers to identify and fix inefficiencies. 5. **Continuous Learning**: Stay updated with new techniques and languages. Online courses, tutorials, and documentation can be very helpful. 6. **Test Rigorously**: Write unit tests to ensure your code works as expected and to catch potential issues early. 7. **Read Code**: Examine code written by experienced developers. GitHub is a good place to find and study well-structured code. 8. **Iterate**: Don't aim for perfection in the first go. Write, test, refactor, and improve continuously.

                  1 Reply Last reply
                  0
                  • H honey the codewitch

                    Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                    rgb_pixel<24> foo;
                    foo.channel(63);

                    so you could set multiple channels at once.

                    rgb_pixel<24> foo;
                    foo.channel(63,31,47);

                    You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                    // sets the integer channel value by name
                    template
                    constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                    constexpr const int index = channel_index_by_name::value;
                    channel(value);
                    }
                    // sets the integer channel values by name
                    template
                    constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                    typename channel_by_index::value>::int_type value2) {
                    constexpr const int index1 = channel_index_by_name::value;
                    channel(value1);
                    constexpr const int index2 = channel_index_by_name::value;
                    channel(value2);
                    }
                    // sets the integer channel values by name
                    template
                    constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                    typename channel_by_index::value>::int_type value2,
                    typename channel_by_index::value>::int_type value3) {
                    constexpr const int index1 = channel_index_by_name

                    J Offline
                    J Offline
                    jochance
                    wrote on last edited by
                    #11

                    DRY is soooooo overrated.

                    U 1 Reply Last reply
                    0
                    • H honey the codewitch

                      Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                      rgb_pixel<24> foo;
                      foo.channel(63);

                      so you could set multiple channels at once.

                      rgb_pixel<24> foo;
                      foo.channel(63,31,47);

                      You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                      // sets the integer channel value by name
                      template
                      constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                      constexpr const int index = channel_index_by_name::value;
                      channel(value);
                      }
                      // sets the integer channel values by name
                      template
                      constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                      typename channel_by_index::value>::int_type value2) {
                      constexpr const int index1 = channel_index_by_name::value;
                      channel(value1);
                      constexpr const int index2 = channel_index_by_name::value;
                      channel(value2);
                      }
                      // sets the integer channel values by name
                      template
                      constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                      typename channel_by_index::value>::int_type value2,
                      typename channel_by_index::value>::int_type value3) {
                      constexpr const int index1 = channel_index_by_name

                      M Offline
                      M Offline
                      Member_16079586
                      wrote on last edited by
                      #12

                      Yes :laugh:

                      1 Reply Last reply
                      0
                      • H honey the codewitch

                        Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                        rgb_pixel<24> foo;
                        foo.channel(63);

                        so you could set multiple channels at once.

                        rgb_pixel<24> foo;
                        foo.channel(63,31,47);

                        You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                        // sets the integer channel value by name
                        template
                        constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                        constexpr const int index = channel_index_by_name::value;
                        channel(value);
                        }
                        // sets the integer channel values by name
                        template
                        constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                        typename channel_by_index::value>::int_type value2) {
                        constexpr const int index1 = channel_index_by_name::value;
                        channel(value1);
                        constexpr const int index2 = channel_index_by_name::value;
                        channel(value2);
                        }
                        // sets the integer channel values by name
                        template
                        constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                        typename channel_by_index::value>::int_type value2,
                        typename channel_by_index::value>::int_type value3) {
                        constexpr const int index1 = channel_index_by_name

                        P Offline
                        P Offline
                        PBorchert
                        wrote on last edited by
                        #13

                        Are you going to post the reddit solution?

                        H 1 Reply Last reply
                        0
                        • P PBorchert

                          Are you going to post the reddit solution?

                          H Offline
                          H Offline
                          honey the codewitch
                          wrote on last edited by
                          #14

                          // sets the integer channel values by name
                          template
                          constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                          constexpr const int index = channel_index_by_name::value;
                          channel(value);
                          }

                          // sets the integer channel values by name
                          template
                          constexpr inline void channel(typename channel_by_index::value>::int_type value,
                          typename type::channel_by_index::value>::int_type... values) {
                          constexpr const int index = type::channel_index_by_name::value;
                          channel(value);
                          channel(values...);
                          }

                          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                          1 Reply Last reply
                          0
                          • H honey the codewitch

                            Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                            rgb_pixel<24> foo;
                            foo.channel(63);

                            so you could set multiple channels at once.

                            rgb_pixel<24> foo;
                            foo.channel(63,31,47);

                            You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                            // sets the integer channel value by name
                            template
                            constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                            constexpr const int index = channel_index_by_name::value;
                            channel(value);
                            }
                            // sets the integer channel values by name
                            template
                            constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                            typename channel_by_index::value>::int_type value2) {
                            constexpr const int index1 = channel_index_by_name::value;
                            channel(value1);
                            constexpr const int index2 = channel_index_by_name::value;
                            channel(value2);
                            }
                            // sets the integer channel values by name
                            template
                            constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                            typename channel_by_index::value>::int_type value2,
                            typename channel_by_index::value>::int_type value3) {
                            constexpr const int index1 = channel_index_by_name

                            A Offline
                            A Offline
                            agolddog
                            wrote on last edited by
                            #15

                            Similarly, when I was still working, every once in a while I'd leave a comment like: //Seems like there should be a better way to do this, but I couldn't think of it. //< explanation of what the code is trying to do >

                            H 1 Reply Last reply
                            0
                            • H honey the codewitch

                              Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                              rgb_pixel<24> foo;
                              foo.channel(63);

                              so you could set multiple channels at once.

                              rgb_pixel<24> foo;
                              foo.channel(63,31,47);

                              You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                              // sets the integer channel value by name
                              template
                              constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                              constexpr const int index = channel_index_by_name::value;
                              channel(value);
                              }
                              // sets the integer channel values by name
                              template
                              constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                              typename channel_by_index::value>::int_type value2) {
                              constexpr const int index1 = channel_index_by_name::value;
                              channel(value1);
                              constexpr const int index2 = channel_index_by_name::value;
                              channel(value2);
                              }
                              // sets the integer channel values by name
                              template
                              constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                              typename channel_by_index::value>::int_type value2,
                              typename channel_by_index::value>::int_type value3) {
                              constexpr const int index1 = channel_index_by_name

                              S Offline
                              S Offline
                              SeattleC
                              wrote on last edited by
                              #16

                              Yes, absolutely. Some days I am just not inspired. On those days, I just crank out something and hope I can come back later to fix it. I often find that the next time around, I have a much better solution. The problem is, this only works if your employer is understanding enough to let you run your own show. Such employers are rare, of course.

                              1 Reply Last reply
                              0
                              • A agolddog

                                Similarly, when I was still working, every once in a while I'd leave a comment like: //Seems like there should be a better way to do this, but I couldn't think of it. //< explanation of what the code is trying to do >

                                H Offline
                                H Offline
                                honey the codewitch
                                wrote on last edited by
                                #17

                                Yeah, I use // TODO: for those so i can search them.

                                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                                1 Reply Last reply
                                0
                                • J jochance

                                  DRY is soooooo overrated.

                                  U Offline
                                  U Offline
                                  User 11783308
                                  wrote on last edited by
                                  #18

                                  It is not often, but there are times in C++ when there is no alternative but to duplicate code. That is when macros come to the rescue. Due to the need for exactly the same code in several related methods, I will sometimes group those together, add a macro #define _someName with the code, expand the macro in each method, and add #undef _someName at the end of the group of methods. Sometimes there just aren't any alternatives. In one case, I had an abstract base class that was essentially inherited in a diamond pattern to get the clean user interface that I wanted. But, even though there was no data in the abstract base class, a subclass either had to have two instances thus pointlessly increasing the size of the overall objects, or it had to be virtual, increasing both size and decreasing performance. There was no need for any of that because it was only providing an interface, with a default implementation of methods that only relied on other methods that were virtual. I wound up with two parallel lines of inheritance, with many methods duplicated between the lines. Invisible to client code (not the library code though), but there were no restrictions on size or performance. Yes, the virtual methods do require data -- but in the vtable, not in the code. And it is possible for the vtable to be optimized with global optimization (which is already being done at link time) so that multiple vtables are not required. Recently, I created an experimental class, which required macros of templates because a template cannot contain a name used in code. A bit strange, but it worked and the user interface was reasonable. That, fortunately, never made it into my library since I had better options. Even in C++ macros are very useful. They really, really need to be made more general with looping, variables, etc. There are places that I could use all of those features. Just like in older assembly languages. And why do macros have "elif", but neither C nor C++ do? Guess what? I use a global macro #define "elif else if". I also define "block", "loop", "when", "unless", "until", etc. I could do more with better macro features. There seems to be a belief that if a language is good enough, that macros are simply obsolete and should be eliminated. That is false. There is no such thing as a language that is good enough. And never will be. C++ certainly isn't. I will always want more in a language (which is why I use C++, even though it is very limited) and I don't have an extra decade or two to design and build a better one.

                                  J 1 Reply Last reply
                                  0
                                  • U User 11783308

                                    It is not often, but there are times in C++ when there is no alternative but to duplicate code. That is when macros come to the rescue. Due to the need for exactly the same code in several related methods, I will sometimes group those together, add a macro #define _someName with the code, expand the macro in each method, and add #undef _someName at the end of the group of methods. Sometimes there just aren't any alternatives. In one case, I had an abstract base class that was essentially inherited in a diamond pattern to get the clean user interface that I wanted. But, even though there was no data in the abstract base class, a subclass either had to have two instances thus pointlessly increasing the size of the overall objects, or it had to be virtual, increasing both size and decreasing performance. There was no need for any of that because it was only providing an interface, with a default implementation of methods that only relied on other methods that were virtual. I wound up with two parallel lines of inheritance, with many methods duplicated between the lines. Invisible to client code (not the library code though), but there were no restrictions on size or performance. Yes, the virtual methods do require data -- but in the vtable, not in the code. And it is possible for the vtable to be optimized with global optimization (which is already being done at link time) so that multiple vtables are not required. Recently, I created an experimental class, which required macros of templates because a template cannot contain a name used in code. A bit strange, but it worked and the user interface was reasonable. That, fortunately, never made it into my library since I had better options. Even in C++ macros are very useful. They really, really need to be made more general with looping, variables, etc. There are places that I could use all of those features. Just like in older assembly languages. And why do macros have "elif", but neither C nor C++ do? Guess what? I use a global macro #define "elif else if". I also define "block", "loop", "when", "unless", "until", etc. I could do more with better macro features. There seems to be a belief that if a language is good enough, that macros are simply obsolete and should be eliminated. That is false. There is no such thing as a language that is good enough. And never will be. C++ certainly isn't. I will always want more in a language (which is why I use C++, even though it is very limited) and I don't have an extra decade or two to design and build a better one.

                                    J Offline
                                    J Offline
                                    jochance
                                    wrote on last edited by
                                    #19

                                    See, like Rust is great and all and cpp is a little dangerous, but you don't get that smart^^ working only with hand holding-y languages. No pain no gain? C# doesn't really have macros the same, but it's the same kinda deal. There's a bunch of information in your post that represents learning like branches on a tree (which has weird branches that grow into one another). Starting with this stuff a long time ago when it was harder, you simply had to learn a bunch of things you just don't anymore. Many of those things have tendrils. Sum > whole of the parts...

                                    1 Reply Last reply
                                    0
                                    • H honey the codewitch

                                      Update: Someone on reddit helped me with this, and the solution is simple and elegant. I'm low key ashamed I didn't think of it. :laugh: Update 2: It doesn't work with the GCC version I use on some of my platforms. =( Oh well, stupid code FTW I run into that with C++ sometimes. all i wanted to do was extend

                                      rgb_pixel<24> foo;
                                      foo.channel(63);

                                      so you could set multiple channels at once.

                                      rgb_pixel<24> foo;
                                      foo.channel(63,31,47);

                                      You'd think you could do with a parameter pack (template but no, because I'm already using a parameter pack and you can't take two to a template. Anyway, I dug and dug and dug even through some esoteric techniques to stash parameter packs in tuples[^] which I still don't understand Finally I gave in. You know what I did?

                                      // sets the integer channel value by name
                                      template
                                      constexpr inline void channel(typename channel_by_index::value>::int_type value) {
                                      constexpr const int index = channel_index_by_name::value;
                                      channel(value);
                                      }
                                      // sets the integer channel values by name
                                      template
                                      constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                                      typename channel_by_index::value>::int_type value2) {
                                      constexpr const int index1 = channel_index_by_name::value;
                                      channel(value1);
                                      constexpr const int index2 = channel_index_by_name::value;
                                      channel(value2);
                                      }
                                      // sets the integer channel values by name
                                      template
                                      constexpr inline void channel(typename channel_by_index::value>::int_type value1,
                                      typename channel_by_index::value>::int_type value2,
                                      typename channel_by_index::value>::int_type value3) {
                                      constexpr const int index1 = channel_index_by_name

                                      M Offline
                                      M Offline
                                      MSBassSinger
                                      wrote on last edited by
                                      #20

                                      Sort of. The first several years I was in software development, I learned more and more, which helped me to improve code I had already written. It wasn't that my code was "stupid", but that I just learned newer and better ways. Second, as the language I use improves, it provides better ways to do things, but it is up to me to discern whether the "better way" is really better. Third, there are those team leads who are less knowledgeable and experienced, and make me rewrite code to how they think it should be done, even though I can make the clear case their way is not better. In those cases, I do what the lead says do, then inform him or her of the shortcomings it causes. In a lot of cases, they then agree (after a lot of wasted time) that I should do it the way I had it. Sometimes, though, pride gets in their way, and they make me put out the stupid code into production - problems and all.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups