Coder Social home page Coder Social logo

c-17-new-features's Introduction

C++17 New Features

Some brief code I wrote to showcase a few new language features of C++17. Specifically, I demonstrated:

  • if constexpr
  • constexpr lambda functions
  • capturing *this in lambda functions

Note: you must compile with either -std=c++17 or -std=c++2a compiler flag with g++.

if constexpr

This allows for compile-time evaluation of an if statement. It is typically used in conjunction with templates and some Boolean operator. The unused branch of the if statement is discarded, meaning it does not have to be able to compile.

It is similar to std::enable_if but much cleaner.

constexpr lambdas

Functions could be declared constexpr since C++11 but not lambda functions. In C++17, lambdas are implicitly constexpr when all conditions are satisfied. This allows lambda functions to be evaluated at compile time. One can also explicitly declare a lambda as constexpr.

Capturing *this in lambda functions

Lambda functions can be used within a class to access member variables of that class, but must explicitly capture this.

Until C++14, one could only capture by reference. C++17 introduced a solution for cleanly capturing *this by value.

c-17-new-features's People

Contributors

spencersharp2010 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.