Skip to content

Commit b482020

Browse files
committed
add lesson_6 and update utils.hpp
1 parent 00a74f3 commit b482020

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

.vscode/settings.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"cSpell.words": [
33
"dylib",
4+
"EENS",
45
"endforeach",
56
"endfunction",
7+
"Kahan",
68
"qmlc",
79
"qmlcache",
810
"qmlproject",

lesson_6/CMakeLists.txt

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# language: CMake
2+
3+
include(${REPO_FOLDER}/settings.cmake)
4+
5+
# Libraries
6+
7+
# find_package()
8+
# add_library()
9+
set(GLOBAL_LIBS)
10+
11+
# Add executable
12+
file(GLOB_RECURSE SUBDIRS LIST_DIRECTORIES true ".")
13+
14+
string(REPLACE ";" "|" PREFIX "${TASK_PREFIXES}")
15+
16+
foreach(SUBDIR ${SUBDIRS})
17+
if(IS_DIRECTORY ${SUBDIR})
18+
if("${SUBDIR}" MATCHES "${PROJECT_NAME}/(${PREFIX})[^/]*$")
19+
get_filename_component(SUBPROJECT_NAME ${SUBDIR} NAME)
20+
set(SUBPROJECT_NAME "${PROJECT_NAME}_${SUBPROJECT_NAME}")
21+
22+
# (files only in one subfolder)
23+
file(GLOB SUBPROJECT_SOURCES
24+
${SUBDIR}/${HEADERS_FORMAT}
25+
${SUBDIR}/${SOURCES_FORMAT}
26+
)
27+
28+
set(SUBPROJECT_SOURCES)
29+
30+
foreach(ITEM_HEADER IN LISTS HEADERS_FORMAT)
31+
file(GLOB ITEM_HEADER_FORMAT_SOURCES ${SUBDIR}/${ITEM_HEADER})
32+
list(APPEND SUBPROJECT_SOURCES ${ITEM_HEADER_FORMAT_SOURCES})
33+
endforeach()
34+
35+
foreach(ITEM_SOURCE IN LISTS SOURCES_FORMAT)
36+
file(GLOB ITEM_SOURCE_FORMAT_SOURCES ${SUBDIR}/${ITEM_SOURCE})
37+
list(APPEND SUBPROJECT_SOURCES ${ITEM_SOURCE_FORMAT_SOURCES})
38+
endforeach()
39+
40+
add_executable(${SUBPROJECT_NAME} ${SUBPROJECT_SOURCES})
41+
42+
target_link_libraries(${SUBPROJECT_NAME} PRIVATE ${LIB_NAME} ${GLOBAL_LIBS})
43+
target_include_directories(${SUBPROJECT_NAME} PRIVATE ${REPO_FOLDER}/${LIB_NAME})
44+
endif()
45+
endif()
46+
endforeach()
47+
48+
# For this project
49+
CopyExtraFiles({})

lesson_6/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Lesson 6: 14.03.2025
2+
3+
SFINAE

lesson_6/example_sfinae/main.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "utils.hpp"
2+
3+
class Simple {
4+
public:
5+
Simple(int var) : var_(var) {}
6+
bool DoSomething() const { return true; }
7+
bool DoSomething2() const { return var_ > 2; }
8+
9+
private:
10+
int var_;
11+
};
12+
13+
inline bool SimpleDoSomething(Simple* s) { return s->DoSomething(); }
14+
15+
inline float KahanSummation(float sum_previous, float input,
16+
float& accumulator) {
17+
IC(sum_previous, input, accumulator);
18+
19+
ICDouble(sum_previous);
20+
ICDouble(input);
21+
ICDouble(accumulator);
22+
23+
const float y = input - accumulator;
24+
ICDouble(y);
25+
26+
const float t = sum_previous + y;
27+
ICDouble(t);
28+
29+
accumulator = (t - sum_previous) - y;
30+
31+
ICDouble(t - sum_previous);
32+
ICDouble(accumulator);
33+
34+
return t;
35+
}
36+
37+
int main() {
38+
bool b_1 = true;
39+
bool b_2 = false;
40+
41+
int a = 10;
42+
auto* ptr = new Simple(a);
43+
delete ptr;
44+
45+
ptr = nullptr;
46+
47+
if (b_1 && b_2) {
48+
}
49+
50+
ptr->DoSomething(); // пока метод не затрагивает поля класса, его можно
51+
// вызывать даже с nullptr
52+
53+
// std::cout <<
54+
ptr->DoSomething2(); // с -O2 компилятор вырезает эту строку, иначе это seg
55+
// fault
56+
// << std::endl;
57+
58+
// if (ptr && ptr->DoSomething()) {
59+
// }
60+
61+
auto accumulator = 0.F;
62+
auto kh_s = KahanSummation(10.33F, 0.47F, accumulator);
63+
ICDouble(kh_s);
64+
65+
return 0;
66+
}

lib/utils.hpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cxxabi.h>
44

5+
#include <iomanip>
56
#include <iostream>
67
#include <memory>
78
#include <type_traits>
@@ -11,7 +12,15 @@
1112

1213
#ifndef IC_TYPE
1314
#define IC_TYPE(x) IC(x, detail::TypeName<decltype(x)>())
15+
#endif
1416

17+
#ifndef ICDouble
18+
#define ICDouble(value) \
19+
{ \
20+
std::stringstream ss; \
21+
ss << std::fixed << std::setprecision(19) << (value); \
22+
IC(value, ss.str()); \
23+
}
1524
#endif
1625

1726
namespace detail {
@@ -69,4 +78,4 @@ void PrintVerboseVarInfo(const T& value) {
6978
IC_TYPE(value);
7079
std::cout << "--------------------------------------------------"
7180
<< std::endl;
72-
}
81+
}

0 commit comments

Comments
 (0)