C++ Boot Camp Wednesday: C++11 Deep Dive, and conversation with Patrick Scheller and Bruneau Babet

Posted by on in Programming

Day 3 of the C++ Boot Camp has a lot of material!  First, a deep dive into C++ - first, a lightning overview of C++ for those who haven't used it before, followed by taking some C++98/03-style code and changing it into modern C++, with explanation about what changed and why, as well as pitfalls and things to look into.

Very interesting material, and very useful for getting started!

Youtube replays

C++ Deep Dive

A lightning look at C++ in general for those who are new to it, followed by taking some "old" C++ code and refactoring it to use modern features - importantly, with explanations of each feature and why they are worth using, or why the new design is better.

Online chat with Patrick Scheller and Brueau Babet

This talk is between Pat of the C++ G+ group, a developer and project manager, and Bruneau, one of our compiler engineers.  It's not about C++ specifically, but instead talks about the compiler history, using Clang, compiler extensions, the difficulties of supporting two different languages that can interoperate, and some C++ features.  Fascinating stuff!

 

[Slides]

 

Resources

Source code

Basics

Forums

C++ / STL

C++ Builder

Thanks for attending, and for the great series of questions in the Q&A section afterwards.  You can still join for the rest of the boot camp, and get a copy of C++Builder Starter 100% off, or read more information on the Boot Camp here!

 Q&A Logs

1st Session

 

Q: What procedure is needed to include all files associated with a program?

A: You can right-click a project in the project manager and click Add, then select all the files you need and add them That can be .cpp files or others like .rc files

 

Q: What is the L in:  L"One"

A: L means wchar_t, so the string is made of 2-byte characters, not 1-byte

 

Q: Is it possible to create a true AlphaNumeric sort?

A: Yes, you can sort by any criteria. Alpha-numeric can be hard because of local issues for unusual Latin or non-Latin characters - even Explorer sorts filenames oddly sometimes - but you can sort as you wish

 

Q: what is clang

A: clang is a compiler open source project that is part of the llvm open source toolchain project. http://clang.llvm.org/

 

Q: can you show us other samples of new things C++11 have from the old C++?

A: http://www.slideshare.net/goldshtn/whats-new-in-c-11

 

Q: do you talk about clang + c11 + compiler/free compiler + ide particularities, concepts etc? I would like to understand all that

A: The goal of the Clang project is to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler.

 

Q: is C11  backward compatible?

A: From Bjarne Stroustrup's C++ FAQ "Only very rarely is the committee willing to break standards-conforming code, though that's done when a new keyword (e.g. static_assert, nullptr, and constexpr) is introduced." http://www.stroustrup.com/C++11FAQ.html

 

Q: when you say code smell. are you saying that it is an indication of bad code

A: http://martinfowler.com/bliki/CodeSmell.html

 

Q: What is LLVM if it is related tp C/C++? And Delphi?

A: We use LLVM in our clang enhanced C++ compilers. We also use LLVM for Delphi iOS and Android compilers.

 

Q: ...or is there something in C++ 11 that is better to iterate through objects placed on a form?

A: For Components placed on forms - each form has a component array that you can use to iterate through the components array. FMX: http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Classes.TComponent.Components

 

Q: does C++Builder support C99 as well?

A: Yes - the clang enhanced C++Builder compiler(s) for Win32, Win65, iOS and Android support C, C99, C++ and Objective-C

 

Q: is there a sample available on MongoDB?

A: http://docwiki.embarcadero.com/RADStudio/Berlin/en/Connect_to_MongoDB_Database_(FireDAC)

 

Q: Can you show an example on how to link code to a UI control ?

A: http://docwiki.embarcadero.com/RADStudio/Berlin/en/Event_Handlers

 

Q: Could you show an example C++ for Fibonacci numbers

A: Here is a link to one sample http://www.programmingsimplified.com/cpp/source-code/fibonacci-series

 

Q: Could you explain the "this" keyword?

A: http://en.cppreference.com/w/cpp/language/this

 

Q: how to display web pages as part of a desktop application. like make a browser as part of our application and handle it as per need.

A: use the TWebBrowser component in C++Builder. http://docwiki.embarcadero.com/Libraries/Berlin/en/FMX.WebBrowser.TWebBrowser

 

Q: Can C++11 for loop be told to perform parallel, eg using multiple cores?

A: We'd suggest using the PPL. Some compilers support OpenMP which parallelizes loops, and we don't support that /now/ - considering it, so email me if it would be important to you. The Parallel Programming Library is great for parallelization, especially as a high-level abstraction

 

Q: How can I pass a unique_ptr to a function which needs a normal pointer?

A: use unique_ptr<>.get() - it returns the pointer held inside the smart pointer without changing or releasing ownership. But better is, if you know you have a valid pointer in your smart pointer, to use references and pass a ref to the method. You can dereference the smart pointer with *foo

 

Q: The Classic Compiler Use 'classic' Borland compiler tickbox is only visible with 32-bit Windows, not 64-bit.  That's kept it searching for a fortnight :-)

A: Ah, yes! The 64-bit compiler is the Clang compiler always, there is no classic 64-bit compiler. Sorry for the confusion!

 

Q: if one uses clang, just how far off from the c standard ami going.  I want to learn a language that is as universally accepted as possible

A: Clang is an excellent, standards-compliant compiler, it's very good to use.

 

Q: In the lambda function example, you have  a reference to the map variable. Is it possible to refer to more than one variable in the beginning square brackets? For instance, [&map,&vec](int n,...)

A: Yes, you can refer to as many as you want. In fact, you can also do some rather lazy code like [&](){...} which captures everything visible

 

Q: in the Lambda is n being incremented ?

A: The lambda is being called for each element of items, so n has a different value every time

 

Q: Why would you use a for_each with a lambda function rather than a standard for loop?

A: Writing a for loop specifies how something happens - that it loops in order, for example. But what you really want to do is specify what happens: 'do something for every item' where you don't mind about the order, say. So using the algorithms (not just for_each) is about stating intent, rather than writing code to say how it is done

 

Q: What about debugging - currently it seems like there is no way to step over boilerplate library/template code. "Step into" always goes into those - which is typically not what anybody wants. Any comments on this (or am I doing something wrong)?

A: there are three choices for stepping while debugging - Step Into, Step Over, and Run Until Return. http://docwiki.embarcadero.com/RADStudio/Berlin/en/Overview_of_Debugging

 

Q: Is there a parallel processing variant for_each loop?

A: C++Builder has the Parallel Programming Library (PPL). David I covered this on Day 1 of the bootcamp and has a link to samples for Parallel For Loop, Parallel Tasks and Parallel Futures - http://docwiki.embarcadero.com/RADStudio/Berlin/en/Using_the_Parallel_Programming_Library

 

Q: How does your IDE implement code completion?

A: We use libclang for code completion (and an inbuilt framework for the classic compiler). We have the ToolsAPI for plugins, and I think that does let you query the language and structure. It's not as in-depth as Roslyn, but I'm always interested in feedback :)

 

Q: I have been studying the tutorials on the http://www.cplusplus.com/doc/tutorial/ page. Are these lessons directly applicable on the RAD studio 10 Berlin Starter?

A: All of those samples are C++11 console applications and should work just fine with C++BUilder 10.1 Berlin. Create a C++ console application and make sure to turn off the "Use Classic Compiler" setting in the Project | Options | C++ Compiler dialog in the IDE.  http://docwiki.embarcadero.com/RADStudio/Berlin/en/New_Console_Application_(C%2B%2B)  and http://docwiki.embarcadero.com/RADStudio/Berlin/en/C%2B%2B_Compiler#Classic_Compiler

 

Q: Processors become more and more powerful and have more advanced machine instruction sets. There is quite a difference between an 80386 and a Pentium Pro for example. Does the compiler make use of these advanced instruction sets? If so how is it done?Do libraries always use CPU's most optimal regarding machine instruction sets?

A: It certainly does support new instructions, by default, and you can also give the compiler command-line options for very specific CPU support (not officially supported, we're thinking of officially exposing those switches in upcoming versions)

 

Q: is there a starter edition of Delphi?

A: Yes - https://www.embarcadero.com/products/delphi/starter

 

Q: I manage to use Delphi code and c++ code in a C++ project. Is there a limit to use another language like objective-c in the same project without incurring in some loss of quality or velocity in the final code?

A: you can use Objective-C source code files (xxx.mm) in a C++ project if you use our clang-enhanced compilers.  In your project manager for your project - you can right mouse click and choose "Add..." or "Add new | Other" local menu options. Add allows you to search for .mm files. Add New | Other - choose  the Objective-C++ file type (.mm file).

 

Q: It's real hard to tell what are variables and what are reserve commands. On the examples could you please refactor the variables (Foo). This would help people from making many code smells.  Refactoring the pointer names stop code smells....

A: Yes, I can see that being confusing (Foo foo.) C++ is case-sensitive and that can be confusing. Delphi is case insensitive which many people like

 

Q: Is it right that your can incorporate Delphi in a C++ project BUT your can't do the reverse?

A: correct - delphi in C++ and not C++ in Delphi.

2nd Session

Q: What is the difference between cout and wcout

A: The second is a wide character out stream, the first is narrow. That is, wcout is Unicode-aware for wide characters

 

Q: what is std:: ?

A: That indicates the 'standard namespace'. std holds all the of STL, the standard template library

 

Q: What is a good place to read about how to deal with different string types converted to and from wchar_t* .. .. that seems to be used in the VCL

A: http://docwiki.embarcadero.com/RADStudio/Seattle/en/Unicode_in_RAD_Studio

 

Q: How complex should a Lambda get before turning it into a complete function ?

A: if you find yourself needing to "capture" a whole lot of external variables, global variables, this pointer, etc - then you should consider putting functions into your classes or as a global function (maybe).

 

Q: I have never used a "unit". What's the point of this? Are there any big advantages to grouping the .h and .cpp in this unit file?

A: it is just a separate source code file. In C++Builder a "unit" is the combination of the separate source code file, header file and form file. The "Unit" name comes from Delphi where a Unit is a syntactic file element - in Delphi the interface and the implementation code can be placed in one source code file.

 

Q: I have been using C++ since the late 80's. Unfortunately, I have been stuck in older versions and haven't been exposed to some of the newer CLANG features.  Is there a course you recommend to get me up to speed?

A: Check out Bjarne Stroustrup's "A Tour of C++" ebook and printed book.

 

Q: I don't really see the use of a lambda function. If I wanted the code for the function inline, I would just put it there. In your example, I could just as easily taken your VectorMapFunctor code and placed it directly in line with your for loop., So... what did using the lambda format really save?

A: Using a lambda you can "constrain" (or protect) external objects/classes from your lambda function by only "capturing" the variables or fields you need inside the lambda. You also don't have to create a name for a function.  You can use inline functions as well.

 

Q: How is the n incremented in the Lambda of your code?

A: you can put variables in the capture list of a Lambda - then the code in the body of the lambda can access external variables. http://en.cppreference.com/w/cpp/language/lambda

 

Q: can you explain a "runtime"

A: A runtime is support code that provides basic functionality like allocating and freeing memory, plus maybe math functions, threads, that kind of thing. In the current discussion it gets blurred to also including the entire frameworks shipped with the product, UI and all, so when it refers to C++ using the Delphi runtime it is referring to using, eg, database and UI frameworks written in Delphi, exposed through headers to C++ (so they appear like native C++ code), and that the compiler and C++ runtime is aware of and uses the Delphi runtime to assist with all this

 

Q: I think that it should be possible change from the old compiler to the new one without changes

A: In general it is; there are some quirks of the old compiler vs the new, but they are documented in the docwiki and it's easy to fix the code. In general, they are very compatible

 

Q: Will this synergy Delphi/C++ cause exe files to be bigger? Please don't mind me for raising this Q, but what I have seen while using Delphi that at some point of new release in the past it cause larger exe file.

A: It's a good question! The EXE is larger when you statically link in the various frameworks (FMX, databases, etc) but you can dynamically link and ship those libraries as separate packages

 

Q: What is Method Toxicity Metrics for?

A: They help measure "bad code", how complex code is and how likely it is a method will be clearer if it is split up or refactored

 

Q: Will the purchase of Embarcadero by IDERA jeopardize future developments in C++ Builder?

A: Not at all. They hired me, I think that's a good sign ;) They are very keen to invest in C++Builder and see it promoted

 

Q: The code auto f = 0.0 - is it a float or double ?

A: Double. It would be a float if it was 0.0f. But that's a good example of when /not/ to use auto, you don't gain anything there

 

Q: Double clicking on a variable name only highlights it that one place, not the other places the same names are

A: if you place the editor in "Sync-Edit' mode you should be able to then double click to select within the sync-edit multi-line area.

 

Q: Have they brought Lambdas in, because that functionality was already available in Delphi?

A: C++ lambdas started as a Boost library.

 

 



About
Gold User, No rank,
C++ Product Manager, looking after C++Builder as well as the IDE.

Comments

  • Milen L
    Milen L Friday, 18 November 2016

    Really very useful material for changing the C++ style of programming to modern C++.
    Thank you David for his efforts.
    P.S. Another good point is http://www.stroustrup.com/C++11FAQ.html

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: