Skip to content

Commit 1bcd71e

Browse files
committed
Lec 06 and 07 updates
1 parent 0fa25e2 commit 1bcd71e

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

06_advanced_templates/06_07_dispatch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ std::enable_if_t<!has_print_to_v<T>> dump(T t) {
9999

100100
// Will work automatically for all classes matching the pattern
101101

102-
#endif
102+
#endif
103103

104104
/// Option 3: C++17 "if constexpr" ////////////////////////////////////////////
105105

06_advanced_templates/06_08_decltype_declval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ int main() {
2222
std::cout << sizeof(fun()) << std::endl;
2323
// also allowed -- also an unevaluated context
2424

25-
// fun();
25+
//fun();
2626
// ^ this causes a linker error (used in evaluated context)
2727
}

06_advanced_templates/06_09_sfinae.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ bool is_odd(int[1][N % 2 == 0 ? 1 : -1] = nullptr) {
1313

1414
int main() {
1515

16-
// int x[-1];
16+
//int x[-1];
1717
// ^ this is a compiler error - we can't use a negative array length
1818

1919
std::cout << "is_odd<0>() : " << is_odd<0>() << std::endl;

06_advanced_templates/06_advanced_templates.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22-
<ClCompile Include="06_06_tuple_includes_prime.cpp" />
22+
<ClCompile Include="06_10_crtp.cpp" />
2323
</ItemGroup>
2424
<PropertyGroup Label="Globals">
2525
<VCProjectVersion>15.0</VCProjectVersion>

07_libraries/07_02_threads.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
void replace_string_thread(std::string& target, std::mutex& target_mutex,
77
const std::string to_replace, const std::string replacement) {
8-
std::lock_guard<std::mutex> lock(target_mutex);
8+
std::lock_guard lock(target_mutex);
99
// ^ in C++17 we can omit the template parameter (deduction guides)
10+
// in earlier versions we need to explicitly write "<std::mutex>"
1011
// lock_guard is a *scoped lock*
1112
target.replace(target.find(to_replace), to_replace.size(), replacement);
1213
}

07_libraries/07_03_future_fs.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
#include <iostream>
22
#include <string>
33
#include <future>
4+
#include <chrono>
45

56
#include <filesystem>
67
namespace fs = std::filesystem;
78

8-
// Note: needs to be linked with -lstdc++fs (in GCC)
9+
// Note: needs to be linked with -lstdc++fs (in GCC 8.2)
10+
// experimental in earlier compilers
911

1012
int main() {
13+
using namespace std::chrono_literals;
1114

1215
auto largest_future = std::async([]() {
1316
// find largest file in working directory
1417
decltype(fs::file_size(std::declval<fs::path>())) largest_size = 0;
1518
fs::path largest;
16-
for(auto& p : fs::directory_iterator(".")) {
17-
auto path = p.path();
18-
auto s = fs::file_size(path);
19-
if(s > largest_size) {
20-
largest_size = s;
21-
largest = path;
19+
try {
20+
for(auto& p : fs::recursive_directory_iterator("R:/")) {
21+
if(p.is_regular_file()) {
22+
auto path = p.path();
23+
auto s = fs::file_size(path);
24+
if(s > largest_size) {
25+
largest_size = s;
26+
largest = path;
27+
}
28+
}
2229
}
2330
}
31+
// when dealing with files, getting an exception isn't unlikely
32+
catch(std::exception& e) {
33+
std::cerr << std::endl << "Exception: " << e.what() << std::endl;
34+
}
2435
return largest;
2536
});
2637

27-
std::cout << "Searching for largest file..." << std::endl;
38+
std::cout << "Searching for largest file...";
2839

2940
// ... other work / progress bar / etc
41+
while(largest_future.wait_for(250ms) != std::future_status::ready) {
42+
std::cout << ".";
43+
}
3044

31-
std::cout << "Largest file: " << largest_future.get() << std::endl;
45+
std::cout << std::endl << "Largest file: " << largest_future.get().generic_string() << std::endl;
3246
}

07_libraries/07_libraries.vcxproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22-
<ClCompile Include="07_10_eigen.cpp" />
22+
<ClCompile Include="07_04_boost_format.cpp" />
2323
</ItemGroup>
2424
<PropertyGroup Label="Globals">
2525
<VCProjectVersion>15.0</VCProjectVersion>
@@ -77,12 +77,24 @@
7777
<RunCodeAnalysis>false</RunCodeAnalysis>
7878
<EnableCppCoreCheck>true</EnableCppCoreCheck>
7979
<IncludePath>D:\dev\eigen;$(IncludePath)</IncludePath>
80+
<OutDir>$(TMP)\$(ProjectName)\$(Platform)\$(Configuration)\</OutDir>
81+
<IntDir>$(TMP)\$(ProjectName)\tmp\$(Platform)\$(Configuration)\</IntDir>
8082
</PropertyGroup>
8183
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
8284
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
8385
<RunCodeAnalysis>false</RunCodeAnalysis>
8486
<EnableCppCoreCheck>true</EnableCppCoreCheck>
8587
<IncludePath>D:\dev\eigen;$(IncludePath)</IncludePath>
88+
<OutDir>$(TMP)\$(ProjectName)\$(Platform)\$(Configuration)\</OutDir>
89+
<IntDir>$(TMP)\$(ProjectName)\tmp\$(Platform)\$(Configuration)\</IntDir>
90+
</PropertyGroup>
91+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
92+
<OutDir>$(TMP)\$(ProjectName)\$(Platform)\$(Configuration)\</OutDir>
93+
<IntDir>$(TMP)\$(ProjectName)\tmp\$(Platform)\$(Configuration)\</IntDir>
94+
</PropertyGroup>
95+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
96+
<OutDir>$(TMP)\$(ProjectName)\$(Platform)\$(Configuration)\</OutDir>
97+
<IntDir>$(TMP)\$(ProjectName)\tmp\$(Platform)\$(Configuration)\</IntDir>
8698
</PropertyGroup>
8799
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
88100
<ClCompile>

0 commit comments

Comments
 (0)