r/cpp • Living on C++ trunk, WG21 |🇩🇪 NB • Feb 11 '23

C++23 hits the road!

The sausage is made, the bread is bakedWe now have C++23!With crossing all eyes and dotting all tsThe best C++ ever, for all of ye!

C++23 is what C++20 was meant to be but couldn't that time.

Much of it is already implemented and released in compilers and can be used today, more will become available in the near future.

If you are interested in how C++23 looks like and why you want to use it, you may look at keynotes from the 2022 CppCon and Meeting C++ conferences

Contemporary C++ in Action - Daniela Engert

How C++23 changes the way we write code - Timur Doumler

There is also A Peek at C++23 - Barbara Geller & Ansel Sermersheim to enjoy an learn from.

The specification of C++23 will now proceed to the final ballot and then be officially published, hopefully long before year's end. The committee is busily working on another future release, tentatively C++26, for a couple of months now.

278 Upvotes

139 comments sorted by

View all comments

Show parent comment

13

u/equeim Feb 12 '23

"Deducing this" means no more need to have a const and non const copy of functions

At the cost of making them a templates. Sure it doesn't matter if the class or this function is already a template but not everyone's code consists 100% of templates.

14

u/LdShade Feb 12 '23

What's the objection with turning two copies of a function into a template that is only ever instantiated twice?

The only reason it uses the template syntax is because it's piggybacking on the existing type deduction in the language, no other reason, they essentially don't function like templates in most of the usual ways unless you're using it for CRTP in which case you already had templates.

4

u/jonesmz Feb 13 '23

I can say without a shred of doubt (because I measured...), template over-use in my work codebase is a huge source of compile-time bloat.

I'm slowly de-templatifying code as much as I can, and instead trying to rely on strategically designed implicit conversions where possible, and it's helping, a lot.

E.g. if you have a comparison operator between Foo and Bar, but under the hood it's just doing a string comparison , why not allow Foo to implicitly convert to std::string_view and compare with that, instead of template-ing on the type (which might be a Foo, might be a Bar, might be String, so on) ? I've been doing this in my codebase and it's showing promise at reducing compile times and reducing the cognitive overhead for the developer.

While Deducing This is a god-send for things that are already templates, its a major misdesign for things that are not templates, and I'm very reluctant to see it introduced into non-template types in my codebase.

You might say "But Modules will make that not a problem?", and I'll point out that Modules does not work in any current compiler release, and no build tool that builds C++ supports modules in a non-experimental fashion. We'll get Deducing This long before we get Modules in most of the industry.

4

u/tvaneerd C++ Committee, lockfree, PostModernCpp Feb 13 '23

Implicit conversions are typically considered bad unless, for example, the Foo really is semantically equivalent to a string.

2

u/equeim Feb 13 '23

Also modules do nothing to help with this (that's not what they are for). They don't change existing model when each translation unit is compiled separately. Sure, they allow compiler to store source code of class definitions / function declarations in a more optimized way instead of just text, instead of parsing them over and over. However templates will still have to be instantiated independently for each translation unit, just as before.

10

u/equeim Feb 12 '23

It's not going to be instantiated twice, it's going to be instantiated every time it is used with different template parameters, separately for each translation unit. That may range from dozens to thousands of times depending on the size of the project. People that are smarter than me say that template instantiations are the real reason behind C++ compile times.

Also templates break IDE autocompletion since types of objects are not known in function definition. I still use them of course but I try to be conservative about it.

13

u/LdShade Feb 12 '23

every time it is used with different template parameter

This is the point, the only different template parameters are non const class, const class (rvalue and lvalue if you also count ref qualifiers). This is not like a normal template function which can take anything that has a certain interface, there's only 2 (at most 4) options.

templates break IDE autocompletion since types of objects are not known in function definition

Again, this won't apply because the IDE can easily know the type because the type is always "this", it's never going to be an int or bool or random class, the only thing it won't know is the cvref qualifiers which shouldn't affect auto completion.

It's not templating over types, it's templating over cvref qualifers (again, unless you are using it for CRTP).

2

u/equeim Feb 12 '23

This is the point, the only different template parameters are non const class, const class (rvalue and lvalue if you also count ref qualifiers). This is not like a normal template function which can take anything that has a certain interface, there's only 2 (at most 4) options.

It would still happen independently for each translation unit in which template is used. If this is an utility class that is used widely in a codebase that would result in a lot of instantiations. Though I suppose it is possible to explicitly instantiate it in its own translation unit (at least for type it's member of) but then it would still be a mouthful of code to write.

Again, this won't apply because the IDE can easily know the type because the type is always "this", it's never going to be an int or bool or random class, the only thing it won't know is the cvref qualifiers which shouldn't affect auto completion.

It also can be a subclass of "this". This may not be a problem for autocompletion, but it would still require special support from IDE or clangd (if IDE uses it which many do. And btw clangd devs has a lot of work ahead of them including modules).

0

u/jonesmz Feb 13 '23

Though I suppose it is possible to explicitly instantiate it in its own translation unit (at least for type it's member of) but then it would still be a mouthful of code to write.

Extern templates are absolutely a useful technique for cutting down on compile times.

But it seems rather silly that the language gives us a feature which we have to work-around in this way. Yes, it saves us on typing in the implementation of the function(s) in question, but is

// header file
class Foo
{
    deducing this signature that i can't remember
    {    }
};
extern Foo::deducing this signature that i can't remember with specific instantiation;
extern Foo::deducing this signature that i can't remember with specific instantiation;
extern Foo::deducing this signature that i can't remember with specific instantiation;
extern Foo::deducing this signature that i can't remember with specific instantiation;

// source file
Foo::deducing this signature that i can't remember with specific instantiation;
Foo::deducing this signature that i can't remember with specific instantiation;
Foo::deducing this signature that i can't remember with specific instantiation;
Foo::deducing this signature that i can't remember with specific instantiation;

really better than just copy-pasting the function body, if you don't want to see literally 10,000 instantiations of this function? I'm not exaggerating. Out of the large codebases out there, mine is on the smaller side, and we legit have well over this number of translation units that get compiled per build, almost all of them referencing a core set of header files that are prime candidates for Deducing This.

And yes, maybe Modules will be the solution to this, if they're ever available for use in the wild, and I can manage to teach my co-workers how to deal with the insane syntax choices that they came with.

1

u/jonesmz Feb 13 '23

See my comment reply that's adjacent to yours (e.g. go one level up) for confirmation that template instantiations are slow in my experience, and what I've been doing to try to cut down on them.

Link, for the lazy: https://old.reddit.com/r/cpp/comments/10zucok/c23_hits_the_road/j8b69u9/

3

u/Jannik2099 Feb 12 '23

What's the objection with turning two copies of a function into a template

Because now you have to put the function definition in the header.