Тёмный

Deducing this Patterns - Ben Deane - CppCon 2021 

CppCon
Подписаться 153 тыс.
Просмотров 12 тыс.
50% 1

cppcon.org/
github.com/Cpp...
---
How would our code change if a member function could deduce the type and value category of the object ("this") it worked on?
Potentially a lot, and this talk will highlight some of those changes. Because with C++23, member functions will be able to do just that using a feature described in P0847, "Deducing this". Many patterns in C++ today could change, and new patterns will arise. This talk will explore those patterns, explain how "deducing this" works, and show how it solves the problems that originally motivated the paper.
Some sample effects on our code: CRTP is simplified; decorators become much easier and terser; recursive lambda expressions become straightforward. There are also implications for safely and efficiently moving values through computations.
---
Ben Deane
Ben was a game programmer for over 20 years, working in C++ since the late 90s, for companies like EA and Blizzard. In 2018 he left the games industry to join Quantlab, an automated trading firm, where he writes modern, well-performing C++. He likes to play with algorithms, and to make the type system work for him, and he’s always looking for useful new techniques. Ben is the presenter of several previous C++Now and CppCon talks.
---
Videos Filmed & Edited by Bash Films: www.BashFilms.com
RU-vid Channel Managed by Digital Medium Ltd events.digital...
*--*

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@Betadel
@Betadel 2 года назад
Personally this is the feature I'm most excited about for C++23. Did forward_like manage to make it to the standard too?
@ultimatesoup
@ultimatesoup Год назад
Doesn't this create a need for virtual template member functions though? Unless that is added, this is pretty restrictive
@Bolpat
@Bolpat 2 года назад
I love the proposal and the solution presented; the presentation is also good. However, I find myself peering on the screen to find out if some brackets are parentheses or braces, the font you use make them look too similar. Also the choice of coloring (dark yellow text on light yellow background) is hard to read.
@TheMR-777
@TheMR-777 2 года назад
I kind of agree, but it's very clearly visible on higher bit displays
@TheMR-777
@TheMR-777 2 года назад
That was… *GREAT!* This is the 2nd “Life Changing” talk I've ever heard, after “Ranges” talk :)
@kodirovsshik
@kodirovsshik 2 года назад
Have been waiting to see a talk about deducing this for so long, and it's finally here let's go
@torstenknodt6866
@torstenknodt6866 5 месяцев назад
It seems I cannot make a function using "this" "const" anymore, which is e.g. not that great for "operator"s. Any reason for that?
@JohnB5290
@JohnB5290 2 года назад
Great talk, and a cool feature. The fact that I have to *decide* between whether I want to see the "call-site" type with void f(this auto self) or the "definition-site" type with void f(this MyClass self) is, however, pretty bad, in particular due to type deduction (the main motivation) *requiring* the type of self to be always the call-site type, even though I may only want to know whether my object is rvalue or lvalue. Why can't I have both, e.g. by permitting the use of this (in its traditional meaning) within the member function? The fact that a c-style cast is presented as a possible workaround (which it probably isn't, think of virtual inheritance) should be a hint that something is missing here.
@MrSwac31
@MrSwac31 2 года назад
Slide 44, just wow! I'm hyped!
@ultimatesoup
@ultimatesoup Год назад
Doesn't this create a need to be able to make virtual template functions though? How is that handled? One thing I would really like to see added is covariance with smart pointers. If we had that we could get rid of half the pointer casts In our codebase
@lincolnsand5127
@lincolnsand5127 2 года назад
This will go well with UFC (Uniform Call Syntax) if UFC eventually gets approved. It already goes a long way to implement/add basic UFC as it is.
@JohnWilliams-gy5yc
@JohnWilliams-gy5yc Год назад
I just wonder if enabling sprinkles of concept contrains all over the members would simply make the code more hard to comprehend unneccesarily? Shouldn't the concept contrains be at only one stop to read?
@blablabla7796
@blablabla7796 2 года назад
I’m happy to see that people need like_t like I did (I had to make my own called copy_qualifiers or something like that). Hopefully with this we can have better range based loop support for custom types. I’m not sure if it’s been resolved yet where you can have for(auto const [a, b] : r) where a and b are actually const. I remember playing with this and it just won’t behave the way I want it to.
@victoreijkhout7115
@victoreijkhout7115 Год назад
Is the "before" in 8:09 correct? I find that I have to write "auto&", not just "auto".
@skilz8098
@skilz8098 2 года назад
I have noticed so far that the slide @16:55 appears to compile only with MSVC x64 & compiler option /std:c++latest exactly as it is on the slide. I tried other major compilers such as gcc, clang, and intels with their equivalent latest features and or experimental branches and so far it appears none of them have compiled at least on Compiler Explorer. It's been a while since I've read any new documentations on the progress of the C++ language going back to before C++20 was first being announced when C++11,14, & 17 were mainstream. I like the concept of this new feature: puns intended, but currently it appears that only MSVC has implemented this current language feature for C++23. I may be wrong so please don't quote me. If you do know that it can be compiled from either GCC or Clang please feel free to point me in the correct direction as to the appropriate release version and required compiler flags. I like to try and stay current with the language when I have the available free time.
@xorbe2
@xorbe2 Год назад
Oct 2022: Still seems to be the case, I checked clang trunk and gcc trunk.
@Bolpat
@Bolpat 2 года назад
Are there plans to allow the _this_ parameter on the second parameter of a binary operator? Because as of now, one can overload an operator like % in two ways (free-standing and member) now also using _this_ parameter for the first, so why not also for the second? It looks very reasonable in my opinion.
@Swedishnbkongu
@Swedishnbkongu 2 года назад
@ok i don't see the use case yet but i think he means struct S { S operator+(S lhs, this S const& this){} };
@Swedishnbkongu
@Swedishnbkongu 2 года назад
@ok as to why this would ever be preferred over a free/friend function, idk
@matthieud.1131
@matthieud.1131 2 года назад
I can see the use of the "this ... self" becoming standard practice recommended in guidelines in the future, more generally than just when it is required. Beyond the use-cases presented here, it makes it very clear inside the body of the function what are member variables/functions vs. what aren't (sure we could always do this->x before but it wasn't required; here it makes the use of self.x mandatory, preventing errors like shadowing a member variable with a local variable). Also if I have a function f that does not deduce this and I want to use a function g that does, I would have to refactor my function f, so better write f with a deduced this to begin with. Plus, it will look familiar to Python developers who want to learn C++.
@lincolnsand5127
@lincolnsand5127 2 года назад
Idk, I think it'll become like trailing return types
@JohnDlugosz
@JohnDlugosz 2 года назад
Is there a succinct way to use abbrviated template declarations, rather than prefixing it with template ? Like with the generic lambda, you used `auto`.
@Bolpat
@Bolpat 2 года назад
I don't see why R f(this auto&& self, …) {…} wouldn't work.
@skilz8098
@skilz8098 2 года назад
I'm also curious about the "sigh" moment when he said, "currently 'this' is still a 'pointer' no 'references'..." I'm wondering if it would be feasible to incorporate 'this' as a reference without breaking the original 'pointer' type. All it would require is an additional character to the syntax that would be a hint to the compiler to tell it to compile 'this' as a reference instead of a 'pointer' simply by appending the 'reference' operator after it. For example: 'this' would still be the currently implemented pointer type where 'this&' would be the reference type to 'this' as opposed to the 'pointer' type to preserve original code bases. If there is no & immediately following this without any spaces then the this pointer would now become the this reference object. A space would terminate the keyword string and would be compiled as the this pointer and the & would cause a compiler error depending on the context of the syntax.
@JohnDlugosz
@JohnDlugosz 2 года назад
That is very close to being able to handle smart pointers as well. Just have the deduced most-derived type skip the following of operator-> as well. That would bring us very close to real monads. To be clear: given std::shared_ptr p; if C::foo had a deduced this, then calling p->foo() would provide the shared_ptr as self.
@Swedishnbkongu
@Swedishnbkongu 2 года назад
Idk though, smart pointers are has-a, not is-a. What do you intend to do with this?
@nmmm2000
@nmmm2000 2 года назад
Did not watch it in full. Can this help to produce const and non-const getter functions? e.g. template auto getX(this Me &&me) -> decltype(something) { return x; }
@Bolpat
@Bolpat 2 года назад
Yes. It's one of its primary applications.
@tomfranky1744
@tomfranky1744 Год назад
Dislike this because he complicated a non-complex thing and extended it to about an hour.
Далее
The Factory Pattern - Mike Shah - CppCon 2021
1:01:06
Просмотров 19 тыс.
Let's get comfortable with SFINAE (C++)
35:55
Просмотров 8 тыс.
Back to Basics: Concurrency - Mike Shah - CppCon 2021
1:02:07