-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReturnValue.cpp
78 lines (64 loc) · 1.97 KB
/
ReturnValue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* \file ReturnValue.cpp
* \brief
*
* \see https://en.cppreference.com/w/cpp/language/constexpr
*
* - Т.е. constexpr - это не гарантия вычисления на этапе компиляции?
*
* Нет. Правильнее сказать, что оно может вычислиться на этапе компиляции, но не факт.
* Как я и приводил пример в одном случае считалось на этапе компиляции, в другом - в рантайме.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
constexpr std::size_t
// consteval std::size_t
factorial(const std::size_t n)
{
if (n == 0) {
return 1;
}
return ::factorial(n - 1) * n;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
/**
* \note noexcept
*
* Because the noexcept operator always returns true for a constant expression,
* it can be used to check if a particular invocation of a constexpr function
* takes the constant expression branch:
*/
{
constexpr bool bRv = noexcept( ::factorial(5) ); // true, f() is a constant expression
// static_assert(bRv); - maybe fail
std::cout << "noexcept: static_assert = " << bRv << std::endl;
}
// const
{
std::cout << "noexcept: " << noexcept( ::factorial(5) ) << std::endl;
}
// const
{
const std::size_t uiRv = ::factorial(5);
static_assert(uiRv == 120);
std::cout << STD_TRACE_VAR(uiRv) << std::endl;
}
// constexpr
{
constexpr std::size_t uiRv = ::factorial(5);
static_assert(uiRv == 120);
std::cout << STD_TRACE_VAR(uiRv) << std::endl;
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
noexcept: static_assert = 1
noexcept: 1
uiRv: 120
uiRv: 120
#endif