Skip to content

Commit

Permalink
Moved if constexpr to constexpr part and out of STL
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Ponce authored and sponce committed Oct 18, 2023
1 parent 8634921 commit 571b224
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
27 changes: 27 additions & 0 deletions talk/morelanguage/constexpr.tex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@
\end{cppcode*}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{compile-time branches}
\begin{block}{{\it if constexpr}}
\begin{itemize}
\item takes a \cppinline{constexpr} expression as condition
\item evaluates at compile time
\item key benefit: the discarded branch can contain invalid code
\end{itemize}
\end{block}
\begin{exampleblock}{Example code}
\small
\begin{cppcode*}{}
template <typename T>
auto remove_ptr(T t) {
if constexpr (std::is_pointer_v<T>) {
return *t;
} else {
return t;
}
}
int i = ...; int *j = ...;
int r = remove_ptr(i); // equivalent to i
int q = remove_ptr(j); // equivalent to *j
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[20]{Immediate functions}
\begin{block}{Motivation}
Expand Down
27 changes: 0 additions & 27 deletions talk/morelanguage/morestl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -424,30 +424,3 @@
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{compile-time branches}
\begin{block}{{\it if constexpr}}
\begin{itemize}
\item takes a \cppinline{constexpr} expression as condition
\item evaluates at compile time
\item key benefit: the discarded branch can contain invalid code
\end{itemize}
\end{block}
\begin{exampleblock}{Example code}
\small
\begin{cppcode*}{}
template <typename T>
auto remove_ptr(T t) {
if constexpr (std::is_pointer_v<T>) {
return *t;
} else {
return t;
}
}
int i = ...; int *j = ...;
int r = remove_ptr(i); // equivalent to i
int q = remove_ptr(j); // equivalent to *j
\end{cppcode*}
\end{exampleblock}
\end{frame}

0 comments on commit 571b224

Please sign in to comment.