Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

C++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library
C++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library
C++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library
Ebook207 pages1 hour

C++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This quick C++17 guide is a condensed code and syntax reference to the popular programming language, fully updated for C++17. It presents the essential C++ syntax in a well-organized format that can be used as a handy reference.
In the C++17 Quick Syntax Reference, you will find short, simple, and focused code examples.  This book includes a well laid out table of contents and a comprehensive index allowing for easy review. You won’t find any technical jargon, bloated samples, drawn out history lessons, or witty stories in this book. What you will find is a language reference that is concise, to the point and highly accessible.  The book is packed with useful information and is a must-have for any C++ programmer.
What You'll Learn
  • Use template argument deduction for class templates
  • Declare non-type template parameters with auto-folding expressions and auto deduction from braced-init-list
  • Apply lambdas and lambda capture by value
  • Work with inline variables, nested namespaces, structured bindings, and selection statements with initializer
  • Use utf-8 character literals
  • Carry out direct-list initialization of enums
  • Use these new C++17 library features or class templates from std::variant, optional, any, string_view, invoke, apply and more
  • Do splicing for maps and sets, also new to C++17

Who This Book Is For
Experienced C++ programmers. Additionally, this is a concise, easily-digested introduction for other programmers new to C++.
LanguageEnglish
PublisherApress
Release dateMar 29, 2018
ISBN9781484236000
C++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library
Author

Mikael Olsson

Mikael Olsson is a professional programmer, author and web entrepreneur. He enjoys teaching, writing books and making sites that summarize various fields of interest. The books he writes are focused on teaching their subject in the most efficient way possible, by explaining only what is relevant and practical without any unnecessary repetition or theory. He can be reached online at Siforia.com.

Read more from Mikael Olsson

Related to C++17 Quick Syntax Reference

Related ebooks

Programming For You

View More

Related articles

Reviews for C++17 Quick Syntax Reference

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    C++17 Quick Syntax Reference - Mikael Olsson

    © Mikael Olsson 2018

    Mikael OlssonC++17 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-3600-0_1

    1. Hello World

    Mikael Olsson¹ 

    (1)

    Hammarland, Finland

    Choosing an IDE

    To begin developing in C++ you need a text editor and a C++ compiler. You can get both at the same time by installing an Integrated Development Environment (IDE) that includes support for C++. A good choice is Microsoft's Visual Studio Community Edition, which is a free version of Visual Studio that is available from Microsoft’s website.¹ This IDE has full support for the C++14 standard and includes most features of C++17 as of the 2017 version.

    Two other popular cross-platform IDEs include NetBeans and Eclipse CDT. Alternatively, you can develop using a simple text editor such as Notepad, although this is less convenient than using an IDE. If you choose to use a simple text editor, just create an empty document with a .cpp file extension and open it in the editor of your choice.

    Creating a Project

    After installing Visual Studio, go ahead and launch the program. You then need to create a project, which will manage the C++ source files and other resources. Go to File ➤ New ➤ Project in Visual Studio to display the New Project window. From there, select the Visual C++ template type in the left frame. Then select the Empty Project template in the right frame. At the bottom of the window, you can configure the name and location of the project. When you are finished, click the OK button to let the wizard create your empty project.

    Adding a Source File

    You have now created a C++ project. In the Solution Explorer pane (choose View ➤ Solution Explorer), you can see that the project consists of three empty folders: Header Files, Resource Files, and Source Files. Right-click on the Source Files folder and choose Add ➤ New Item. From the Add New Item dialog box, choose the C++ file (.cpp) template. Give this source file the name MyApp and click the Add button. An empty .cpp file will now be added to your project and opened for you.

    Selecting Language Standard

    To enable the latest features of the C++ language outlined in this book it is necessary to manually change the language standard setting for your project. You can do this by first going to Project ➤ Properties to bring up the Property pages. From there, navigate to Configuration Properties ➤ C/C++ ➤ Language ➤ C++ Language Standard. Select the ISO C++17 standard from the drop-down list. Click OK and the project will now be configured to compile according to the C++17 language standard.

    Hello World

    The first thing to add to the source file is the main() function. This is the entry point of the program, and the code inside of the curly brackets is executed when the program runs. The brackets, along with their content, is referred to as a code block, or just a block.

    int main() {}

    The first application will simply output the text Hello World to the screen. Before this can be done the iostream header needs to be included. This header provides input and output functionality for the program, and it is one of the standard library files that comes with all C++ compilers. The #include directive effectively replaces the line with everything in the specified header before the file is compiled into an executable.

    #include

    int main() {}

    With iostream included, you gain access to several new functions. These are all located in the standard namespace called std, which you can examine by using a double colon, also called the scope resolution operator (:: ) . After typing this in Visual Studio, the IntelliSense window will automatically open, displaying the namespace contents. Among the members you find the cout stream, which is the standard output stream in C++ that will be used to print text to a console window. It uses two less than signs, collectively known as the insertion operator (<<), to indicate what to output. The string can then be specified, delimited by double quotes, and followed by a semicolon. The semicolon is used in C++ to mark the end of all statements.

    #include

    int main()

    {

      std::cout << Hello World;

    }

    Using the Standard Namespace

    To make things a bit easier, you can add a line specifying that the code file uses the standard namespace. You then no longer have to prefix cout with the namespace (std::) since it is used by default.

    #include

    using namespace std;

    int main()

    {

      cout << Hello World;

    }

    IntelliSense

    When writing code in Visual Studio, a window called IntelliSense will pop up wherever there are multiple predetermined alternatives from which to choose. This window can be also brought up manually at any time by pressing Ctrl+Space to provide quick access to any code entities you are able to use within your program. This is a very powerful feature that you should learn to make good use of.

    Footnotes

    1

    http://www.microsoft.com/visualstudio

    © Mikael Olsson 2018

    Mikael OlssonC++17 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-3600-0_2

    2. Compile and Run

    Mikael Olsson¹ 

    (1)

    Hammarland, Finland

    Visual Studio Compilation

    Continuing from the last chapter, the Hello World program is now complete and ready to be compiled and run. You can do this by going to the Debug menu and clicking on Start Without Debugging (Ctrl+F5). Visual Studio then compiles and runs the application, which displays the text in a console window.

    If you select Start Debugging (F5) from the Debug menu instead, the console window displaying Hello World will close as soon as the main function is finished. To prevent this, you can add a call to the cin.get function at the end of main. This function, belonging to the console input stream, will read input from the keyboard until the Return key is pressed.

    #include

    using namespace std;

    int main()

    {

      cout << Hello World;

      cin.get();

    }

    Console Compilation

    As an alternative to using an IDE, you can also compile source files from a terminal window as long as you have a C++ compiler.¹ For example, on a Linux machine you can use the GNU C++ compiler, which is available on virtually all UNIX systems, including Linux and the BSD family, as part of the GNU Compiler Collection (GCC) . This compiler can also be installed on Windows by downloading MinGW or on the Mac as part of the Xcode development environment.

    To use the GNU compiler, you type its name g++ in a terminal window and give it the input and output filenames as arguments. It then produces an executable file, which when run gives the same result as the one compiled under Windows in Visual

    Enjoying the preview?
    Page 1 of 1