Skip to content

Commit 35ad3bb

Browse files
author
XuhuaHuang
committed
Update notes on template
1 parent 69ecc9a commit 35ad3bb

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

Template/ClassTemplate/calc.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
3+
template <typename T>
4+
class calc_t {
5+
public:
6+
[[nodiscard]]
7+
constexpr T multiply(T x, T y) const noexcept {
8+
return x * y;
9+
}
10+
11+
[[nodiscard]]
12+
constexpr T add(T x, T y) const noexcept {
13+
return x + y;
14+
}
15+
};
16+
17+
int main() {
18+
calc_t<int> c;
19+
20+
std::cout << c.add(10, 20) << std::endl;
21+
22+
return 0;
23+
}

Template/FunctionTemplate/main.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file main.cpp
34
* \brief
45
*
56
* \author Xuhua Huang
67
* \date November 15, 2022
78
*********************************************************************/
9+
// clang-format on
810

911
#include <iostream>
1012

1113
#include "max.hpp"
1214

1315
int main(void) {
14-
// implicit template instanciation
15-
// T is deducted to int
16-
// same for std::vector
17-
// std::vector v { 1, 2 };
18-
// std::cout << max<int>(42, 77) << "\n";
19-
std::cout << max(42, 77) << "\n";
20-
std::cout << max<double>(3.14, 3.1415) << "\n";
16+
// implicit template instanciation
17+
// T is deducted to int
18+
// same for std::vector
19+
// std::vector v { 1, 2 };
20+
// std::cout << max<int>(42, 77) << "\n";
21+
std::cout << max(42, 77) << "\n";
22+
std::cout << max<double>(3.14, 3.1415) << "\n";
2123

22-
return EXIT_SUCCESS;
24+
return 0;
2325
}

Template/FunctionTemplate/max.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1+
// clang-format off
12
/*****************************************************************//**
2-
* \file main.hpp
3+
* \file max.hpp
34
* \brief Usually the definition of a templated function is provided
45
* in the same header file, instead of a separate translation unit.
56
*
67
* \author Xuhua Huang
78
* \date November 15, 2022
89
*********************************************************************/
10+
// clang-format on
911

1012
#ifndef MAX_HPP
1113
#define MAX_HPP
1214

13-
#ifndef _IOSTREAM_
14-
#include <iostream>
15-
#endif
16-
1715
#include <concepts>
1816

1917
/**
@@ -31,9 +29,21 @@ concept SupportsLessThan = requires (T t) { t < t; };
3129

3230
/* Adding explicit constraint for T */
3331
template <typename T>
34-
requires std::copyable<T> && SupportsLessThan<T>
32+
requires std::copyable<T> && SupportsLessThan<T>
3533
T max(T a, T b) {
36-
return b < a ? a : b;
34+
return b < a ? a : b;
35+
}
36+
37+
/** \brief Returns the maximum of two values.
38+
* \tparam T The type of the values.
39+
* \param a The first value.
40+
* \param b The second value.
41+
* \return The maximum of a and b.
42+
*/
43+
template <typename T>
44+
[[nodiscard]]
45+
constexpr T maximum(T a, T b) noexcept {
46+
return (a > b) ? a : b;
3747
}
3848

3949
#endif

0 commit comments

Comments
 (0)