8000 GitHub - jvillasante/moderncpp: Modern C++: Snippets and Examples
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

jvillasante/moderncpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Modern C++: Snippets and Examples

GitHub Pages with snippets for Modern C++


  • We often need to copy and paste some snippets to code more productively.
  • Snippets can help us when it's not easy to remember all high levels features Modern C++ has to offer.
  • This repository contains lots of organized, reusable, and safe snippets for Modern C++.
  • All snippets are available in GitHub pages in a convenient way for copying and pasting.


Facebook QZone Weibo Reddit Twitter LinkedIn WhatsApp Line.me Telegram.me HackerNews


Table of Contents

Quick Start

This is how this repository works:

  • The snippets directory has lots of short programs with useful C++20 snippets
  • The examples directory has lots of short tasks using these snippets
  • GitHub actions ensures all snippets are working on GCC, MSVC, and Clang
  • We generate GitHub pages with all snippets by groups of tasks using mkdocs material and mdsplit

Libraries

We give preference to libraries in this order:

  1. Libraries supported by most C++ compilers
  2. Libraries accepted into the C++ standard
  3. Libraries likely to be accepted into the next C++ standard
  4. Libraries representative of existing practice

External Libraries

For external libraries, we also include a short CMake snippet in the build script with:

  • find_package to find, setup, and link the large external libraries
  • FetchContents to download, build, and link the external library

GitHub Pages

We generate GitHub pages with all snippets:

  • The GitHub pages are generated with mkdocs material and mdsplit
  • Snippets organized are categorized by groups of tasks
  • Sections are easy to explore, copy, and paste

Data types

Build script:

--8<-- "snippets/data_types/CMakeLists.txt"

Fundamental Data Types

--8<-- "snippets/data_types/data_types.cpp"

Auto

--8<-- "snippets/data_types/auto.cpp"

Constants

--8<-- "snippets/data_types/constants.cpp"

Raw arrays

--8<-- "snippets/data_types/raw_arrays.cpp"

Arrays

--8<-- "snippets/data_types/arrays.cpp"

Vectors

--8<-- "snippets/data_types/vectors.cpp"

Strings

--8<-- "snippets/data_types/strings.cpp"

Raw Pointers

--8<-- "snippets/data_types/raw_pointers.cpp"

Smart pointers

--8<-- "snippets/data_types/smart_pointers.cpp"

Move

--8<-- "snippets/data_types/move.cpp"

Aggregate initialization

--8<-- "snippets/data_types/aggregate_initialization.cpp"

Structured binding

--8<-- "snippets/data_types/structured_binding.cpp"

Control flow

Build script:

--8<-- "snippets/control_flow/CMakeLists.txt"

Hello World

--8<-- "snippets/control_flow/hello_world.cpp"

Sequential

--8<-- "snippets/control_flow/sequential.cpp"

Input

--8<-- "snippets/control_flow/cin.cpp"

Output

--8<-- "snippets/control_flow/printing.cpp"

Format

--8<-- "snippets/control_flow/format.cpp"

Operators

--8<-- "snippets/control_flow/operators.cpp"

Conditional

--8<-- "snippets/control_flow/conditional.cpp"

Loops

--8<-- "snippets/control_flow/loops.cpp"

Functions

Build script:

--8<-- "snippets/functions/CMakeLists.txt"

Function

--8<-- "snippets/functions/functions.cpp"

Lambda

--8<-- "snippets/functions/lambda.cpp"

Files

Build script:

--8<-- "snippets/files/CMakeLists.txt"

FindFilesystem.cmake:

--8<-- "cmake/FindFilesystem.cmake"

File streams

--8<-- "snippets/files/files.cpp"

Filesystem

--8<-- "snippets/files/filesystem.cpp"

Template

Build script:

--8<-- "snippets/templates/CMakeLists.txt"

Template function

--8<-- "snippets/templates/template_functions.cpp"

Template aliases

--8<-- "snippets/templates/template_alias.cpp"

Concepts

--8<-- "snippets/templates/concepts.cpp"

FindConcepts.cmake:

--8<-- "cmake/FindConcepts.cmake"

SFINAE

--8<-- "snippets/templates/sfinae.cpp"

Date and time

Build script:

--8<-- "snippets/datetime/CMakeLists.txt"

Clock

--8<-- "snippets/datetime/clock.cpp"

Datetime

--8<-- "snippets/datetime/datetime.cpp"

Random

Build script:

--8<-- "snippets/random/CMakeLists.txt"

Random

--8<-- "snippets/random/random.cpp"

Algorithm

Build script:

--8<-- "snippets/algorithm/CMakeLists.txt"

Basic Algorithms

--8<-- "snippets/algorithm/algorithms.cpp"

Ranges

--8<-- "snippets/algorithm/ranges.cpp"

Sorting

--8<-- "snippets/algorithm/sorting.cpp"

Searching

--8<-- "snippets/algorithm/searching.cpp"

Data structures

Build script:

--8<-- "snippets/data_structures/CMakeLists.txt"

Sequential Containers

--8<-- "snippets/data_structures/sequential_containers.cpp"

Associative Containers

--8<-- "snippets/data_structures/associative_containers.cpp"

Memory resource

--8<-- "snippets/data_structures/memory_resource.cpp"

FindPMR.cmake:

--8<-- "cmake/FindPMR.cmake"

Tuples

--8<-- "snippets/data_structures/tuples.cpp"

Any

--8<-- "snippets/data_structures/any.cpp"

Optional

--8<-- "snippets/data_structures/optional.cpp"

Variant

--8<-- "snippets/data_structures/variant.cpp"

Span

--8<-- "snippets/data_structures/span.cpp"

Bitset

--8<-- "snippets/data_structures/bitset.cpp"

Paradigms

Build script:

--8<-- "snippets/paradigms/CMakeLists.txt"

Polymorphism

--8<-- "snippets/paradigms/polymorphism.cpp"

Shared from this

--8<-- "snippets/paradigms/shared_from_this.cpp"

Metaprogramming

--8<-- "snippets/paradigms/metaprogramming.cpp"

CRTP

--8<-- "snippets/paradigms/CRTP.cpp"

SFINAE

--8<-- "snippets/paradigms/SFINAE.cpp"

Parallel

Build script:

--8<-- "snippets/parallel/CMakeLists.txt"

Threads

--8<-- "snippets/parallel/multithreading.cpp"

Executors

--8<-- "snippets/parallel/executors.cpp"

Timers

--8<-- "snippets/parallel/timers.cpp"

Signals

--8<-- "snippets/parallel/signals.cpp"

Async++

--8<-- "snippets/parallel/async_pools.cpp"

Networking

Build script:

--8<-- "snippets/networking/CMakeLists.txt"

Network

--8<-- "snippets/networking/network.cpp"

Async Network

--8<-- "snippets/networking/network_async.cpp"

Http server

main.cpp

--8<-- "snippets/networking/server/main.cpp"

server.hpp

--8<-- "snippets/networking/server/server.hpp"

connection_manager.hpp

--8<-- "snippets/networking/server/connection_manager.hpp"

connection.hpp

--8<-- "snippets/networking/server/connection.hpp"

header.hpp

--8<-- "snippets/networking/server/header.hpp"

reply.hpp

--8<-- "snippets/networking/server/reply.hpp"

request.hpp

--8<-- "snippets/networking/server/request.hpp"

request_parser.hpp

--8<-- "snippets/networking/server/request_parser.hpp"

mime_types.hpp

--8<-- "snippets/networking/server/mime_types.hpp"

request_handler.hpp

--8<-- "snippets/networking/server/request_handler.hpp"

mime_types.cpp

--8<-- "snippets/networking/server/mime_types.cpp"

request_parser.cpp

--8<-- "snippets/networking/server/request_parser.cpp"

reply.cpp

--8<-- "snippets/networking/server/reply.cpp"

request_handler.cpp

--8<-- "snippets/networking/server/request_handler.cpp"

connection.cpp

--8<-- "snippets/networking/server/connection.cpp"

connection_manager.cpp

--8<-- "snippets/networking/server/connection_manager.cpp"

server.cpp

--8<-- "snippets/networking/server/server.cpp"

Utilities

Build script:

--8<-- "snippets/utilities/CMakeLists.txt"

Regex

--8<-- "snippets/utilities/regex.cpp"

GUI

Build script:

--8<-- "snippets/gui/CMakeLists.txt"

Qt

--8<-- "snippets/gui/qt/qt_hello.cpp"

Build script:

--8<-- "snippets/gui/qt/CMakeLists.txt"

OpenGL + SDL

--8<-- "snippets/gui/opengl/sdl/sdl_hello.cpp"

Build script:

--8<-- "snippets/gui/opengl/sdl/CMakeLists.txt"

OpenGL + GLFW

--8<-- "snippets/gui/opengl/glfw/opengl_hello.cpp"

Build script:

--8<-- "snippets/gui/opengl/glfw/CMakeLists.txt"

IMGUI

--8<-- "snippets/gui/imgui/imgui_hello.cpp"

Build script:

--8<-- "snippets/gui/imgui/CMakeLists.txt"

Webview

--8<-- "snippets/gui/webview/webview_hello.cpp"

Build script:

--8<-- "snippets/gui/webview/CMakeLists.txt"

System tray

--8<-- "snippets/gui/tray/tray_hello.cpp"

Build script:

--8<-- "snippets/gui/tray/CMakeLists.txt"

Tests

Build script:

--8<-- "snippets/tests/CMakeLists.txt"

Catch2

--8<-- "snippets/tests/unit_tests_catch.cpp"

boost.ut

--8<-- "snippets/tests/unit_tests_ut.cpp"

Source Location

--8<-- "snippets/tests/source_location.cpp"

Plots

--8<-- "snippets/tests/plots.cpp"

Installing C++20

Many operating systems don't use C++20 by default yet. Follow these instructions to install C++20.

Linux

Update GCC:

sudo apt install build-essential
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt install gcc-10
sudo apt install g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10

Set your default compiler with:

update-alternatives --config g++

Mac OS

Download a recent version of Clang.

curl --output clang.tar.xz -L https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/clang+llvm-11.0.0-x86_64-apple-darwin.tar.xz
mkdir clang
tar -xvJf clang.tar.xz -C clang

Copy the files to usr/local/:

cd clang/clang+llvm-11.0.0-x86_64-apple-darwin
sudo cp -R * /usr/local/

Let CMake know that's the compiler you want to use.

If you want this to be your default compiler, you can set the CXX environment variable:

export CXX=/usr/local/bin/clang++

If you want to use this compiler in a single project, run CMake with these options:

-DCMAKE_C_COMPILER=/usr/local/bin/clang -DCMAKE_CXX_COMPILER=/usr/local/bin/clang++

Or tell you IDE to pass these options to CMake:

Windows

Update your Visual Studio Compiler.

The most recent version of Visual Studio should include C++20.

!!! warning We still don't know of a script for installing C++20 on Windows from the terminal.

This would be especially useful for our [build workflow](./.github/workflows/build.yml), which has not been testing MSVC since we moved to C++20.

Please let us know or open a PR if you know of such a script.

About

Modern C++: Snippets and Examples

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 87.3%
  • CMake 12.7%
0