From ff0507d6db02b9fc43e621d51b73419018bb38c8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 24 Jul 2023 15:00:59 +0200 Subject: A1: c++20 variation and report. --- .gitignore | 5 +++++ Makefile | 13 +++++++------ a1/.gitignore | 2 ++ a1/Makefile | 9 ++++++++- a1/exercise.tex | 36 ++++++++++++++++++++++++++++++++++++ a1/hello-cpp20.cc | 21 +++++++++++++++++++++ preamble.tex | 3 +-- 7 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 a1/.gitignore create mode 100644 a1/exercise.tex create mode 100644 a1/hello-cpp20.cc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..feff949 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +gcm.cache +*.zip +*.pdf +*.aux +*.log \ No newline at end of file diff --git a/Makefile b/Makefile index ce36bd1..4b3b255 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ +PRE=au_BentBisballeNyeng_ all: A1 A1: - zip au_BentBisballeNyeng_a1.zip a1/hello.cc - zip au_BentBisballeNyeng_a1.zip a1/hello-cpp20.cc - zip au_BentBisballeNyeng_a1.zip a1/Makefile - xelatex --jobname=au_BentBisballeNyeng.tex a1/exercise.tex + zip ${PRE}$@.zip a1/hello.cc + zip ${PRE}$@.zip a1/hello-cpp20.cc + zip ${PRE}$@.zip a1/Makefile + xelatex -halt-on-error -jobname=${PRE}$@ -auxdir=build a1/exercise.tex + rm -f ${PRE}$@.aux ${PRE}$@.log clean: - rm -f au_BentBisballeNyeng*.log - rm -f au_BentBisballeNyeng*.aux + rm -f ${PRE}* diff --git a/a1/.gitignore b/a1/.gitignore new file mode 100644 index 0000000..04e88da --- /dev/null +++ b/a1/.gitignore @@ -0,0 +1,2 @@ +hello +hello-cpp20 \ No newline at end of file diff --git a/a1/Makefile b/a1/Makefile index af4aa77..8bab294 100644 --- a/a1/Makefile +++ b/a1/Makefile @@ -1,3 +1,10 @@ -all: +all: hello hello-cpp20 + +hello: hello.cc g++ -std=c++20 -fmodules-ts -x c++-system-header iostream g++ -std=c++20 -fmodules-ts hello.cc -o hello + +hello-cpp20: hello-cpp20.cc + g++ -std=c++20 -fmodules-ts -x c++-system-header iostream + g++ -std=c++20 -fmodules-ts -x c++-system-header string + g++ -std=c++20 -fmodules-ts hello-cpp20.cc -o hello-cpp20 diff --git a/a1/exercise.tex b/a1/exercise.tex new file mode 100644 index 0000000..b02f83f --- /dev/null +++ b/a1/exercise.tex @@ -0,0 +1,36 @@ +\title{A1: Tool Chain} +\input{preamble.tex} + +This assignment consists of two small programs; \texttt{hello.cc} +and \texttt{hello-cpp20.cc}. +Both use \texttt{import } rather the \texttt{\#include } +which on my tool-chain (gcc on linux) requires some pre-generation in +order to work. +A \texttt{Makefile} is provided which handles all the necessary +pre-generation as well as the actual compilation of the programs. + +\section*{Exercise 1} +The \texttt{hello.cc} simply prints a \texttt{const char*} literal +string to the console using the \texttt{std::cout} output stream. + +\section*{Exercise 2} +The \texttt{hello-cpp2.cc} wraps the printing in a templated function, +utilizing a \texttt{concept} ``Printable'' to make sure that the stream +operator is implemented on the supplied type for the \texttt{std::cout} +stream. +Examples of calling the function is supplied; one using a \texttt{const + char*} literal string, one using a \texttt{std::string} literal, one +using an \texttt{int}, and finally one using a custom type which fails +compilation because it doesn't provide a stream operator implementaion +for \texttt{std::ostream}. + +Initially I had the argument for the \texttt{printIt} function being +just a reference, which failed compilation because of the +\texttt{std::string} litteral being a temporary. +Adding \texttt{const} fixed the issue. + +I originally had the idea to somehow use \texttt{std::format} for the +second part of the exercise, but since this is not yet supported by my +compiler I had to change plans. + +\end{document} diff --git a/a1/hello-cpp20.cc b/a1/hello-cpp20.cc new file mode 100644 index 0000000..76b2bbd --- /dev/null +++ b/a1/hello-cpp20.cc @@ -0,0 +1,21 @@ +import ; +import ; + +template +concept Printable = requires(T a) { std::cout << a; }; + +void printIt(const Printable auto& p) +{ + std::cout << p; +} + +struct MyType {}; + +int main() +{ + using namespace std::string_literals; + printIt("Hello Modern World!\n"); // ok, const char* string is printable + printIt("Hello Modern World!\n"s); // ok, std::string literal is printable + printIt(42); // ok, int is printable + // printIt(MyType{}); // error: doesn't adhere to Printable concept +} diff --git a/preamble.tex b/preamble.tex index 9c3cf21..ced8266 100644 --- a/preamble.tex +++ b/preamble.tex @@ -1,7 +1,6 @@ -%\def\jobname{au\_BentBisballeNyeng\_\jobname{}} \documentclass[11pt]{article} \usepackage{fancyhdr} \pagestyle{fancy} -\author{Bent Bisballe Nyeng - University of Aarhus} +\author{Bent Bisballe Nyeng University of Aarhus} \begin{document} \maketitle -- cgit v1.2.3