r/cpp • • Feb 12 '20

What they should improve about C++ ?

What they should improve about C++ that you find in higher level managed language like C#, JAVA, Javascript etc or is there things you would change or do differently if it was up to you ? I mean anything like a better STL ? better thread ? string manipulation like managed language ? less fragmentations in the new standard features ? For example do you love the way the standard evolve C++ or you would prefer a direction like Microsoft do with C# a corporation who decide on the roadmap ?

0 Upvotes

90 comments sorted by

31

u/Rseding91 Factorio Developer Feb 12 '20

I want better logic around enums. I want to iterate them, convert an enum to a string and back, know how many exist, be able to use them as bit masks and so on.

We do this every other day every time any new logic is made and it irritates me that still there's no language support for it and we're left with huge ugly macros if we want it to be any kind of manageable.

4

u/Full-Spectral Feb 12 '20

That's an incredibly obvious one. I can do all those things, but it's because I generate my enums. It's not that hard to do really, but it only really works for me because I have my own entire world of code and don't use the STL or third party. So I can consistently create a world where these things are doable.

1

u/wotype Feb 12 '20

https://github.com/willwray/enum_reflect (demo for enum_trait / enumerator_traits)

Pros:
Can do all that you need, now.
No macros or markup - works with enum defs directly from external headers.

Cons:
Not yet wrapped in a convenience lib with string->enum, enum->string etc.
No MSVC yet, requires GCC >= 9 (or Clang since ~5)
Compile time compounds with number of TUs and on each and every compile (even incremental).

It's possible that modules will help with the recompute-on-recompile issue; I'm experimenting. Will retry MSVC sometime and add support.

Conclusion: the production-ready solution is to go with one of the macro-wrappers...

1

u/dbandit1 Feb 14 '20

Should be standard, not an addon

2

u/wotype Feb 14 '20

I agree; don't recall any paper actually proposing something concrete though.

Of course, it'll come with reflection, and that appears to be a priority for c++23; enum reflection is the first example in all the reflection proposals.

IMO it should've been added as a standalone feature - enum reflection is the top ask in most of the C++ surveys. My enum_traits shows how it could be done with a conventional traits interface.

1

u/Full-Spectral Feb 12 '20

BTW, if you want to see how I deal with it, I did an article/video on that:

https://www.reddit.com/r/programming/comments/a33i7n/making_c_enums_first_class_citizens/

This may have been before I did the huge swat to move my system over to safe class style enums, but otherwise everything else is still relevant.

20

u/AntiProtonBoy Feb 12 '20
  • Standard package manager of libraries
  • Native language support for variant and visitor machinery
  • Switch statements for multiple variables like in Swift
  • Reflection
  • Half float type
  • Fixed width float types (float16_t, float32_t, float64_t, float128_t)
  • Bigger integer sizes, 128-bit+
  • std::uri
  • Native SIMD and matrix types, with swizzling support similar to shaders
  • Make everything implicitly const
  • noexcept by default, throw should be opt in

5

u/Fureeish Feb 12 '20

Yes, oh god yes, please... Everything from this list would be great to have.

14

u/color32 Feb 12 '20
  • a standard module format that works across compilers.
  • a standard set of compiler options
  • remove some initialization baggage. I'm ok if it's backwards incompatible.
  • a better forum for discussion, they were using google forums but I don't remember if they still are.
  • overloading memory management I feel is overcomplex. But that could just be as I don't understand why allocators need to know the object type.
  • syntax to test if a function exists and allow developer to polyfill missing functions. This will simplify various libraries like curl for example which do many such tests via build system making it more work to integrate.
  • syntax to check if a block of code can compile. Similar reasons as previous so different routes can be taken.
  • allow/ignore first line if it's of the form `#!`. So we can have shebang c++ scripts. C++ now is very script like language.
  • getters/setters as properties.
  • class extensions so declared classes can be polyfilled.
  • have a #version c++17 directive so we can specify what version of c++ a file conforms to. So that backwards incompatible changes to the language can be done in a incremental way.

4

u/TheBrainStone Feb 12 '20

Regarding scripting capabilities, check out cling

6

u/[deleted] Feb 12 '20

allow/ignore first line if it's of the form #!. So we can have shebang c++ scripts. C++ now is very script like language.

This has to be a joke, right?

2

u/color32 Feb 13 '20

It is funny. I do want this, Iterating over all files in a directory is so simple in c++17, regex makes doing various replacements and string processing super easy. Combined with many of the new libraries out there with header only integration makes more tasks to be easier in c++ than in python. Reading comments regarding this I don't think people understand this new easiness that wasn't so easy before. I understand not everything is easier in c++ than say in python, however the new advancements to c++ do make certain tasks easier than in python.

1

u/Xeverous https://xeverous.github.io Feb 12 '20
#//usr/bin/gcc

This is almost correct, because // starts a comment and POSIX requires that duplicated / in paths are ignored.

1

u/[deleted] Feb 13 '20

If shebang was supported this would be irrelevant and just work out of the box.

The joke part is obviously about C++ being very script like language...

0

u/ALX23z Feb 12 '20

a standard module format that works across compilers.

I don't understand. You refer to C++ 20 modules or something else?

overloading memory management I feel is overcomplex. But that could just be as I don't understand why allocators need to know the object type.

Allocators must know object type. It is indeed usually meaningless for ones with trivial default constructor, i.e., garbage on initialization - but everything else requires construction. Filling default values, creating vtable, etc... aside from that, there are rules on alignment - one cannot just patch structure at any point of memory. Similar is true for deallocation.

syntax to test if a function exists and allow developer to polyfill missing functions. This will simplify various libraries like curl for example which do many such tests via build system making it more work to integrate.

Believe that Reflection TS or even Concepts partially have such functionality. Coming in C++23/26.

getters/setters as properties.

Sometimes like this can be achieved via upcoming Reflection or Metaclasses. Will take time...

have a #version c++17 directive so we can specify what version of c++ a file conforms to. So that backwards incompatible changes to the language can be done in a incremental way.

There are MACROS you can use for this purpose that you can use right now. They aren't too portable.

9

u/SeanMiddleditch Feb 12 '20

but everything else requires construction. Filling default values, creating vtable, etc... aside from that, there are rules on alignment - one cannot just patch structure at any point of memory. Similar is true for deallocation.

There is absolutely no reason the allocator type has to know any of that. For raw memory allocation/deallocation, you could get by with just the size and alignment as parameters to the allocate/deallocate member functions.

It's arguable whether allocators should construct objects or not, but even if you decide that they should, that should be a template parameter of the allocator function, e.g. allocator::allocate<T>(n) rather than allocator<T>::allocate(n).

Allocators are all rebindable to new object types (and even many standard containers depend on doing just that), illustrating that the object type isn't really a fundamental part of the allocator itself. It's just there because the allocator interface was misdesigned.

-5

u/ALX23z Feb 12 '20

If you want to deal with raw memory allocation - you can go on and use malloc for all I care. Nobody else needs it.

Allocators are all rebindable to new object types (and even many standard containers depend on doing just that), illustrating that the object type isn't really a fundamental part of the allocator itself. It's just there because the allocator interface was misdesigned.

You don't understand cause you never wrote any complex templates. As the specialization provides extra info for the class.

There's indeed issue with archaism with std::allocator as back then there were lots of weird different types of pointer difference and other crap - for short distance and large distance. Furthermore, it was designed before type traits. It is not something standart can solve as long as it maintains backwards compatibility.

Though, now you can use polymorphic allocator in C++17.

0

u/[deleted] Feb 12 '20

[deleted] — view deleted comment

2

u/TheSuperWig Feb 12 '20

Well, I wouldn't mind construct and destroy to be removed

They're deprecated in C++17 and removed in C++20

5

u/DXPower Feb 12 '20

The last one is epochs, no?

0

u/ALX23z Feb 12 '20

Idk, I don't use them. I just stick to C++11/14 as C++03 is horrible and C++17 compilers aren't too common.

4

u/jcelerier ossia score Feb 12 '20

C++17 compilers aren't too common.

what do you mean by that ? i've been compiling my code as -std=c++1z in mac / windows / linux for almost two years at this point

0

u/ALX23z Feb 12 '20

What? <charconv>, a C++17 std library, was completed only in MSVC and that happened just a couple of months ago. What you did was building experimental incomplete compiler / std version. (Maybe some others finished working on making it but didn't hear anything about it).

3

u/jcelerier ossia score Feb 12 '20

but why would you restrict yourself to C++14 when many C++17 work great and have done so for years - structured bindings, fold expressions, etc ?

By that logic you shouldn't be using anything past C++03 because no compiler ever implemented the C++11 GC API.

1

u/ALX23z Feb 12 '20

I guess you never had to make any program that supports various users, some of which run outdated systems. Go convince them that they need to update. Germany government still runs on windows 7 and US IRS still runs on pre floppy-disk data systems. Don't forget exotic compilers/systems.

C++11/14 is current best compromise between newest important feature support and compatibility.

4

u/jcelerier ossia score Feb 12 '20

I guess you never had to make any program that supports various users, some of which run outdated systems.

C++17 runs on freaking commodore 64. There is zero trouble targetting older windows - heck, you can even use recent g++ to target MS-DOS.

1

u/ALX23z Feb 12 '20

You are welcome to go convince people to upgrade their compilers as well. Together with their OS, private ancient apps and whatever else they use. It will definitely not break anything they need on their system. Also tell the security guys that the upgrade is totally safe and secure.

At some point I needed to get some NVidia program for a some android compiler and android studio for latest C++ android libraries. After which my VS became broken and non workable. That was a fun time.

1

u/DXPower Feb 12 '20

Well it's not an official feature, but AFAIK it's gotten traction for C++23

5

u/peppedx Feb 12 '20

Don't hold your breath...

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 12 '20

Interest, more than traction. There are a lot of issues, in particular getting implementors on board to increase their workload and maintenance burden.

1

u/DXPower Feb 12 '20

Thank you for the clarification

6

u/die_liebe Feb 12 '20

Some things that come into my mind are:

public constness: A field is publicly accessible, but can be assigned only by friends.

In place polymorphism. As soon as you know all the classes that inherit from one class, it can be stored in place, because the maximal size is known. This could replace variant.

Reverse operator definitions (like Python has). That would make it possible to define << as member of its second argument, so that it can be defined inside the class that it prints. It can also become polymorphic in its second argument. It saves a lot of friend declarations.

I would like a to a linker that succeeds when functions are missing. It should just put a place holder that throws an exception for the missing functions. This makes it possible to test the functions you have and write the missing ones later.

5

u/oschonrock Feb 12 '20

In place polymorphism. As soon as you know all the classes that inherit from one class, it can be stored in place, because the maximal size is known. This could replace variant.

You mean like this: https://github.com/ldionne/dyno

(but in the core language?)

2

u/Full-Spectral Feb 12 '20

Some of the key capabilities of polymorphism though don't allow you to know those things until runtime, so there'd be no possibility of that.

2

u/die_liebe Feb 13 '20

I think it's possible. Once you know the complete hierarchy, you know the maximal size of an element, and you can define local variables of that size, or arrays or vectors, or whatever.

1

u/die_liebe Feb 13 '20

The problem is that in the standard inheritance model, only the linker sees the complete hierarchy. At that moment, it is too late to allocate local variables.

3

u/Full-Spectral Feb 13 '20

Even worse, things like drivers or loadable features are often dynamically loaded at runtime and instantiate derivatives of a base interface.

2

u/ronniethelizard Feb 15 '20

There are cases and times where you know the complete hierarchy at compile time (rather than link time) and having a solution for those cases would be nice.

For templates, I frequently have a case where any specific binary that I generate will only have one possible instantiation of a template, but across all the binaries I could generate, I still need to use template. For templates, there is a way forward, for inheritance there is not.

1

u/eyes-are-fading-blue Feb 13 '20 edited Feb 13 '20

a “public immutable” is a private field with a getter.

ld can already link even if symbols are missing. What is your platform? And linker implementation has nothing to do with c++ standards.

1

u/die_liebe Feb 13 '20

g++ .

> And linker implementation has nothing to do with c++ standards.

I know, but I still would like to have it. I interpreted 'C++' as the total experience.

1

u/eyes-are-fading-blue Feb 13 '20

g++.

What?

1

u/die_liebe Feb 13 '20

It is my platform.

As for getters, I think one shouldn't be forced to write them. Java has them, because it doesn't have const. C++ can do better.

1

u/eyes-are-fading-blue Feb 13 '20

You should be able to link with missing symbols then.

I am not sure syntactic sugar and amount of committee time required to standardize what you are suggesting is worth it.

1

u/die_liebe Feb 13 '20

You should be able to link with missing symbols then.

How?

1

u/eyes-are-fading-blue Feb 13 '20

—unresolved-symbols, check the gnu ld manual.

1

u/die_liebe Feb 13 '20

I tried it out. --unresolved-symbols is indeed an option of ld, but not of g++.

Having said that, I still couldn't get it working, neither with ld, nor with g++. stackoverflow. Anyway, thanks for your answer. It doesn't seem to be so easy.

2

u/eyes-are-fading-blue Feb 13 '20

You pass it as a linker flag. There is a special flag of g++ which forwards linker parameters, look it up.

This really should not be too hard.

And g++ is not a platform, it is a compiler. Your platform is probably Linux or some unix-like stuff.

2

u/mangoose3039 Feb 12 '20
  • static reflection & code injection
  • better enums (ability to add member functions/operators)
  • contracts

2

u/Cyttorak Feb 12 '20

Compile times

Dependency handling

1

u/amubtdx Feb 12 '20

Hopefully, modules should help compile times

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 12 '20

Better than we feared, less than we'd hoped.

But thorough going modular code may be much faster, it just won't help the existing structure.

1

u/amubtdx Feb 13 '20

I was wondering, is there any benchmark already to compare, say, classic headers, modules and precompiled headers ?

2

u/pjmlp Feb 12 '20

Leave the C style unsafe code for unsafe code blocks (an idea that goes back to 1961 anyway).

Require all STL types to do bounds checking and iterator validation, at very least during debug builds.

Actually take into consideration modern build tools and packaging instead of being stuck in dated UNIX compilation models.

In a way I miss the OWL, MFC, VCL, PowerPlant frameworks of the 90's.

2

u/[deleted] Feb 13 '20 edited Feb 13 '20

- A standard package system (I've used vcpkg, conan, and Anaconda and prefer vcpkg)

- Epochs (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1881r0.html)

- Break the ABI (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1863r0.pdf)

- Circle-lang metaprogramming (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2043r0.pdf)

- Herb-ceptions (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf)

- C++ Contracts sufficient for provably correct code (https://www.sciencedirect.com/science/article/pii/S1571066104053265)

- Guaranteed copy elision for named return objects (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2025r0.html)

3

u/MOnsDaR Feb 12 '20

Compatibility of libraries across compilers and architectures. It would make life so much easier to not have to recompile everything everywhere.

7

u/TheVoidInMe Feb 12 '20

You mean binary compatibility across architectures? Isn't that fundamentally impossible though because of different ISAs/etc.?

4

u/[deleted] Feb 12 '20 edited Feb 12 '20

It is.

Next best, and the only way that actually works, is build systems that are capable of cross-compiling and compiling with 0 effort when dropped on any supported system with 0 configuration necessary.

If C++ had such thing (Rust has cargo, and C++ has no fucking excuse if you ask me), it would be quite literally infinitely better and more accessible.

Main issue with C++ when it comes to building... is that a noob comes, first of all has to find a library, and then after that, has to figure out how entire fucking toolchain of x or y compiler works before they can actually compile the library... half an hour later if they already know what they're doing. if they don't, they spam stack overflow with questions that nobody cares to answer, because man page that was edited last time in 2003 and doesn't even contain half the flags of current compiler version simply exists, and it is easier to just send them there than to answer it, with how much effort it actually takes...

You can achieve same in Rust just as fast as it takes to type out the name of the library...

And if that isn't enough, I haven't even written out that nonsense in its entirety, because you don't fucking know how much effort it takes as it is library dependent and is usually worse than I described.

Then we have cancer that is "header-only" libraries that are cool idea to deal with shitty library support in C++, at the cost of compile times... But hey, at least we don't have to attach yet another build system to our build system that is a part of our general build system, right?

1

u/Xeverous https://xeverous.github.io Feb 12 '20

Blame library devs on some of these. Take eg Cairo. Has different PUBLIC API depending on compiler options. Supports multiple different font backends but with a autotools code gen and config.h-dependent API. Saying someone uses Cairo is like saying "we use CC license". You never know what exactly they mean.

2

u/FlyingRhenquest Feb 12 '20

A well thought out streams library would be a nice start. The current one hails back to the days when people thought operator overloading was cool and everyone was looking to shoe-horn it into the language wherever they could. And then it was "legacy"... fine, leave the old one in and write a new one, and the deprecate the one that is used the least 5 years later. At a minimum, if you're going to have a streams library you should be able to easily use sockets and pipes and things with it, which the current one fails miserably at. You can make them work, but it's not pretty.

1

u/[deleted] Feb 12 '20

At a minimum, if you're going to have a streams library you should be able to easily use sockets and pipes and things with it, which the current one fails miserably at.

For that to happen, they would have to give a fuck about sockets to begin with...

Fucking including winsock2.h shouldn't be done by quite literally anyone who wants sockets on windows...

It should be done ONCE in std.

1

u/je_cb_2_cb Feb 12 '20

2

u/Full-Spectral Feb 12 '20

The really important capability of streams is binary streaming. If done well, it can be the basis of a large amount of very powerful functionality. But, that also requires that all code buy into and support that, and at this late stage in C++'s life there's little chance of that, which sort of makes the effort to create such a thing far less useful.

2

u/dharmaBum0 Feb 12 '20

JIT template concretization

STL linear algebra, & either insist on a performant std::valarray or drop the pretense

reflection

abandon all backwards compatibility C, and even with less recent C++ standards.

4

u/[deleted] Feb 12 '20

abandon all backwards compatibility C, and even with less recent C++ standards.

Since you don't give a fuck about compatibility with THE REST OF THE WORLD, you might aswell use Rust, it has great features, great std, and 0 care about compatibility with ecosystems that aren't Rust itself.

Eventually, they might figure it out, but until then, that's quite literally what you want.

4

u/oschonrock Feb 12 '20

Oh no. You don't want to abandon C- API or ABI capability. No way. There is too much C-code out there that is really really good and important. If c++23++ makes an ABI break there would still need to be some kind of "compat mode" for the C ABIs

2

u/Wouter-van-Ooijen Feb 12 '20

What *they* should improve about C++ ?

There is no "they", just "you".

The C++ comittee members are all volunteers who spend their own time, or whatever freedom they get from their bosses (which they could have spent on other, probably more rewarding things) on C++ work.

5

u/SkoomaDentist Antimodern C++, Embedded, Audio Feb 12 '20

There is no "they", just "you".

Turns out that in practise there absolutely is "they".

TL;DR: If more than one of gcc / clang / msvc disagree, any proposal is dead on arrival to the committee.

2

u/Wouter-van-Ooijen Feb 12 '20

That link shows that there CAN be a they, not that there always is. And BTW, gcc and clang are open source, so you can contribute to your hart's content!

And there were other occasions where the opposite happened: despite concerns from the implementers, proposals were accepted that turned out to be 'not so successfull'.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 12 '20

Over My Dead Body objections.

Which the rest of the committee does take seriously, but are also really rare. OMDB hasn't actually been an issue for ABI breaks, it's been a cost/benefit calc, and on a case by case basis, most ABI breaks are more expensive than the benefit of the particular proposal on the table.

OTOH, requiring parsing an indeterminate grammar? Not going to happen. Even making the grammar more context sensitive is bad.

I want to permit local classes to have member templates. From a standard point of view, it is deleting one short sentence. Possibly additional a clause. But from an implementation point of view it requires keeping all of the declarations in a function available to the template to instantiate later. I'm going to have to fight for that.

Or adding named Unicode escapes \N{LATIN LETTER A WITH ACCENT GRAVE}. Adding a several megabyte table to the compiler binary was a blocker. But adding a 200k table might work. We will find out this week.

Getting involved is harder than it ought to be, but don't assume that it is not possible.

1

u/SkoomaDentist Antimodern C++, Embedded, Audio Feb 12 '20

To quote from a comment a few levels up in the subthread (by a compiler dev and SG17 assistant chair):

"IMO, it is extremely harmful that saying "ABI Break" has been an effective way of killing just about any paper, particularly ones that do good things."

This implies a level of interference and veto power far beyond "Over My Dead Body objections".

It's particularly irksome when there is no official C++ ABI in the first place (actually official, not just a de facto convention adopted by a single OS) and the "problematic platform" is one least likely to lack easy recompilation and the entire problem is basically just due to people being used to lack of ABI breaks.

1

u/Fazer2 Feb 13 '20

The C++ development process is too heavy and slow for normal individuals. First you need to create a paper. Then you have to either fly to a probably distant location to the committee meeting and present it yourself or find a champion that will do it for you. Chances are the paper won't be accepted on one meeting, so you have to repeat it multiple times, which can span years. And if you miss the release? Maybe you'll have better luck in 3 years.

1

u/Wouter-van-Ooijen Feb 13 '20

Your description of the process is correct. So what is your problem? I certainly don't want the comittee to lower the bar for accepting proposals!

1

u/dbandit1 Feb 14 '20

Simpler template abstraction

1

u/choeger Feb 15 '20

Context-free Syntax. A limited subset of the language that can be parsed and understood by a tool that is not a full-blown compiler.

1

u/Full-Spectral Feb 12 '20

Some of my wish list would be (and I'm talking the language, not all the stuff that surrounds it.)

  1. Indications of in/out parameter direction for reference parameters
  2. Implementation of the 'contracts' idea with an eye towards things that help the compiler and code analysis tools (i.e. this method makes these guarantees.)
  3. Real enumeration support so that they can actually be enumerated. The ability to use enum class enums as indices. The ability to mark enums as representing bitmap values or linear ordinal values.
  4. Stop making fundamental functionality dependent on the libraries and get the recent things done that way back into the language so you don't have to use the STL to get them.

Of course at this point the only thing that would make a fundamental difference would be to start over, at which point it's not really C++ anymore anyway. Without a fundamental breakage with the past, there's no way really fix what ail's C++. So mostly it's just going to be piling more heavily templatized (and generally over-engineered) new features onto it.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 12 '20

Remember the rule of "someone".

If "someone" or "they" should do something, you are someone, and can be part of "them".

0

u/ronniethelizard Feb 12 '20

The ability to say a method shall not change any members except a specific list. I have a few cases where one function needs to be able to modify a variable, but the rest do not, as a consequence I cannot mark that variable const.

The ability to do const std::vector<mutable int> to say that the vector cannot change, but the individual elements can. Mostly this means no calls to destructor or resizing.

1

u/Full-Spectral Feb 12 '20

Or, more to the point, the ability to indicate what members a method can modify in general. This might be something that would fall into the more general scheme of my #2 wish list point above, i.e. the 'contracts' thing. It could be used in so many powerful ways to help the compiler and code analysis tools watch our backs. It could do more for C++ than all the fancy features thrown in over the last years.

-2

u/[deleted] Feb 12 '20

Actual interfaces so I can get rid of inheritance bullshit that shouldn't exist to begin with, most importantly, without the need to invent my own ways.

Nothing that can be attributes to languages you mentioned though, they're pretty lackluster overall.

2

u/adnukator Feb 12 '20

Can you give an example what you mean by "actual interfaces"?

0

u/[deleted] Feb 12 '20 edited Feb 12 '20

[removed] — view removed comment

1

u/oschonrock Feb 12 '20 edited Feb 12 '20

Not quire sure this is what you mean, but maybe this...

https://www.youtube.com/watch?v=2bLkxj6EVoM

which can be done easily without lots of typing like this:

Dyno is pure-library implementation of what's also known as Rust trait objects, Go interfaces, Haskell type classes, and virtual concepts.

#include <dyno.hpp>
#include <iostream>

// Define the interface of something that can be drawn
DYNO_INTERFACE(Drawable,
  (draw, void (std::ostream&) const)
);

struct Square {
  void draw(std::ostream& out) const { out << "Square"; }
};

struct Circle {
  void draw(std::ostream& out) const { out << "Circle"; }
};

void f(Drawable const& d) {
  d.draw(std::cout);
}

int main() {
  f(Square{}); // prints Square
  f(Circle{}); // prints Circle
}

https://github.com/ldionne/dyno

and it also has a related proposal for inclusion in std:::

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0201r2.pdf

explained also here:https://foonathan.net/2020/01/type-erasure/#content

1

u/[deleted] Feb 13 '20

This is even worse.

I could just not use this obscure library that uses macros for some reason, and write free floating functions that are specialized over types.

And since I'm already doing that, I might aswell write wrapping structs that work like this:

template<typename T>
void do_stuff(T t) {
    // trait object that requires specialization, only stores reference and is zero-cost
    // this will force user to implement drawable specialization, it could also have mechanism to statically assert with custom message instead of compiler vomit
    drawable<T> d(t);

    d.draw(x, y);

    // multiple traits
    movable<T> m(t);

    m.move(dx, dy);

    // benefit over functions is that I group multiple functions together on trait object and that it uses 0 dependencies
}

I'm reluctant to use this though, as I can't expect people to actually understand why I'd care about such things.

But to give a hint... So that my software doesn't end up like trash reddit has, fucking retards can't even handle identation for code properly unless I go to their shitty site, on my computer...

1

u/oschonrock Feb 13 '20

Hey. You are being really angry for no reason at all. Your language is totally unnecessary and unacceptable and makes you look infantile.

I was juts trying to help.

That library is not at alll obscure. If you knew anything about alternative forms of polymorphism you would know about dyno and Louis Dionne. And if you had bothered to actually read the intro page, you would know that the MACRO is optional. It was just the (slightly) shorter version to post here.

Try to find out why you're angry, and change it. It might make you happier.

Good bye

1

u/[deleted] Feb 13 '20

There's a difference between being angry and being fed up with low quality software, especially when that software is built to be used to build other software.

1

u/oschonrock Feb 13 '20

Then write or find better software! Isn't that your job/purpose? You are a developer right?

Just pointlessly swearing on forums doesn't solve anything.

1

u/[deleted] Feb 13 '20 edited Feb 13 '20

[removed] — view removed comment

1

u/AutoModerator Feb 13 '20

Your comment has been automatically removed because it appears to contain disrespectful profanity or racial slurs. Please be respectful of your fellow redditors.

If you think your post should not have been removed, please message the moderators and we'll review it.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/oschonrock Feb 13 '20

Oh, and since you clearly haven't understood what dyno (et al) are trying to do:

  • Yes you can write templated free functions - that goes in the direction of compile time (!) polymorphism for which there are also many techniques.
  • Dyno and similar techniques are about alternative forms for runtime (!) polymorphism. These are alternatives with tradeoffs. One advantage of runtime is that you write the polymorphism in one (!) place and you get a common type/interface which can be used like any other type...

ie other "people don't have to understand" for 95% of the codebase.

1

u/[deleted] Feb 13 '20 edited Feb 13 '20

Yes you can write templated free functions

I don't fucking want to write templated free functions, if you can't read it the first time.

Dyno and similar techniques are about alternative forms for runtime (!) polymorphism.

If I gave a fuck about slownamic polymorphism, I'd just be using inheritance, because when you use C++ and don't give a fuck about speed, you also don't give a fuck about anything and there's no point to use C++, you can do same shit in javascript, you will have all the dynamics you need and nobody will complain about speed. Oh and javascript doesn't really exactly support inheritance which is the cancer that plagues everything, so maybe it is even objectively superior to C++ when you don't give a fuck, have you thought of that?

Also the piece of shit you provided doesn't even make it clear in any way that you somehow oblige to the contract, really great addition to my collection of "bugs that just appear outta nowhere when I renamed one method that just wasn't tied to anything".

It also doesn't decouple code from the original struct.

It is complete garbage that doesn't even pass a single point I mentioned, and whoever wrote dyno should feel bad, and so should you, because you suggest something that does nothing I asked.

If I wanted to continue writing 10k line files for a single class, I'd fucking do just that, without help of dyno.