Skip to content
Snippets Groups Projects
Commit 66fcdb66 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Common material

parent 9df6204f
Branches
Tags
No related merge requests found
Showing with 698 additions and 67 deletions
# High-performance scientific computing in C++
# High-performance scientific computing in C++ 2022
Course material for the PRACE Advanced Training Course
"High-performance scientific computing in C++",
7 --10 June 2022, organized by the Jülich Supercomputing Centre,
Forschungszentrum Jülich, Germany.
# hpcxx2022
## Getting started
You will need to pull from this repository periodically during this course.
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
If your computer does not have "git", you will need to use the download
button to the left of the "Clone" button in the gitlab page.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Recommended way of working with this material
## Add your files
The downloaded or cloned material from this repository will be updated
during the course days. You should keep one copy of the examples and
exercises in the pristine form, and work on your own duplicates.
For example, to work on the examples in Chapter1 of the companion book,
do as follows:
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.jsc.fz-juelich.de/sdlbio-courses/hpcxx2022.git
git branch -M main
git push -uf origin main
```bash
cd Book/chapter1
cp -r examples mine
cd mine
clang++ -std=c++20 math_functions.cc
./a.out
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.jsc.fz-juelich.de/sdlbio-courses/hpcxx2022/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
Only examples (which includes some exercises) and solutions need to
be duplicated. You don't need to clone the entire material as that
includes the PDFs of the book, course slides and other utillities.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
## Testing your set up
# Editing this README
To work with this material, you will need to have a fairly up-to-date
C++ compiler. GCC version 11.2 or Clang version 13.0 or later are
recommended.
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
Microsoft's Visual C++ compiler has excellent support for C++20, if
you have the latest version installed. During the course, we will focus
on using open source tools, which are available to everyone, rather
than proprietary tools.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
To test your setup, proceed as follows:
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
```bash
cd wherever_you_downloaded_or_cloned_this_thing
cd test_your_setup
g++ hello.cc -o hello.gcc
./hello.gcc
clang++ hello.cc -o hello.clang
./hello.clang
g++ -std=c++20 hello_c++20.cc -o hello_cxx20.gcc
./hello_cxx20.gcc
clang++ -std=c++20 hello_c++20.cc -o hello_cxx20.clang
./hello_cxx20.clang
```
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
And finally to test if your compiler can at least partially
handle C++20 modules, try the two remaining examples:
```bash
clang++ -std=c++20 -stdlib=libc++ -fmodules hello_m.cc -o hello_m.clang
./hello_m.clang
clang++ -std=c++20 -stdlib=libc++ -fmodules hello_c++20_m.cc -o hello_cxx20_m.clang
./hello_cxx20_m.clang
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
g++ -std=c++20 -fmodules-ts -xc++-system-header concepts
g++ -std=c++20 -fmodules-ts hello_m.cc -o hello_m.gcc
g++ -std=c++20 -fmodules-ts hello_m.cc -o hello_cxx20_m.gcc
./hello_m.gcc
./hello_cxx20_m.gcc
```
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
# Using aliases to save some typing
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
As the above examples show, we will often need to type a
lot of options to compile our programs during this course,
since we want to use C++20, which is not the default
standard for the compilers yet. A vew convenient aliases
have been prepared which you can use to shorten the
above compilation steps. To use them, do as follows:
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
```bash
source course_material_dir/code/bash/aliases.sh
```
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
Once you have done that, you can shorten the above
steps to the following:
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
```bash
C hello.cc
C hello_c++20.cc
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
Cm hello_m.cc
Cm hello_c++20_m.cc
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
Gh iostream
Gh concepts
Gm hello_m.cc
Gm hello_c++20_m.cc
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
alias cf="clang-format --style=WebKit -i"
alias C="clang++ -std=c++20 -stdlib=libc++ -Ofast"
alias Cm="clang++ -std=c++20 -stdlib=libc++ -Ofast -fmodules -fimplicit-module-maps -fprebuilt-module-path=."
alias Cixx="clang++ -std=c++20 -stdlib=libc++ -Ofast -fmodules -fimplicit-module-maps --precompile -xc++-module -fprebuilt-module-path=."
alias G="g++ -std=c++20 -pedantic -Wall -O3"
alias Gm="g++ -std=c++20 -fmodules-ts -pedantic -Wall -O3"
alias Gh="g++ -std=c++20 -fmodules-ts -xc++-system-header -O3"
#!/bin/bash
# ***********************************************
# High-performance scientific computing in C++
# 07 -- 10 June 2022
# Jülich Supercomputing Centre
# ***********************************************
rmspecial() {
sed -e 's/[]\/()$*.^|[]/\\&/g' <<< "$1"
}
# Usage:
# prepend_path NAME_OF_PATH_ENV_VAR LOCATION_TO_PREPEND
prepend_path() {
local pathtochange prevpath
pathtochange=$1
prevpath=${!pathtochange}
if [ -d "$2" ]; then
[[ ":${prevpath}:" != *":$2:"* ]] && prevpath="$2:${prevpath}"
fi
eval "export ${pathtochange}=${prevpath}"
}
# Usage:
# remove_path_element NAME_OF_PATH_ENV_VAR LOCATION_TO_REMOVE_FROM_IT
remove_path_element() {
local pathtochange prevpath toremove
pathtochange=$1
prevpath=${!pathtochange}
toremove=$(rmspecial $2)
prevpath=$(eval "echo ${prevpath} | sed -e 's#$toremove:##'")
prevpath=$(eval "echo ${prevpath} | sed -e 's#:$toremove##'")
eval "export ${pathtochange}=${prevpath}"
}
# Usage:
# pathadd /a/b/c/bin
pathadd() {
prepend_path PATH $1
}
# Usage:
# pathrm /a/b/c/bin
pathrm() {
remove_path_element PATH $1
}
libpathadd() {
prepend_path LD_LIBRARY_PATH $1
prepend_path LD_RUN_PATH $1
prepend_path LIBRARY_PATH $1
}
libpathrm() {
remove_path_element LD_LIBRARY_PATH $1
remove_path_element LD_RUN_PATH $1
remove_path_element LIBRARY_PATH $1
}
incpathadd() {
prepend_path CPATH $1
}
incpathrm() {
remove_path_element CPATH $1
}
cmpathadd() {
prepend_path CMAKE_PREFIX_PATH $1
}
cmpathrm() {
remove_path_element CMAKE_PREFIX_PATH $1
}
#pragma once
#include <iterator>
#include <type_traits>
#include <concepts>
template <std::integral T>
struct CountingIterator {
using difference_type = std::make_signed_t<T>;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;
constexpr auto operator++() noexcept -> CountingIterator { return { ++val }; }
constexpr auto operator++(int) noexcept -> CountingIterator { return { val++ }; }
constexpr auto operator--() noexcept -> CountingIterator { return { --val }; }
constexpr auto operator--(int) noexcept -> CountingIterator { return { val-- }; }
constexpr auto operator+=(difference_type d)
{
val += d;
return *this;
}
constexpr auto operator-=(difference_type d)
{
val -= d;
return *this;
}
constexpr auto operator<(CountingIterator other) const noexcept -> bool { return val < other.val; }
constexpr auto operator==(CountingIterator other) const noexcept -> bool { return val == other.val; }
constexpr auto operator!=(CountingIterator other) const noexcept -> bool { return val != other.val; }
constexpr auto operator>(CountingIterator other) const noexcept -> bool { return val > other.val; }
constexpr auto operator<=(CountingIterator other) const noexcept -> bool { return val <= other.val; }
constexpr auto operator>=(CountingIterator other) const noexcept -> bool { return val >= other.val; }
constexpr auto operator*() const noexcept { return val; }
constexpr auto operator*() noexcept -> auto& { return val; }
constexpr auto operator->() noexcept -> auto* { return &val; }
constexpr auto operator-(CountingIterator other) const noexcept -> difference_type { return val - other.val; }
constexpr auto operator+(difference_type d) const noexcept -> CountingIterator { return { val + d }; }
constexpr auto operator[](size_t d) const noexcept { return val + d; }
T val {};
};
template <std::integral T>
struct Sequence {
constexpr auto begin() const noexcept { return beg; }
constexpr auto end() const noexcept { return nde; }
using iterator = CountingIterator<T>;
iterator beg{};
iterator nde{std::numeric_limits<T>::max()};
Sequence(T b, T e) noexcept : beg{b}, nde{e} {}
explicit Sequence(T b) noexcept : beg{b} {}
};
template <std::integral T>
constexpr auto algo_counter(T t) noexcept -> CountingIterator<T> { return {t}; }
#pragma once
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
class FileAsLines {
public:
using iterator = std::vector<std::string>::iterator;
void read(std::filesystem::path nm)
{
std::ifstream fin { nm };
std::string line;
while (getline(fin, line))
mylines.push_back(line);
}
FileAsLines(std::filesystem::path nm) { read(nm); }
FileAsLines() = default;
FileAsLines(const FileAsLines&) = default;
FileAsLines(FileAsLines&&) = default;
FileAsLines& operator=(const FileAsLines&) = default;
FileAsLines& operator=(FileAsLines&&) = default;
~FileAsLines() = default;
inline size_t size() const { return mylines.size(); }
inline bool empty() const { return mylines.empty(); }
inline const std::string& operator[](size_t i) const { return mylines[i]; }
inline std::string& operator[](size_t i) { return mylines[i]; }
inline const std::string& front() const { return mylines.front(); }
inline std::string& front() { return mylines.front(); }
inline const std::string& back() const { return mylines.back(); }
inline std::string& back() { return mylines.back(); }
inline decltype(auto) begin() { return mylines.begin(); }
inline decltype(auto) end() { return mylines.end(); }
inline decltype(auto) begin() const { return mylines.cbegin(); }
inline decltype(auto) end() const { return mylines.cend(); }
private:
std::vector<std::string> mylines;
};
std::ostream& operator<<(std::ostream& os, const FileAsLines& lines);
#pragma once
#include <iomanip>
#include <iostream>
#include <string>
class Vbose {
public:
Vbose()
{
std::cout << "Default constructor of object at " << ((size_t)this) << "\n";
}
inline auto getval() const { return nm; }
inline void setval(const std::string& nw) { nm = nw; }
Vbose(const Vbose& v)
: nm(v.nm)
{
std::cout << "Copy constructor of object at " << ((size_t)this) << ". ";
std::cout << "Source for copy is at " << ((size_t)&v) << "\n";
}
Vbose(Vbose&& v) noexcept
: nm(std::move(v.nm))
{
std::cout << "Move constructor of object at " << ((size_t)this) << ". ";
std::cout << "Source for move is at " << ((size_t)&v) << "\n";
}
Vbose(std::string gs) noexcept
: nm(gs)
{
std::cout << "Constructor of object at " << ((size_t)this) << ",";
std::cout << " using string " << std::quoted(gs) << "\n";
}
auto operator=(const Vbose& v) -> Vbose&
{
std::cout << "Assignment operator: LHS @ " << ((size_t)this) << "(" << nm << "), ";
std::cout << "RHS @ " << ((size_t)&v) << "(" << std::quoted(v.nm) << ")\n";
if (this != &v) {
nm = v.nm;
}
return *this;
}
auto operator=(Vbose&& v) -> Vbose&
{
std::cout << "Move assignment operator: LHS @ " << ((size_t)this) << "(" << std::quoted(nm) << "), ";
std::cout << "RHS @ " << ((size_t)&v) << "(" << std::quoted(v.nm) << ")\n";
std::swap(nm, v.nm);
return *this;
}
~Vbose()
{
std::cout << "Destructor of object at " << ((size_t)this) << " with data " << std::quoted(nm) << "\n";
}
auto operator+(const Vbose& v) -> Vbose
{
std::cout << "Inside operator + ()\n";
return {nm + "+" + v.nm};
}
auto value() const noexcept -> std::string
{
return nm;
}
void value(const std::string& vl)
{
std::cout << "Changing internal value of object at " << ((size_t)this) << " from " << nm << " to " << vl << "\n";
nm = vl;
}
private:
std::string nm { "Uninitialized" };
};
#include <iostream>
#include <bitset>
#include <cstddef>
#include <iomanip>
namespace cxx2022 {
using byte = unsigned char;
template <class T>
auto bits(T var)
{
std::remove_cv_t<std::remove_reference_t<T>> loc{var};
unsigned long bytes{0UL};
byte* in = reinterpret_cast<byte *>(&loc);
byte* out = reinterpret_cast<byte *>(&bytes);
std::copy(in, in + sizeof(loc), out);
return std::bitset<8*sizeof(decltype(loc))>(bytes) ;
}
void showbits(double var)
{
std::cout << "bits for double value " << var << "\n";
auto b = bits(var).to_string();
std::cout << "raw = " << b << "\n";
std::cout << "sign exponent mantissa\n"
<< std::setw(6) << std::left << b[0]
<< std::setw(16) << std::left << b.substr(1UL, 11)
<< " " << b.substr(12) << "\n\n";
}
void showbits(float var)
{
std::cout << "bits for float value " << var << "\n";
auto b = bits(var).to_string();
std::cout << "raw = " << b << "\n";
std::cout << "sign exponent mantissa\n"
<< std::setw(6) << std::left << b[0]
<< std::setw(16) << std::left << b.substr(1UL, 8)
<< " " << b.substr(9) << "\n\n";
}
template <class T>
void showbits(T var)
{
std::cout << "bits for " << var << " with non-floating point type : " << bits(var) << "\n";
}
}
#include <version>
#ifdef __cpp_lib_format
#include<format>
namespace fmt = std;
using std::format;
#elif __has_include (<fmt/format.h>)
#define FMT_HEADER_ONLY
#include<fmt/core.h>
#include<fmt/format.h>
using fmt::format;
#warning Using external format.h header from the fmt library.
#endif
#include <version>
#ifdef __cpp_lib_ranges
#include<ranges>
namespace sr = std::ranges;
namespace sv = std::views;
#elif __has_include (<range/v3/all.hpp>)
#include<range/v3/all.hpp>
namespace sr = ranges;
namespace sv = ranges::views;
#warning Using rangesv3 3rd party library
#else
#error No suitable header for C++20 ranges was found!
#endif
#pragma once
#include <tuple>
#include <iostream>
#include <iomanip>
template <class ... Args>
std::ostream & operator<<(std::ostream & strm, const std::tuple<Args...> & t)
{
using namespace std;
auto print_one = [&strm](const auto & onearg) -> ostream & {
using bare_type = remove_cv_t<remove_reference_t<decltype(onearg)>>;
if constexpr (is_same_v<bare_type, string>)
strm << quoted(onearg);
else
strm << onearg;
return strm;
};
auto print_components = [&](const auto & ... args){
((print_one(args) << ", "), ...);
};
strm << "[";
apply(print_components, t);
return strm <<"]";
}
#pragma once
#include <algorithm>
#include <iomanip>
#include <iosfwd>
#include <cxx20ranges>
#include <string>
namespace output {
template <class C>
concept RawArray = std::is_array_v<C>;
template <class C>
concept ArrayClass = requires(C c)
{
{ c[1ul] };
{ c.size() };
};
template <class C>
concept StringLike = std::convertible_to<C, std::string_view>;
namespace {
//auto size(const Container auto& C) { return C.size(); }
auto size(const RawArray auto& C) { return std::extent_v<std::remove_cvref_t<decltype(C)>>; }
}
template <class C>
concept ArrayLike = (not StringLike<C>)and(ArrayClass<C> or RawArray<C>);
template <class T>
void write_possibly_quoted(std::ostream& os, T&& t)
{
if constexpr (StringLike<T>) {
os << std::quoted(t);
} else {
os << t;
}
}
std::ostream& operator<<(std::ostream& os, const ArrayLike auto& c)
{
os << "[";
auto sz = size(c);
for (size_t i {}; i != sz; ++i) {
write_possibly_quoted(os, c[i]);
if (i + 1 < sz)
os << ", ";
}
return os << "]";
}
template <class T>
requires(not StringLike<T> and sr::range<T>)
std::ostream&
operator<<(std::ostream& os, T&& r)
{
os << "[";
for (auto el : r) {
write_possibly_quoted(os, el);
os << ", ";
}
return os << "]";
}
template <class SepType>
class rprinter {
std::ostream* strmptr = nullptr;
SepType sep;
public:
rprinter(SepType s)
: sep { s }
{
}
rprinter(const rprinter&) noexcept = default;
std::ostream& stream() { return *strmptr; }
void stream(std::ostream& os) { strmptr = &os; }
const auto& separator() const noexcept { return sep; }
};
template <>
class rprinter<void> {
std::ostream* strmptr = nullptr;
public:
std::ostream& stream() { return *strmptr; }
void stream(std::ostream& os) { strmptr = &os; }
};
template <class T>
rprinter<T> operator<<(std::ostream& os, rprinter<T> r)
{
r.stream(os);
return { r };
}
template <class T>
std::ostream& operator<<(rprinter<T> vp, sr::range auto&& t)
{
if constexpr (std::is_same_v<T, void>) {
for (auto&& el : t)
vp.stream() << el;
} else {
if (not sr::empty(t)) {
vp.stream() << *sr::begin(t);
sr::for_each(t | sv::drop(1), [&vp](auto&& el) {
vp.stream() << vp.separator() << el;
});
}
}
return vp.stream();
}
inline rprinter<void> unseparated;
inline rprinter<const char*> comma_separated { ", " };
}
#pragma once
#include <chrono>
#include <cmath>
#include <iostream>
#include <string_view>
template <typename Callable, typename... Args>
void timeit(std::string_view testname, unsigned reps, Callable&& f, Args&&... args)
{
using namespace std::chrono;
double tmin {}, tmax {}, ttot {}, t2tot {};
for (auto i = 0u; i < reps; ++i) {
auto start = high_resolution_clock::now();
std::forward<Callable>(f)(std::forward<Args>(args)...);
auto end = high_resolution_clock::now();
auto intv = duration<double>(end - start).count();
if (i == 0u) {
tmin = tmax = intv;
} else {
tmax = std::max(tmax, intv);
tmin = std::min(tmin, intv);
}
ttot += intv;
t2tot += (intv * intv);
}
enum class TUnit { sec,
msec,
usec,
nsec };
TUnit u = TUnit::sec;
if (tmin < 1.0)
u = TUnit::msec;
if (tmin < 1.e-3)
u = TUnit::usec;
if (tmin < 1.e-6)
u = TUnit::nsec;
double tmean = ttot / reps;
double t2mean = t2tot / reps;
double tstdv = sqrt(t2mean - tmean * tmean);
std::string unitstring { " seconds" };
double conv { 1.0 };
switch (u) {
case TUnit::msec:
unitstring = " milliseconds";
conv = 1000.0;
break;
case TUnit::usec:
unitstring = " microseconds";
conv = 1000000.0;
break;
case TUnit::nsec:
unitstring = " nanoseconds";
conv = 1000000000.0;
break;
case TUnit::sec:
default:
break;
};
std::cout << "Timing results for " << testname << " from " << reps << " repeatitions...\n";
std::cout << "Mean = " << tmean * conv << unitstring << "\n";
std::cout << "Std. dev = " << tstdv * conv << unitstring << "\n";
std::cout << "Minimum = " << tmin * conv << unitstring << "\n";
std::cout << "Maximum = " << tmax * conv << unitstring << "\n";
}
# Compile:
## hello.cc
g++ hello.cc
clang++ hello.cc
## hello_c++20.cc
g++ -std=c++20 hello_c++20.cc
clang++ -std=c++20 -stdlib=libc++ hello_c++20.cc
## hello_m.cc
With gcc:
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
g++ -std=c++20 -fmodules-ts hello_m.cc
with clang:
clang++ -std=c++20 -stdlib=libc++ -fmodules hello_m.cc
## hello_c++20_m.cc
With gcc:
g++ -std=c++20 -fmodules-ts -xc++-system-header concepts
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
g++ -std=c++20 -fmodules-ts hello_c++20_m.cc
With clang:
clang++ -std=c++20 -stdlib=libc++ -fmodules hello_c++20_m.cc
#include <iostream>
auto main() -> int
{
std::cout << "Hello, world!\n";
}
#include <iostream>
#include <concepts>
auto message(auto&& x)
{
std::cout << x << "\n";
}
template <class T>
concept is_a_number = std::integral<T> or std::floating_point<T>;
auto main() -> int
{
message("Hello, world!");
auto n = 1234.0;
if constexpr (is_a_number<decltype(n)>)
std::cout << n << " is a number.\n";
else
std::cout << n << " is not a number!\n";
}
import <iostream>;
import <concepts>;
auto message(auto&& x)
{
std::cout << x << "\n";
}
template <class T>
concept is_a_number = std::integral<T> or std::floating_point<T>;
auto main() -> int
{
message("Hello, world!");
auto n = 1234.0;
if constexpr (is_a_number<decltype(n)>)
std::cout << n << " is a number.\n";
else
std::cout << n << " is not a number!\n";
}
import <iostream>;
auto main() -> int
{
std::cout << "Hello, world!\n";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment