C++ reflection
-
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?
-
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?
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.14Mircea
-
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?
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
-
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.14Mircea
-
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
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 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.
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
-
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
-
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
-
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
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.