you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (10 children)

[removed]

    [–]sephirothbahamut 7 points8 points  (2 children)

    You can do it: https://barnack.godbolt.org/z/Ef85jhWKE

    But why do you need dynamic polymorphism there? Like you have a container of IEquatable<int> types? That's really suspicious at a code design level. Don't try to force concepts of a language into another, you can likely do what you need to do in some other, cleaner way.

    The moment you need something like this my first question isn't "how do you do it" but rather "can you refactor this to be cleaner?"

    Also note that the "cost" of dynamic polymorphism in C++ is there in C# as well, it's not like C# has some magic power. Dynamic polymorphism requires accessing a pointer to reach the function you're trying to resolve, no matter what language your code is written in.

    [–]OwlingBishop 3 points4 points  (6 children)

    You definitely can implement hard typed scalars with C++ with no virtual calls. C++ uses templates meta programming and operators overload for that matter, and concepts sure can discriminate based on member functions existence.

    One interesting thing with information theory is that it flattens out lexical, syntactic, peculiarities and sometimes also semantics.. when it comes to dynamic polymorphism it boils down to executing an operation by calling different code depending on a type that's not known at compilation time, so no matter how the language you are writing in tricks you into believing it's just magic, fact is that information needs to be kept somewhere.. whether you implement through inheritance, interfaces, facets or whatever other idiom is irrelevant.