CppCast - Teaching Concepts

Episode Date: July 5, 2017

Rob and Jason are joined by Christopher Di Bella to talk about his experience teaching C++ and his proposed changes to Concepts. Christopher Di Bella will soon be a Runtime Technology Engineer... at Codeplay, and was previously university tutor (teaching assistant) for the course 'Advanced C++ Programming', at the University of New South Wales, Australia. He is an avid C++ programmer, and also enjoys film, board games, and snowboarding in his spare time. News CppCon Last Call for Early Bird Pricing Introducing the Splash Damage C++ Tech Blog Security features in Microsoft Visual C++ Meeting Embedded Catch Survey Christopher Di Bella @cjdb_ns Christopher Di Bella's GitHub Links CppCon: Exploring the C++ Standard Library Concepts TS Range v3 Library Range TS/STL2 C++ Extension for Ranges Advanced C++ Programming Course Notes Book: "Writing Secure Code" Book: "Code Complete" Book: "A Tour of C++" Seth Bling "Hacking the Super Mario World" Sponsors Backtrace Hosts @robwirving @lefticus Better C++/Chicago

Transcript
Discussion (0)
Starting point is 00:00:00 This episode of CppCast is sponsored by Backtrace, the turnkey debugging platform that helps you spend less time debugging and more time building. Get to the root cause quickly with detailed information at your fingertips. Start your free trial at backtrace.io slash cppcast. CppCast is also sponsored by CppCon, the annual week-long face-to-face gathering for the entire C++ community. Get your ticket today. Episode 108 of CppCast with guest Christopher DiBella, recorded July 5th, 2017. In this episode, we talk about C++ security and some new tech blogs. Then we talk to Chris DiBella. We talk with Chris about his experience teaching C++ and his proposed changes to concepts. Welcome to episode 108 of CppCast, the only podcast for C++ developers by C++ developers.
Starting point is 00:01:26 I'm your host, Rob Irving, joined by my co-host, Jason Turner. Jason, how are you doing today? I'm doing pretty good, Rob. How are you doing? Doing good. Had a good 4th of July cookout yesterday. You? Well, honestly, mostly I just tried to keep my dog from being too terrified of all the illegal fireworks going off around me. Oh, you see they're not illegal here in North Carolina. A lot of explosions. Even sparklers are illegal in our city limits.
Starting point is 00:01:53 Wow. And it's partially due to the fact that we live in a desert during extreme fire danger. But that doesn't stop people from setting off giant mortars and everything else all around us. Which I must say, it does have me a tad annoyed because we've actually had two grass fires near our neighborhood in the last 12 months. Sounds like it's outlawed for a reason in Colorado then. I would think so, but you know. People like blowing stuff up though. Yes, it is an American holiday through and through indeed it is well top of episode i'd like to read a piece of feedback um this week i got a good tweet from
Starting point is 00:02:33 vladi and he wrote in in response to last week's episode with howard hennant a good episode you guys always bring on the c++ enthusiasts maybe talk to a skeptic sometime and he mentioned uh mike acton jonathan blow and actually forgot to check to see who this other person was uh c muratori do you know who that is jason i do not i'm sorry i might look it up real quick but yeah um the one thing i'll say to that is we would love to have some of these people on. Like Mike Acton, I know he keynoted at CPPCon at the first one, I believe, right? First or second, 2014, I believe. Yeah, 2014.
Starting point is 00:03:20 We definitely like having skeptics and people who aren't diehard C++. We've had interviews with people from the DE community and the Rust community. But it's a lot easier to get someone who is a C++ enthusiast on the show than it is to get someone who's not necessarily as enthused about the language. And just for the record, if you want to come on and talk about the weaknesses of C++,
Starting point is 00:03:40 that's okay. We all admit that this language is not perfect right and i looked up and it's a casey muratori was a suggestion he made and he's a programmer at molly rocket uh which i'm not familiar with but uh we'll definitely reach out to the three of them i think we have reached out to jonathan blow in the past I think we have reached out to Jonathan Blow in the past. We may have reached out to Mike Acton too, but we can try again.
Starting point is 00:04:11 We'd love to hear your thoughts about the show as well. You can always reach out to us on Facebook, Twitter, or email us at feedback at cpcast.com. And don't forget to leave us a review on iTunes. Joining us today is Chris DiBella. He will soon be a runtime technology engineer at Codeplay
Starting point is 00:04:25 and was previously a university tutor and teaching assistant for the course Advanced C++ Programming at the University of New South Wales in Australia. He's an avid C++ programmer and also enjoys film, board games, and snowboarding in his spare time. Chris, welcome to the show. Thank you. So film, board games board games snowboarding i don't think of skiing a lot in australia but there is skiing available right yes um actually i've just come back from new zealand because our slopes are absolutely terrible so paris which is one of the biggest slopes in New South Wales, the state I live in, and the snow is probably less than an inch thick. That's not enough. Yeah.
Starting point is 00:05:17 So they don't run snow machines or something to make sure there's a base? Oh, that's with snow machines. Oh, wow. Okay. So I went last year, and it was raining so much the first day that there was always no snow. And then the second day, it was so hot that the snow machines just weren't putting out enough snow.
Starting point is 00:05:35 So this time around, I decided to go snowboarding in New Zealand and had the time of my life. Awesome. So you're moving soon? Yes, to Edinburgh. Yeah, there's skiing in Scotland? I'm hoping so. I've heard varying accounts of that.
Starting point is 00:05:53 Okay. But it's in Europe, so there's going to be a few places like... Germany, Switzerland. Yeah. Although my brother, who lives in Switzerland, says a lot of people he knows fly to Colorado to take advantage of our awesome snow out here. Well, that's definitely another one. I have heard good things about Colorado's slopes. Yeah, we had some really good late snow this year.
Starting point is 00:06:20 It was pretty crazy. Anyhow. Colorado must be in a really interesting place because you've got the total fire bans right now, which we have in New South Wales pretty much across the board. We can't light any open fires or have sparklers during the summer either. But then you've also got this amazing snow as well. It sounds a lot like Kansas as well. We've got maybe a little off topic for the topic of the show but we basically have three climate zones so on the eastern half it's wide open plains and prairie with dry grasslands in the central it's
Starting point is 00:06:57 the rocky mountains so it's very moist and it's rugged, rough terrain. And on the western slope, you've got the moisture as it's moving across the country from west to east. That is where we've got more of our farming and peach orchards, that kind of thing. So it's interesting out here, for sure. Sure. Yeah. So, Chris, we have a couple of news articles to talk about, and then we'll talk to you about
Starting point is 00:07:25 you know ranges your c++ course maybe the new job you'll be going to soon okay sure sounds good okay so the first one and we really should have been mentioning this last week too but uh cpp con it is the last call for early bird pricing. This episode will publish Friday morning, July 7th. And if you're listening to it on Friday, today is your last chance to get the early bird deal. Wow. Everyone should take advantage of that immediately. Yeah, I think it's like $150 off to get the early bird pricing, which a very very good deal yeah it's definitely worth it yeah absolutely and but you know if you're listening to it after friday you can you can
Starting point is 00:08:12 still register for cpp con and uh the conference will be september 24th 29th so you still have plenty of time plus training before and after that which we'll talk about more too. Yeah, absolutely. Next thing I want to talk about was Splash Damage, which is a gaming company, and they are introducing a new C++ tech blog where they'll be going over various C++ topics, and I thought this was a pretty good introductory post where they're going into their own solution for std function. Yes, it's interesting.
Starting point is 00:08:50 I don't know if our listeners are familiar with standard functions details, but they all have a small function optimization. So if the function that you're wrapping falls within a certain size, basically the size of a pointer, then you don't have to do any dynamic allocation. But in his example, he builds a much larger static buffer so that it can also handle things with captures
Starting point is 00:09:16 and that kind of thing. Right. And he also mentioned in here that after he went and made his own, they're using the Unreal Engine, and he realized that the Unreal Engine actually has its own function, std function type interface. And theirs also has a built-in buffer of 32 bytes
Starting point is 00:09:39 and he says some nice allocator optimization, so maybe that's another good one to use interesting yeah did you have any thoughts chris i thought the blog was quite interesting and the uh the way that he implements the uh his own version of the function was quite surprising i didn't i wasn't aware of the small function optimization so um when i was reading it i was i thought this was a bit of magic but uh it did all come to a a nice close and made sense toward the end and i thought the blog was really well written yeah and really encouraged these guys to continue blogging especially if they're going to be following this style because it was nice nicely well formatted and actually achieved
Starting point is 00:10:23 the point yeah yeah i agree it's very well writtenatted and actually achieved the point. Yeah, I agree. It's very well written. So hopefully we'll see more out of this. And this is on splashdamage.com. Okay, next one, Visual C++ blog. And this is a post on security features in Microsoft Visual C++. And this is a really lengthy post going into different tools and analysis and just other suggestions from the Microsoft team on how to write secure code and look out for problems that attackers will try to exploit. And I thought this was another really well done post. They did mention one book that apparently sounds like it's required reading at Microsoft related to security.
Starting point is 00:11:11 Might need to look into that. And it's just a good highlighting of all the different security stuff that they have available. Yeah, writing secure code. Have you read that book? I haven't read that book, but i have heard of it um it looks like it's in the same series as code complete which i have read and is a very very good book it's interesting it's from 2002 but the basic exploit vectors probably have not changed very much in a long time yeah actually if you, if you watch any of Seth Bling's videos about exploiting Super Mario World,
Starting point is 00:11:49 you can see that really the exploit vectors have not changed in a really long time. Is this like a YouTube series you're talking about, Jason? Exploiting Super Mario World? Oh, yes. It's fascinating stuff. I mean, basically just using controller inputs, he's able to rewrite the game to the point that he is able to do
Starting point is 00:12:10 in-game modifications, in-game hex editor to do real-time editing of the game code, stuff like that. It's, yeah. That's pretty amazing. It's fun stuff. Chris, did you have anything to add on this one? I thought it was really good and touched on,
Starting point is 00:12:30 so we were talking about the blog, not Super Mario World. Yeah. Yeah. I thought it was really good, really good blog and touched on some really, really important points, particularly the section about warnings and static analysis.
Starting point is 00:12:48 The place that I've just left, they have certain sections where we have warnings turned on completely and in certain sections where warnings were turned off, much to my dismay. Often actually in sections that I felt could be turned on,
Starting point is 00:13:05 but the fact that they're saying that there are certain warnings that are turned off by default and you should be turning them back on is something I believe that Clang has now... They've got a minus for everything or something. Jason, you did a talk on this when you're contrasting Visual Studio to Clang and GCC. There's a C++ Weekly? Oh, maybe.
Starting point is 00:13:28 I've done a lot of episodes of those right now. I've also done a conference talk on static analysis in general. And yeah, turning dash W everything on can get a little noisy with Clang, but it can also find a lot of really interesting things in your code. The main things I don't like about WEverything is it warns on C++98 compatibility, and if I've explicitly enabled C++17 mode, I don't care. Right.
Starting point is 00:13:56 Okay, next thing we have is an announcement, and this is from Jens Weller, who runs Meeting C++, obviously. And he's now running Meeting Embedded, which for now looks like it's just a blog role where you can see various blog posts related to embedded programming. I'm not sure if it's specific to embedded C++ programming, but it's good to see that he's putting out this new service to help the embedded community. Hopefully it'll grow
Starting point is 00:14:32 to be more feature-rich the way meeting C++ is and highlighting all the user groups and everything. But we'll have to see where it goes. Yeah. Okay.
Starting point is 00:14:48 And then the last thing, Jason, do you want to mention this, the catch survey? Yeah. So Phil Nash, who we've had on the show a couple of times, who is also author of the catch C++ unit testing framework,
Starting point is 00:15:03 has just asked that users of Catch take a quick SurveyMonkey survey to give them some feedback on how you're using Catch and what improvements could be made. And so I agreed to share it on the show. Yeah, and this is a pretty quick 10-question survey. So if you're using the Catch unit testing library and you'd like to have a say in how its future goes,
Starting point is 00:15:26 definitely take a few minutes to contribute to this. Yeah. And if you're using the free version of SurveyMonkey, you're limited to 10 questions. So you can guarantee that most of these surveys are very short. Yeah. Nice. Before we move on, I just want to say if you haven't used Catch, then please certainly give it a go. I've noticed that a few interviewing companies have started to use Catch as their testing software for C++. And so if you're familiar with that ahead of time, then it'll certainly make the way you write your unit tests in interviews a lot nicer than having to learn how to use Catch on the fly.
Starting point is 00:16:08 Interesting. It is quite a nice library as well. Yeah. It's an interesting way to do interviews, programming interviews, to write your own unit tests. That's pretty cool. That's what you're saying, Chris? That's essentially what I'm saying, but the platform offers it.
Starting point is 00:16:26 And so some companies might actually ask you to write a few tests before you say, OK, I'm ready to submit. Right. OK, so, Chris, do you want to talk a little bit about the advanced C++ course that you were doing, that you were teaching? Sure. So unlike most advanced level computing courses at the University of New South Wales, where advanced implies that it's a second level course and you had a previous course that you had to take,
Starting point is 00:16:59 advanced C++ programming is a first C++ course. It's actually our only C++ course. But they teach advanced programming techniques. And in that, we assume that you're already familiar with at least one other programming language. It's typically C or Java. Undergraduates will come in with both C and Java knowledge and postgrads can come in with C knowledge, but they typically come in with Java and Python knowledge. As a university tutor, I'm essentially responsible for facilitating tutorials, which are run weekly.
Starting point is 00:17:34 They run a week behind the lectures, so I cover practical-ish material. So it's sort of like a lab, and that expands on what the lecturer covered the previous week. In a formal sense, I cover cover in a more practical sense. And then I'm also responsible for marking my own students' assignments. And so we go over all the topics that the lecturer does in a little bit more detail because we have more time and there's fewer students to to reach per class and if uh it's an incredibly rewarding experience so if you do enjoy helping others and you're passionate about um about something that you enjoy helping others with it doesn't have to be c++ but in my experience it typically is um i do
Starting point is 00:18:17 encourage you to apply to be a university tutor or a it's a teaching assistant in the u.s um it's it's actually really really rewarding and you do get a lot out of uh a lot out of it i'm kind of curious about what kind of actual material this advanced c++ class teaches so uh we we start out uh with World, and we progress through the Standard Library. That's actually the second week. It's really early on, which is great. Then we kind of swing back into looking at building abstractions. So the first few weeks are all about using the abstractions that C++ gives you immediately. And then we talk about building abstractions in the second part of the course and so we cover um essentially um everything to do with classes in the first few weeks after
Starting point is 00:19:14 we've covered the standard library uh and then we move on to um i think the most recent iteration of it covered inheritance immediately after that, but I could be wrong. And then we jump into generic programming. And in this section, students are able to actually get hands-on programming by building their own STL-like container. It's complete with an iterator as well uh and then last year's course was very ambitious and uh took on the uh the threads library as well oh wow interesting so you're exposing them to templates and uh yeah you said generic programming um is it staying up to date with modern c++ features? Yes. So last year's iteration, it hasn't started this year,
Starting point is 00:20:08 but last year's one was C++ 14. I believe this last year was the first year of C++ 14. I've been trying to get it to move to C++ 17 for this year, but that's up to the lecturer who runs the course this year. But previously, so in 2015, it was C++11, and it has been since 2013. Not bad. So it stays up to date pretty regularly.
Starting point is 00:20:40 One of the other tutors and myself have tried to get the C++ core guidelines into the course as well to make sure that it's up to date with not only the language itself, but also with good programming practices. Since you bring up the C++ core guidelines, I feel like, I mean, they're just so massive, right? It's like 500 pages or something if you were to print it. What would your approach be
Starting point is 00:21:03 to introducing students to the core guidelines? Don't do what I did last year and just throw it at them. I made the mistake of doing that week one, which was great because no one read it. And the best way to, or at least in my experience, the best way to do it is to write your own notes and feed in relevant uh guidelines so then it's on you to read the uh the 500 pages but you can uh pick
Starting point is 00:21:34 and choose based on the topics and so what i did was i have at least up until i went to cpp con uh i think that was up to the templates uh topic um there are notes that i've got on uh on c++ and you can read through them and they are themselves are quite lengthy i wouldn't read any more than once one section per week but um they they do cover a fair amount of the guidelines for those particular topics and then after that just provide links to the actual direct section so you can read through their examples and understand why those are the cases. But other than that, I would say reading a tour of C++ actually does capture a fair amount of the guidelines.
Starting point is 00:22:17 That's Bjarne's book, right? The small one, yes. Yeah, okay. You mentioned that the undergrads are coming in with C or Java experience. How are they taking C++? Do they seem to enjoy learning the new language? Or new to them, rather? It's incredibly difficult to get students out of a mindset that C++ is not quote c with classes unquote right quote c plus more
Starting point is 00:22:48 unquote or quote low-level java unquote level java yeah it's uh it's it's really difficult in the first few uh first few assignments there's five, and you have some students who immediately pick up that it's going to be a completely different language, you need to approach it as though it's something that you've never seen before. But then there are people who go, well, the syntax looks the same, and I've read a lot about it, and these opinions kind of resonate with what I'm thinking about right now. But around assignments one and two, you can really see which students realized immediately that C++ is something new to learn
Starting point is 00:23:34 and those that are still adapting to that. But usually by assignment three, you can see where... So you can usually see that students have realized that C++ is a brand new language and that you need to put aside what knowledge you already have. And it's definitely not going to be a C-style programming. And it's definitely not going to be a Java-style programming. And you need to make C++ a friend rather than someone that you thought was an acquaintance that you've met along the way.
Starting point is 00:24:08 And I think one of the biggest challenges for students is to adopt the standard library very early on. That's actually a really clear dividing point between high achievers and students that fall between the 50 and the 75 mark. If you do start using the standard library pretty much immediately, then I'm confident that you'll start being able to write code that's really well developed
Starting point is 00:24:37 and you'll usually score better in the assignments if you haven't handcrafted your own code. Interesting. So if you don't mind, if i take a quick step back are most of these students um computer science students coming into this course or from other disciplines majority of so the majority of the undergrads are computer scientists or software engineers uh i believe the postgrads are there's definitely going to be a few electrical engineers in there as well. And I think I had one mechatronics student last year.
Starting point is 00:25:10 The postgrads are a little bit more murky. I had a physics student one year. And the majority of them, I do believe, are going to be doing a master's in IT. Okay. So it sounds like the main thing that you think is important to communicate is the standard library to these students. I think that's a huge portion of it. But I think the main thing is that you're coming in with you've learned in those languages in this new language that looks similar because the values that C++ programmers have aren't going to be exactly the same as what a C programmer or what a Java programmer is going to consider as important. For example, C++ has value semantics, whereas Java doesn't.
Starting point is 00:26:13 And so because you aren't copying in Java, but you are copying in C++, putting in your references and making sure that everything is const is absolutely essential for passing things between functions in C++, putting in your references and making sure that everything is const is absolutely essential for passing things between functions in C++, but it's not the case in Java. And so often people accidentally copy by value instead of passing by reference. Right. So you're going to be giving a class at CppCon this year. Do you want to tell us what you're going to be giving a class at cpp con this year do you want to tell us what you're going to be talking about at the class yep so uh just going back a bit to what jason was saying
Starting point is 00:26:52 about the uh the standard library the class is exploring the standard library okay and uh that is essentially so we start with um some of the basics. So we go through some of the basic containers that you should be using in everyday programming. We dabble a little bit with concepts and ranges and how they can improve your use of the standard library. And then we move a little bit deeper into streams and invocables, which is, I believe the new name for the,
Starting point is 00:27:25 uh, callable objects and function objects. Um, and then we move on to an in-depth review of containers and look at why vector is always the best. and finally we move on to iterators and that can, sorry. And then after that we have utilities,
Starting point is 00:27:40 which we look at, uh, variant optional and tuple. And that concludes the first section of the course. Then we move on to building a mini container. And while building the mini container, we explore some more things such as exception handling and smart pointers. And then what I'm planning to do is to race the container that we've just built against a vector implementation that I'll probably only have time to privately build.
Starting point is 00:28:11 And then see whether or not the effort that we went to in the course is actually worth it or whether or not we should have just used vector in the end so maybe you i'm asking you to give away too much but the container that you are building throughout the course is it vector like or is it something different that's uh that's a good question i haven't actually uh finished working out whether or i want it to be exactly an implementation of a mini vector say a static vector um or if i want it to be something a little bit more uh a little bit more exotic such as say a a vector, or if I want it to be something a little bit more exotic, such as, say, a flat map that you find in Boost, for example, that's something that is going to very much be time-constrained because the other sections of the course are very important,
Starting point is 00:29:00 and they do take up a lot of time. I wanted to interrupt this discussion for just a moment to bring you a word from our sponsors. Backtrace is a debugging platform that improves software quality, reliability, and support by bringing deep introspection and automation throughout the software error lifecycle. Spend less time debugging and reduce your mean time to resolution by using the first and only platform to combine symbolic debugging,
Starting point is 00:29:22 error aggregation, and state analysis. At the time of error, Bactres jumps into action, capturing detailed dumps of application and environmental state. Bactres then performs automated analysis on process memory and executable code to classify errors and highlight important signals such as heap corruption, malware, and much more. This data is aggregated and archived in a centralized object store, providing your team a single system to investigate errors across your environments. Join industry leaders like Fastly, Message Systems, and AppNexus that use Backtrace to modernize their debugging infrastructure. It's free to try, minutes to set up, fully featured with no commitment necessary.
Starting point is 00:29:58 Check them out at backtrace.io slash cppcast. And you also gave a talk or a lecture at CppCon last year where you were talking about concepts and ranges, right? Yes, that's correct. So that talk was titled Start Teaching the Concepts TS and the Ranges Library Now. Unfortunately, it wasn't recorded. Yeah, I was looking for it. It looked like it was at like 9 o'clock at night so i guess they
Starting point is 00:30:25 didn't record things that were that late yeah i just walked into the room as the recording guys had finished packing up and they said no it's too late we're going home um no that's okay i've resubmitted the uh the talk this year so hopefully i'll be able to get it recorded but essentially um the the talk uh introduced what concepts are and uh how how to use the uh the rangers ts as well back then i wasn't aware it was a ts i thought it was just a library that was being proposed um and so essentially it encourages you to start using concepts and rangers then realize that they are an important teaching to us for generic programming and how they can constrain your templates to ensure that you are programming correctly. If you've mistyped something, sorry, that's type in the sense of types in C++, not typo. If you've passed in the wrong type, then you can get some simple feedback from the compiler
Starting point is 00:31:35 rather than having an explosion that you might submit to a journal for publication. So are you using, uh, ranges and concepts in real world code today? So in code that I write personally, yes. Uh,
Starting point is 00:31:52 it's very, I think it's going to be very difficult to get, uh, that into production code that companies might publish because it's, all for, uh, the, the ranges library at the moment is,
Starting point is 00:32:04 uh, still a prototype and it's, um, it's, well, the Rangers library at the moment is still a prototype, and it's, so the TS hasn't been published yet, so the upcoming ISO meeting is when we're hoping to have the TS published as an actual technical specification. Concepts, on the other hand, they are a feature in GCC from 6.0 onwards, but it makes the code you write very constrained. You can't go to Clang or Visual Studio
Starting point is 00:32:29 and use another compiler because it's a GCC-only implementation at the moment. If you do find that the idea of concepts are interesting enough to actually try out, Eric Niebler's Range v3 library is essentially the basis for the rangers ts uh without actually using concepts and i found that it's almost uh almost exactly the same it's not quite as it doesn't have the language support that concepts offer and i last i used it
Starting point is 00:33:01 the uh the diagnostics weren't quite as nice as ranges, but that's actually up to debate. Sometimes I can get ugly messages with ranges. But it's definitely a great library, and it has some things that aren't in the ranges tier, such as views and actions, which make programming even simpler again. I may be misremembering this, but I believe I saw some comments on Twitter recently that ranges can slow down your compile times in some circumstances.
Starting point is 00:33:34 Have you noticed any issues with this? Yes. So both ranges and range v3 seem to have this. But I do have a huge slowdown in certain sections of my code that's generally um i guess i i don't know the actual implementation reason but my uh i guess we it's due to the concept checking in that regard and making sure that your types are constrained but i think that's a price worth paying if you are going to um if you're going to be able to ensure that your your code is type safe and correct and if it isn't you're going to be getting back some
Starting point is 00:34:11 getting some feedback that is actually readable rather than some mess that you will then have to sit through and read because you can uh you can easily make the changes or think about your program in a different way um because you know what types are supposed to be expected and what type you've passed in instead of having to work out what this weird syntax feature that you didn't write is now breaking because you passed in the wrong thing. So it moves the error from being inside the code you didn't write to the code you have written
Starting point is 00:34:45 and only exposes the interface rather than the implementation. So you think that's worth the compile time cost? For small projects, definitely. I haven't tried it on anything that's a million lines in size, so I can't say for that. But definitely for programs that are in the order of 10 000 to 100 000 lines so thinking about teaching c++ and these crazy compilers we can get you said that you've been involved in teaching template programming how do you teach your students to deal with these things when they see them um at the moment it's so i've
Starting point is 00:35:26 been trying to get uh concepts into the into the course um at least as an extension topic so that way students can reduce the amount but at the moment it's sort of we've got to find the bit of code that you've written and work from there so um there's one section and it's a uh it's a talking point in the talk that i gave last year at cpp con essentially um we have a constructor that takes an integer and a double and does some does some computation and then we have another one that takes an iterator range and does some computation. And if you pass in an integer and another integer, it'll default to the iterator constructor
Starting point is 00:36:14 because it's template-based. So we have a homogenous pair rather than a heterogeneous one. And so then we have this this weird can't dereference an integer uh error and so the students are all wondering what's going on and so i had to kind of point them along the the lines of well what's going on with uh with the parameters here you've passed in something and so remember templates are lazy and think about what they'll default to and think about how the template will look for what is already existing and what doesn't exist and just try and work from there but when it comes to much
Starting point is 00:36:59 larger template errors it's more a case of you've got to sit down. I've used STL Filt once. I haven't used it since, but from memory, I do believe that it was actually somewhat, maybe it was somewhat nicer, but not enough to actually make me integrate it into my tool chain. It's more a case of head to the top of the error and try and work down from there but so definitely disregard the latter half in my experience okay yeah i was going to ask if you had if you encourage your students to use any of these filters because i i think vittorio is also written one i don't know if that's stL filter, if that's something different. But it seems like there's a handful of these Python scripts that
Starting point is 00:37:48 make an attempt to filter out your template errors. Do you ever go down the road in this Advanced C++ class of trying to teach Sphene and VoidT and like, well, we can, if we want to, disable this overload so that it only works on
Starting point is 00:38:05 things that are dereferenceable or whatever. So there's a section at the moment at the very back of the course that's not assessed on template metaprogramming. And it does very, very, very briefly touch on Sphinae, but it doesn't do it enough to, in my opinion, to make it a practical, in a practical sense, it's kind of, this is a cool feature of templates, but it's not something that feels very real world. And I think if VoidT, what is it? Integral constant, I think is the one,
Starting point is 00:38:43 true type, false type and so on i think if those were integrated a little bit more into the uh into the course it could help with eliminating some of those uh those large diagnostics at least in the short term but um i'm not too sure how well it'll play in the long term in terms of for students own sake uh in terms of uh writing code that's understandable i know that uh when you do get very involved in that kind of stuff and that's coming speaking from experience um code can start to become a little bit more verbose and difficult to understand if you aren't doing it in a way that is, uh,
Starting point is 00:39:28 is what I'd say easily readable. And that's, um, there's definitely a point where you need to sit down and plan how to, how to write the code first and then how to teach it. I think that's a very, a very good topic if you can teach it well and something that will crash and burn if you don't teach it the correct way right right do you think it'll you think
Starting point is 00:39:51 ultimately spinae will go like the way of manual memory management once we have concepts i don't know if it'll go away entirely but that's definitely my my hope i hope that concepts will make it easier to to write code that would have been tablet metaprogramming and it certainly does in certain sections i've i haven't written enable if uh since i've started using concepts um i haven't written enable if in sections where i do use concepts. I use enable if now to write concepts. So that's the same as what you're saying about manual memory management. But as for whether or not it'll eliminate it entirely, that is something that remains to be seen.
Starting point is 00:40:41 Right. So changing gears again, you worked on a proposal for changes to the concepts TS, right? Do you want to tell us a little bit about that? Sure. So for those who aren't familiar with concepts intimately, essentially what you do is you say that you have a concept and for the sake of this discussion,
Starting point is 00:41:02 we're only going to talk about variable concepts. So we say we have a concept foo equals and then something on the right-hand side of equals. And at the moment, if you have a very verbose concept that you want to write, you have no way of shortening names internally. If you want to create an alias, it needs to be either in the template, in the parameter header, or it needs to be an external alias.
Starting point is 00:41:36 And that can lead to problems such as, for example, if it's in the parameter header, then programmers who have access to the interface have the opportunity to mess with that if they can work out ways to mess with it and internally if you do want to to make a few shorthands without having to expose names outside as aliases you would need to then create a secondary concept whose sole purpose is to abstract away all the details of the long names. For example, if you wanted to have the value type of a container, instead of saying type name T colon colon value type, you would need to pass that into this helper helper concept and then you could
Starting point is 00:42:26 just say V for value type and in the second concept and all the details are actually in the second concept and the first concept is just the I guess the delegator okay so this proposal offers you to obviously the suggestion to create aliases inside concepts so that way you don't have to you don't have to do that it's all just in one concept and that's it you know i'm i guess a little late to the game here but i just realized we haven't really actually introduced what concepts are is it possible that you could give us like a quick overview of really what the C++ programmer expects to get from concepts? Sure. Yes, I realize what you're saying now.
Starting point is 00:43:13 So essentially, templates and generic programming in general are a great and powerful feature of any language, but especially C++ and templates are extremely powerful. You can write code that essentially takes on any form whenever you need it and it's lazy instantiation which is great but the problem is that if you don't pass in the... they're very fragile... if you don't pass in the exact um parameters that are expected if you don't have the correct syntax if you don't have the correct semantics you are either going to have some very serious syntax errors or um uh really really hope not uh some very bad uh semantic errors which which may manifest at runtime even. And that's the section that I'm most scared about.
Starting point is 00:44:08 And so the way I generally summarize diagnostics with templates is that you could submit it to your professor for a PhD. And so what concepts aim to achieve is type safe generic programming and it's constraint-based generic programming so what you do is you say for example that take take sort for example you can't sort a std list because it has a a bi-directional iterator and you need a random access iterator right and so at the moment it's just uh i believe it takes an a type name i and um actually i think that's it just takes a type name i but i can't remember the standard off the top of my head um and so that's not exactly great if you then go and pass in and a bidirectional iterator because you are working
Starting point is 00:45:08 in a generic context and so this sort happens to be in a generic function and that you haven't written and you just pass and so it works for everything but this one line of code and you pass in your list iterator and then it explodes because just because of this sort and you don't know what's going on because you haven't written this generic function so what concepts can do is we can say at the sort level that um that the sort needs to be needs to take a random access iterator or we could say that it needs to be sortable and so there is a sortable concept in the ranges TS. The ranges TS is essentially a collection of standard concepts and extensions to the standard library. So they do take on concepts as well.
Starting point is 00:45:53 And sort now has this constraint on it that anything that wants to be sorted needs to have a sortable property about it. And vector satisfies that. sorted needs to have a sortable property about it. And vector satisfies that. Pretty sure deck and array also satisfy that, but list doesn't. And so we could stop here and say that now that we're using the ranges version of sort, we just, we can walk away.
Starting point is 00:46:19 But that's not enough because you didn't write this function that calls sort. So it's also on the responsibility of this other generic function to say, well, you've passed in these list iterators. They aren't random access iterators. So, or they aren't sortable. So we need to push that back up
Starting point is 00:46:36 to the top of the generic code and constrain everything. So that way you're not writing code that could then explode at any point and now the programmer knows when they're calling this this generic foo that their list is never going to work with the function because of the requirements of something inside that that function which happens to be sought but they don't need to worry about it being sought they need to worry about the fact that their their choice of container is not sortable.
Starting point is 00:47:05 Okay. So if I understand correctly, concepts do participate in overload resolution. So if I wanted to, I could write a sort that works with forward iterator or a sort that with bidirectional iterator and one that works with random access iterator, and it would dispatch to the correct thing at compile time? That's, yes, that's correct. So something that I didn't say before is that concepts, first and foremost, are templates. So if you have a concept,
Starting point is 00:47:37 you can easily substitute that with type name and it will do exactly the same job. You can substitute it with auto. It will do exactly the same job. The difference is that you can think of it like type name standing behind a security guard and the security guard will or a bouncer has a list of names to say yes you can go in yes you can go in nope sorry you're not a bi-directional iterator you're a forward iterator you're not allowed to enter the uh you're not allowed to enter this function but uh you don't if you're using concepts to write your generic function,
Starting point is 00:48:07 you don't actually see the template keyword? Or do you, when you're actually writing a function? This is actually a hot topic at the moment. At the moment, yes, you can see the template keyword. Okay. And so you can say template, type name T, and then underneath the template section, you have the concept section,
Starting point is 00:48:28 which says requires forward iterator of T. Okay. We're saying that T must be a forward iterator, and then we continue on as though it's a function. The second way to do it is that you can say we have template forward iterator uh t okay and so now we've eliminated the word type name but the word template is still there and this is the way that i write most of my my concept base code
Starting point is 00:48:55 um so we're eliminating type name so it can no longer be any type it's just going to be a forward iterator um and then the third way that we can do it is that we can say um we can eliminate the template section altogether and just have um generic foo forward iterator t so forward iterator i um where i is the variable name as there's no template section it's like a generic lambda where you can say auto, but this is any function. It can both be a Lambda or a normal function. And this is the topic of debate you're saying?
Starting point is 00:49:33 This is the topic of debate, primarily because of readability, I think. I know that Alistair Meredith did a talk at C++ Now on the standard library version 2, and he did talk about this issue a little bit. He was saying, I think from memory, it was one of his issue is determining if something is a forwarding reference or if it's simply an R value reference.
Starting point is 00:50:02 And so that seems to be a point of contention there, but there may be other issues to do with readability. I don't know if it's simply an R value reference. And so that seems to be a point of contention there, but there may be other issues to do with readability. I don't know it's a generic function. What should I do? In my opinion, just because it's a generic function that shouldn't matter to you, the person who's using the code, it's an interface,
Starting point is 00:50:19 just accept it whether or not it's polymorphic in the sense of static polymorphism or runtime polymorphism. It doesn't really matter from your perspective. I feel like personally I've gotten so used to implicitly writing templates through generic lambdas that it would seem kind of natural at this point to do the same thing with concepts. But I haven't played with concepts yet. I highly recommend it if you've got the time. Okay. It sounds like you're pretty involved with the uh the concepts proposal um how do you feel about its chances for making it into the next standard um well we've just touched on uh at least from
Starting point is 00:50:57 my perspective because i haven't actually gone to a standards meeting standard meeting okay uh thank you uh i haven't gone there but um are you going to toronto i would love to go to toronto but because i'm moving to edinburgh i don't have uh i don't have the time or the money to go there um but um but from from what i gather from the community it's a uh it's a uh it's something that everyone wants we just can't seem to agree on a few small things that um that are blocking it at the moment and the concepts ts has things that people think should have been proposed separately to the uh to the rest of concepts for example the uh the the terse syntax which where you don't have the word template type name in it at all.
Starting point is 00:51:48 And so from my understanding, it's those things that are stopping us at the moment from getting it into the standard. But I'm really hoping that we can reach a middle ground there because I do think that it's time for concepts to start shining in our code as much as possible. Okay. Okay. Well, where can people find you online, Chris, or find information about your upcoming course at CppCon? So if you want to get in touch with me, probably Twitter or Slack are the best two ways to get in touch.
Starting point is 00:52:21 So on Twitter, I'm at CJDB underscore NS. And on Slack, I'm just CJDB. If you want to check out my stuff on GitHub, then it's also CJDB. I try to keep those four letters for myself wherever I can. And for CppCon, the best place to look at the moment is the cpp con website and is your course going to be before or after the conference right scheduled for after the conference so i'm in direct competition with jason that's all right okay well it's been great
Starting point is 00:53:01 having you on today chris. Thank you. Yeah. Thanks for joining us. Thanks so much for listening in as we chat about C++. I'd love to hear what you think of the podcast. Please let me know if we're discussing the stuff you're interested in. Or if you have a suggestion for a topic, I'd love to hear about that too. You can email all your thoughts to feedback at cppcast.com. I'd also appreciate if you like CppCast on Facebook and follow CppCast
Starting point is 00:53:26 on Twitter. You can also follow me at Rob W. Irving and Jason at Leftkiss on Twitter. And of course, you can find all that info and the show notes on the podcast website at cppcast.com. Theme music for this episode is provided by podcastthemes.com.

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.