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. I can't stand them, but I need them - nested template instantiations

I can't stand them, but I need them - nested template instantiations

Scheduled Pinned Locked Moved The Lounge
c++csharpwpfjsonquestion
10 Posts 3 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 Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #1

    The STL does this too but it's annoying. You wind up declaring C++ templates that package a bunch of template parameters together as a unit, and then pass that as an argument to another template, and so on.

    rgb_pwm<
    rgb_pwm_group< // declares one rgb pulse width modulation group
    pwm_traits<32,0>, // Red: GPIO pin # 32, PWM channel 0
    pwm_traits<25,1>, // Green: GPIO pin # 25, PWM channel 1
    pwm_traits<26,2> // Blue: GPIO pin # 26, PWM channel 2

    p;

    If that isn't the ugliest elephanting thing outside of preprocessor macros, I'm not sure what is. Sure you can use typedef/using, but then you just end up adding even more lines of code. I don't know what I want though. I guess JSON wouldn't be much more brief. I do kinda wish C++ allowed you to explicitly pass your arguments by name like you can do in C#. That way this would be at least a little more legible, if verbose.

    To err is human. Fortune favors the monsters.

    M 1 Reply Last reply
    0
    • H honey the codewitch

      The STL does this too but it's annoying. You wind up declaring C++ templates that package a bunch of template parameters together as a unit, and then pass that as an argument to another template, and so on.

      rgb_pwm<
      rgb_pwm_group< // declares one rgb pulse width modulation group
      pwm_traits<32,0>, // Red: GPIO pin # 32, PWM channel 0
      pwm_traits<25,1>, // Green: GPIO pin # 25, PWM channel 1
      pwm_traits<26,2> // Blue: GPIO pin # 26, PWM channel 2

      p;

      If that isn't the ugliest elephanting thing outside of preprocessor macros, I'm not sure what is. Sure you can use typedef/using, but then you just end up adding even more lines of code. I don't know what I want though. I guess JSON wouldn't be much more brief. I do kinda wish C++ allowed you to explicitly pass your arguments by name like you can do in C#. That way this would be at least a little more legible, if verbose.

      To err is human. Fortune favors the monsters.

      M Offline
      M Offline
      megaadam
      wrote on last edited by
      #2

      I bet I am missing the point but how about something like std::map pwmPins { {32, 0}, {25, 1}, {26, 2}, }; And of you have a bunch of these also create a type alias using PinMap = std::map

      "If we don't change direction, we'll end up where we're going"

      H 1 Reply Last reply
      0
      • M megaadam

        I bet I am missing the point but how about something like std::map pwmPins { {32, 0}, {25, 1}, {26, 2}, }; And of you have a bunch of these also create a type alias using PinMap = std::map

        "If we don't change direction, we'll end up where we're going"

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

        Ummm, no. I'm not using a map and relegating all of my microsecond fast pin operation code to a hash table. Compile time and runtime are not the same thing. Not even a little bit. Edit: Sorry if I came across as hostile. I didn't mean to be. It's early and my conversation skills are a bit dodgy right now.

        To err is human. Fortune favors the monsters.

        M 2 Replies Last reply
        0
        • H honey the codewitch

          Ummm, no. I'm not using a map and relegating all of my microsecond fast pin operation code to a hash table. Compile time and runtime are not the same thing. Not even a little bit. Edit: Sorry if I came across as hostile. I didn't mean to be. It's early and my conversation skills are a bit dodgy right now.

          To err is human. Fortune favors the monsters.

          M Offline
          M Offline
          megaadam
          wrote on last edited by
          #4

          Makes perfect engineering sense, and I did not read it as unfriendly.

          "If we don't change direction, we'll end up where we're going"

          1 Reply Last reply
          0
          • H honey the codewitch

            Ummm, no. I'm not using a map and relegating all of my microsecond fast pin operation code to a hash table. Compile time and runtime are not the same thing. Not even a little bit. Edit: Sorry if I came across as hostile. I didn't mean to be. It's early and my conversation skills are a bit dodgy right now.

            To err is human. Fortune favors the monsters.

            M Offline
            M Offline
            megaadam
            wrote on last edited by
            #5

            How about this then? After the template is built the table definitions become neat. constexpr-map/[^]

            "If we don't change direction, we'll end up where we're going"

            H 1 Reply Last reply
            0
            • M megaadam

              How about this then? After the template is built the table definitions become neat. constexpr-map/[^]

              "If we don't change direction, we'll end up where we're going"

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

              That *would* be nice, but I don't think it's available under the Arduino framework necessarily - on many platforms The STL is only partially implemented. I'd probably have to reimplement much of that instead of using the std:: namespace. Not out of the realm of possibility. I'll poke at it. Thanks!

              To err is human. Fortune favors the monsters.

              M 1 Reply Last reply
              0
              • H honey the codewitch

                That *would* be nice, but I don't think it's available under the Arduino framework necessarily - on many platforms The STL is only partially implemented. I'd probably have to reimplement much of that instead of using the std:: namespace. Not out of the realm of possibility. I'll poke at it. Thanks!

                To err is human. Fortune favors the monsters.

                M Offline
                M Offline
                megaadam
                wrote on last edited by
                #7

                When I get fed up with all this magic stuff I ask myself: Alrite, how would I do it in plain old C ?

                "If we don't change direction, we'll end up where we're going"

                H 1 Reply Last reply
                0
                • M megaadam

                  When I get fed up with all this magic stuff I ask myself: Alrite, how would I do it in plain old C ?

                  "If we don't change direction, we'll end up where we're going"

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

                  You'd most likely have to use a ton of preprocessor macros. :(

                  To err is human. Fortune favors the monsters.

                  D 1 Reply Last reply
                  0
                  • H honey the codewitch

                    You'd most likely have to use a ton of preprocessor macros. :(

                    To err is human. Fortune favors the monsters.

                    D Offline
                    D Offline
                    David ONeil
                    wrote on last edited by
                    #9

                    If you have to go more than three 'if' levels deep - :elephant: that! Even two levels looks like something I never want to eat! I feel for the MS guys who keep up their #ifdefs in the Windows headers! X|

                    Our Forgotten Astronomy | Object Oriented Programming with C++

                    H 1 Reply Last reply
                    0
                    • D David ONeil

                      If you have to go more than three 'if' levels deep - :elephant: that! Even two levels looks like something I never want to eat! I feel for the MS guys who keep up their #ifdefs in the Windows headers! X|

                      Our Forgotten Astronomy | Object Oriented Programming with C++

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

                      The problem in this case is you can have up to 5 RGB LEDs you can control, each requiring 3 pins. The above was just for one LED. Variadic template arguments are how I accomplish that. It would be really messy to do that with the preprocessor. It's doable I think, but it requires a lot of nonsense.

                      To err is human. Fortune favors the monsters.

                      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