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. General Programming
  3. C / C++ / MFC
  4. C++ reflection

C++ reflection

Scheduled Pinned Locked Moved C / C++ / MFC
c++comdebuggingquestionannouncement
9 Posts 3 Posters 34 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.
  • J Offline
    J Offline
    jschell
    wrote on last edited by
    #1

    From CP newletter C++ 26 committee progresses new features including contracts and 'desperately needed' reflection • DEVCLASS[^] Just curious. C++ binaries already do or at least can have meta data right about the classes/methods? Thought that was added a long time ago? (Not debug info I don't think.) If so is reflection here just a standard way to get to that. Or is going to add additional or even new meta data?

    Mircea NeacsuM honey the codewitchH 2 Replies Last reply
    0
    • J jschell

      From CP newletter C++ 26 committee progresses new features including contracts and 'desperately needed' reflection • DEVCLASS[^] Just curious. C++ binaries already do or at least can have meta data right about the classes/methods? Thought that was added a long time ago? (Not debug info I don't think.) If so is reflection here just a standard way to get to that. Or is going to add additional or even new meta data?

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      AFAIK there is no reflection in C++. You cannot have something like:

      struct S{
      int value;
      double number;
      }
      //...
      S to_serialize{1, 3.14};
      std::string tag;
      stream strm;

      //fantasy code below
      for (auto& m: members_of(S))
      {
      strm << name_of(m) << ': ';
      strm << to_serialize.m << std::endl;
      }

      In a language with reflection this would produce an output like:

      value: 1
      number: 3.14

      Mircea

      J 1 Reply Last reply
      0
      • J jschell

        From CP newletter C++ 26 committee progresses new features including contracts and 'desperately needed' reflection • DEVCLASS[^] Just curious. C++ binaries already do or at least can have meta data right about the classes/methods? Thought that was added a long time ago? (Not debug info I don't think.) If so is reflection here just a standard way to get to that. Or is going to add additional or even new meta data?

        honey the codewitchH Online
        honey the codewitchH Online
        honey the codewitch
        wrote on last edited by
        #3

        RTTI is the only metadata functionality I'm aware of in C++. There are hacks to sort of divine some reflective type information at compile time like SFINAE but not at runtime excepting RTTI i think. But then the only thing I really have worked with is C++17 and prior, so if they have introduced something besides since I wouldn't know.

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

        J 1 Reply Last reply
        0
        • Mircea NeacsuM Mircea Neacsu

          AFAIK there is no reflection in C++. You cannot have something like:

          struct S{
          int value;
          double number;
          }
          //...
          S to_serialize{1, 3.14};
          std::string tag;
          stream strm;

          //fantasy code below
          for (auto& m: members_of(S))
          {
          strm << name_of(m) << ': ';
          strm << to_serialize.m << std::endl;
          }

          In a language with reflection this would produce an output like:

          value: 1
          number: 3.14

          Mircea

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Mircea Neacsu wrote:

          AFAIK there is no reflection in C++.

          Ok? That of course makes sense given that the link I posted is a about a new feature that will be added to the next standard. Kind of silly to add it if it already existed.

          1 Reply Last reply
          0
          • honey the codewitchH honey the codewitch

            RTTI is the only metadata functionality I'm aware of in C++. There are hacks to sort of divine some reflective type information at compile time like SFINAE but not at runtime excepting RTTI i think. But then the only thing I really have worked with is C++17 and prior, so if they have introduced something besides since I wouldn't know.

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

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            honey the codewitch wrote:

            RTTI

            Yeah that is the one that I was remembering. So then that is not sufficient perhaps? Limited in some way? In Java/C# one can get to things like class name, method names (public and private), method parameters (types), access types, class attributes. All of those can be listed and manipulated. So for example if a class attribute is private and modifying that is going to help with better unit testing one can write reflection code to modify it. That is the most common use I have used for accessing private attributes. I don't use it that much and seems like bit of a wash versus adding a setter/getter.

            honey the codewitchH 1 Reply Last reply
            0
            • J jschell

              honey the codewitch wrote:

              RTTI

              Yeah that is the one that I was remembering. So then that is not sufficient perhaps? Limited in some way? In Java/C# one can get to things like class name, method names (public and private), method parameters (types), access types, class attributes. All of those can be listed and manipulated. So for example if a class attribute is private and modifying that is going to help with better unit testing one can write reflection code to modify it. That is the most common use I have used for accessing private attributes. I don't use it that much and seems like bit of a wash versus adding a setter/getter.

              honey the codewitchH Online
              honey the codewitchH Online
              honey the codewitch
              wrote on last edited by
              #6

              RTTI is extremely limited last time I checked. Given I've maybe used it once to play around with it years ago and that's it, so don't quote me. It's nothing like what .NET offers, for example.

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

              J 1 Reply Last reply
              0
              • honey the codewitchH honey the codewitch

                RTTI is extremely limited last time I checked. Given I've maybe used it once to play around with it years ago and that's it, so don't quote me. It's nothing like what .NET offers, for example.

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

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                Ok. But a brief reading of the link I posted suggests that will changing.

                honey the codewitchH 1 Reply Last reply
                0
                • J jschell

                  Ok. But a brief reading of the link I posted suggests that will changing.

                  honey the codewitchH Online
                  honey the codewitchH Online
                  honey the codewitch
                  wrote on last edited by
                  #8

                  Personally, I'd probably still avoid it unless it solved a very significant problem for me because I like being able to make 17KB dlls that are actually useful. Metadata ain't free.

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

                  J 1 Reply Last reply
                  0
                  • honey the codewitchH honey the codewitch

                    Personally, I'd probably still avoid it unless it solved a very significant problem for me because I like being able to make 17KB dlls that are actually useful. Metadata ain't free.

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

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    honey the codewitch wrote:

                    I'd probably still avoid it unless it solved a very significant problem

                    Specific times I have used it in other languages. - Dynamic loading of classes. Especially when parameter lists are dynamic. - Messing with private data of a class. Most the time for unit tests, but I think one time it was in the production code. In C++ there is a way to do that anyways but it always risky and just as hacky.

                    honey the codewitch wrote:

                    Metadata ain't free.

                    Which is why it has taken so long I suspect.

                    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