What is the general feeling about reflections in std :: type_info?

I noticed that reflection is one of the features that developers from other languages ​​really miss in C ++. For some applications, I really can understand why! It's much easier to write things like IDE autocomplete if you have reflection. And of course, serialization APIs would be easier in the world if we had it.

On the other hand, one of the basic principles of C ++ is not to pay for what you are not using. It makes sense. This is what I love about C ++.

But it occurred to me that there could be a compromise. Why don't compilers add extensions to the std::type_info ? There was no overhead. The binary may be larger, but it can be a simple compiler to turn on / off and, to be honest, if you are really worried about saving space, you will probably turn off exceptions and RTTI.

Some people cite problems with templates, but the compiler happily generates std::type_info for template types already.

I can imagine a g ++ switch like -fenable-typeinfo-reflection , which can become very popular (and in major libraries like boost / Qt / etc, it can be easy to check the code that uses it, if any, and in in this case, the end user will benefit at no cost than switching the switch). I do not think this is unreasonable, as large portable libraries like this already depend on compiler extensions.

So why is this not so common? I guess I missed something, what are the technical issues with this?

EDIT: Only a few re-the bloat metrics:

I looked at a rather large Qt project (about 45,000 LoC) and measures the size of meta objects. I think this is a reasonable metric because the Qt moc system is a fairly comprehensive reflection system (types, functions, enumerations, members, and a few special Qt concepts, such as “properties”). There were 67 meta objects in total, therefore not a trivial amount, but nothing crazy , which added up to 5479 bytes . However, almost all of them were 32 bytes or less ( the largest of which was 1427 bytes ). Given that modern compilers produce binaries higher than 4K for even the simplest program, these numbers are not outrageous). Although I would like to see something like this in relation to STL , to see how it looks.

+4
source share
3 answers

Typically, the use of reflection indicates poor software design; the proper use of interfaces and polymorphism is enough to do whatever you do with reflection. If someone added additional information to std :: type_info, this would really lead to bloating the program. The problem with templates is not that you cannot generate std :: type_info from them, but rather because you can get a type explosion, so each template creation leads to another std :: type_info object, which you need it. Your suggestion about using the compiler switch really does not help or makes sense ... first of all, the standard will never indicate the compiler, because it will be implementation specific, but assuming that it needs to be done ... what happens if you want to use reflection with a class that came from a library for which reflection was disabled? If reflection was turned off in most libraries, which would probably be possible, then this would seriously limit the usefulness of this function, and if most libraries did not turn it off, you would pay for it without using it.

+1
source

Instead of imposing a specific approach at runtime on me, I would rather compile time reflection capabilities that I could use to generate the information I need.

Thus, I would not have to pay for information that I do not actually use, and could use metaprogramming to generate interaction code if necessary without additional steps of preliminary assembly or development of a complex declarative EDSL.

Kaspin mentioned one possible approach here . Honestly, I am surprised that the topic seems to have never been put forward as a proposal for inclusion in C ++.

+1
source

Perhaps many types in C / C ++ make programming flexible. A hypothetical database or serialization interface capable of handling any primitive types will require special cases for [unsigned] int, short, [long] long, char, float, [long] double, arrays, related functions, etc. This will mean an interface called mangler.

Not that I think this is a good reason. A few years ago, I wrote a SQL reflective interface in Boost MPL, and that was unpleasant. It would be much better and easier with a "native" object.

I think most people get thought with extra preprocessing, such as the Apple Mach Interface Generator and similar tools in GNU and Microsoft.

More unexpected to me than the lack of support in C ++ is that in promising languages ​​that stimulate competition, such as D and (in spirit), go.

0
source

Source: https://habr.com/ru/post/1304488/


All Articles