 Oh, okay. This part of talk for Pure C++ 2020 is about how you can effectively use concepts in embedded programming or just a non-generic programming context. So, I'm going to talk about how you can lift some structural constraints that are usually verified from time to component to time. So, you achieve some partial evaluation and so you take away things that are computed repeatedly at runtime. You lift that compile time that becomes the job of a compiler so that you can only focus on what is actually essential at runtime. So, that is the focus of this talk. Let's get started. So, let's get into it. How would you pick safely at a table doing so with concepts? So, imagine that you have a programming task where you need to go through tables and when you go through a table you look at an entry and then you perform an action based on what you found at that entry in the table. Here is an abstract representation of that scenario. We have a set of actions to take. So, that set of actions is represented here by an enum class, a well-behaved enum, and of 01 may. And then for each action we read the scripts and then we perform something based on that script. So, the code that run the script we call that an actor. So, in our situation here where we have actions 01 and many we have actors 01 and actor many. And the mapping from an action to an actor is a play and we've record all the plays in the table that we call action map that has the structure of a key value pair table. And on the key here I will say is the action and the corresponding value will be the actor. So, going through the table and performing an action is very simple basic programming task. All we have to do is to carefully going through the table inspecting each entry and asking if what we found there is the action we're asked to perform. And if that is so we just retrieve the corresponding actor and ask it to run this script and then return a value. If we didn't find anything then we will return minus 01 to indicate error. As a matter of fact if the actor also run into some trouble it will return some numerical value to indicate error. Basic. Now this code will run perfectly will do what it's supposed to do. The additional concern here is that we want to perform these tasks while being efficient. So the first idea is to say hey these actions are actually numerical values consecutive numerical values and they are most likely to have as many actions as we have actors. All we need to do is to check that we have the action in bounds. So it must be at least zero and must be less than many and if so then we retrieve the numerical representation of the action. Use that to retrieve the corresponding entry in the action map table. Ask the actor to run the script and return. This is close to optimal efficiency and we might be tempted to say hey we're done. Let's go get go home and have fun. Well not so fast. What happens if the action map that table that maps an action to an actor happens to have a hold on the maintenance. Someone removes retires an action or someone adds something that is not there. Well in that case in case there's a hold in the map table we have a bound safety problem. Even though the action might be within bounds of the number of actions the numerical value may not be in bounds in terms of the size of the table. So boom we get a problem. There are many ways of fixing this and one that I want to show in this short presentation is how we can use concepts to elegantly solve that problem in a way that is reusable therefore generic. So I guess the first question that will pop to mine is what are concepts? The first other approximation is that a concept is a predicate over types and compile time value. So typically we will use concepts to express the expectations of a functional template on each template argument. Okay and this provides the bedrock of principled generic programming, structured generic programming, the way Alex Stepanov does it. Okay which is very successful. Look at the STL and all the many libraries that have been modeled after the approach taken by Stepanov. Concepts being predicate can be composed and are composed via the conventional logical operators conjunction, disjunction, and negation. The interesting thing to bring to the table is that they allow us programmers to express directly in code our intent. Okay that gives you a better structure to code and relegates the hackery, sine, stuff to, you know, places we don't want to hear them from again. And it allows us, language designer, to bring generic programming to the masses, to make generic programming looks like just programming that has been one of the major goals behind concept. As a benefit of being disciplined, bringing structure to the programs, we get partial evaluation for free. I'm going to explain what I mean shortly. But before that, how do you use a concept? Well, most of us will not have to define a concept. We will just use concepts defined by someone else. So C++20 comes with some basic foundational concept. You find them in the standard header concept. You can use them to declare template parameters and place combined constraints on function template parameters. Or if you within normal programming, you can use them to require some structural checking on the type of a local variable. And as an example here on the right side, I have a function that I want to use to compute the minimum number of bits required to express a natural number. So here I will take unsigned int as a natural number or unsigned long or unsigned long long as representation of natural number. And after that definition, where I have the template parameter t with the type of the value, here I'm declaring just like I'll declare normal variable. I declare with the concept unsigned integral. So unsigned integral is a standard concept. It is a predicate, okay? It takes only one type parameter and check whether that parameter type is an integral type according to the standard definition of integral type unsigned integral type. So if I call the function length with a manifestly unsigned integral, now the call would just succeed. If on the other hand, I tried with double the compiler like, yeah, I don't think that would work. Or if I try to call that function length with a manifestly negative number, we'll also get a feedback no can't do. Okay, so this brings additional types of C at the use sites of template. Now, how do you go by defining your own concept? I will get there shortly, but do remember that concepts are compile time predicates. Now, in the context of the problems that we have, which is that we have a table that we want to ensure doesn't have a hole in it, all the entries are filled linearly from zero up to the end. There is no hole in between. I call that a retractable space. Okay, so the first thing we want to do is to define a compile time predicate that check for that property. So again, that predicate is take the table, go through it, and in each entry, check whether the key is numerically the same as the action, the position in the table. If that is true, we're done. If not, then return false. Basic thing. So this function is declared const expert, so that we can use it at compile time with compile time value with a compile time table. Okay, so if we had C++14, we will actually say something like this, we will have our table, and then we will ask the compiler at top level, static assert, please verify that the action map table satisfies the predicate retractable by key. And given what we have, the compiler will go, think about it, and we come back to, yeah, I think that's true. And now that we have that compile time predicate, we have statically verified the structure, the algebraic structure of the table. We can just go in the perform function, check that the runtime value of act is at least zero and less than the size of the table, and use that miracle value to index into the table, retrieve the actor, run the script, and then return whatever is the outcome of the script. So we can express all these safely using generate programming techniques, but not concept to express this. So you will say, we're done. Why do we need concepts? Is this just being fancy? Not exactly. One thing I want to emphasize here is that this is just one table, action map. Usually you'll have many tables like these in structure. And you like to be able to ensure that when you try to index into these tables, these properties of being retractable by key are automatically verified with that you having to duplicate the static asset every time comes tedious, right? So the, the whole point of generate programming is being lazy, safe, and being lazy with class. Okay. So how can we go from this place where we are, which is a safe ground to a higher ground that requires less tapping if the map you happen to have many of them. So the way we do that is first by defining a concept, turning that predicates into a higher level predicate that we call concept. So here we introduce the concept retractable by key. The definition here looks like we have a template, but it is not a template, it is a concept. And we know it is a concept because we use the keyword concept. And the definition is by running this predicate, tracking by key, it's function predicate by key on the table, which is a template non type parameter. Okay. So once we have that, now we can abstract over the table that now we're going to have the instance of in the program and have a function that retrieves the play entry from the table by looking at the corresponding action. Right. So here the function template play entry has the usual template parameters, but additionally has this thing that we call the requires close that says, oh, by the way, I do require that this compartment value table satisfy the predicate retractable by key. Okay. And once we know that, we will just take the numerical value of action, look into that table that is provided as a template parameter and then return the corresponding entry. So this is basically where we want it to go. It's almost there except that the numerical value of action might potentially be out of bound. No problem. When we get to a call that function, we just assert that the value is indeed in bound. And then we call that function and then we're done. We're done because now we still retain the constant time, the near optimal efficiency while remaining safe, bound safe, memory safe. And we have done so by being completely agnostic of the kind of table. The only thing we need from the table is that it is retractable by key. And now any other table that has the same property, we can use the same function to retrieve the entry and perform the same kind of operation with optimal efficiency. So the summary of this technique is that you look at your code and look at what kind of structure does the data type, the data that I'm playing with have and then turn that into some kind of predicate that can be evaluated at compile time. And then abstracting over the specific data so that at compile time, you can perform an action only if the guards, the predicate that guard the action is satisfied. So your compiler will never generate codes for something that is faulty. So just as a summary, this is where the left side is where we were. We have a linear search and the right side is where we ended up where we move the structure of the table, the algebraic structure of the table into a predicate that the compiler can verify. And then we take the residual, which is the things that requires runtime value, and then only do that efficiently. We have removed the inefficiency. We have partially evaluated the algebraic structure of the table and of the computation, and then evaluate the compile time part of it and letting the residual through runtime. So this I hope you can generalize to tables that are not just retractable by key, but have some other algebraic properties, like the entries could be strings and things are sorted by key. Only your imagination is the limit. However, what I want you to also retain is to use concepts early, to use concepts often. Thank you very much for watching. So I hope you get the essence of this talk, which is when you're doing some computations that have structures that they always do. You know, sometimes when it's not obvious, they always have structures that please pay attention to that. I hope you got the essence of techniques and you're able to apply that to your day to day programming job. I don't think of this as some kind of cute puzzles or brain teaser that make you look smart. I think of this as an effective technique that helps you to be more effective in your day-to-day job. Please go try it out. Download your favorite compiler. It could be MSVC, I would prefer, or GCC, or Klein, but please, please try it out and let me know. Give me feedback and I would love to hear from you. Thank you.