diff --git a/.gitignore b/.gitignore index e91e50d..3b15271 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ CMakeFiles cmake_install.cmake CMakeCache.txt *.so +CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt index cd73fdc..83e32bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 2.6) -project(Effective_Modern_C++ CXX) +#project(Effective_Modern_C++ C CXX) +project(Effective_Modern_C++ C CXX) # Uncomment this to use clang++ to compile. #SET (CMAKE_CXX_COMPILER "/usr/bin/clang++-3.5") @@ -9,7 +10,13 @@ project(Effective_Modern_C++ CXX) # compiles with any C++ compiler, so make this more portable. Also, some files # should be compiled as C++11, others as C++14. Find out how we can # differentiate. -set(CMAKE_CXX_FLAGS "-std=c++11") +#set(CMAKE_CXX_FLAGS "-std=c++11") + +FIND_PACKAGE ( Threads REQUIRED ) + +set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${PROJECT_SOURCE_DIR}/cmake") + +include(cmake_tools) add_subdirectory(Item01_Understand_template_type_deduction) add_subdirectory(Item02_Understand_auto_type_deduction) diff --git a/Item01_Understand_template_type_deduction/CMakeLists.txt b/Item01_Understand_template_type_deduction/CMakeLists.txt index 40f572e..2db2f3d 100644 --- a/Item01_Understand_template_type_deduction/CMakeLists.txt +++ b/Item01_Understand_template_type_deduction/CMakeLists.txt @@ -1,13 +1,20 @@ -add_executable(pinch_of_pseudocode pinch_of_pseudocode.cpp) -add_executable(case1_const case1_const.cpp) -add_executable(case1_non_const case1_non_const.cpp) -add_executable(case1_pointer case1_pointer.cpp) -add_executable(case2_uref case2_uref.cpp) -add_executable(case3_pass_by_value case3_pass_by_value.cpp) -add_executable(array-to-pointer_decay_rule array-to-pointer_decay_rule.cpp) -add_executable(arrays_by_value arrays_by_value.cpp) -add_executable(arrays_by_reference arrays_by_reference.cpp) -add_executable(function-to-pointer_decay_rule function-to-pointer_decay_rule.cpp) +SET(NAMES_SAMPLES + pinch_of_pseudocode + case1_const + case1_non_const + case1_pointer + case3_pass_by_value + array-to-pointer_decay_rule + arrays_by_value + arrays_by_reference + function-to-pointer_decay_rule +) +subitems_add_executables("${NAMES_SAMPLES}" "") -add_library(item01 deduce_nb_array_elements.cpp - array_and_pointer_parameter_equivalence.cpp) +subitem_add_executable(NAME case2_uref SRC case2_uref.cpp ADD_FLAG_FOR_CXX_11) + +subitem_add_library( + NAME item01 + SRC deduce_nb_array_elements.cpp array_and_pointer_parameter_equivalence.cpp + ADD_FLAG_FOR_CXX_11 +) diff --git a/Item02_Understand_auto_type_deduction/CMakeLists.txt b/Item02_Understand_auto_type_deduction/CMakeLists.txt index 0da5902..5201bfb 100644 --- a/Item02_Understand_auto_type_deduction/CMakeLists.txt +++ b/Item02_Understand_auto_type_deduction/CMakeLists.txt @@ -1,5 +1,7 @@ -set_source_files_properties(function_return_type_deduction.cpp PROPERTIES COMPILE_FLAGS -std=c++14) +SET(SAMPLES_NAMES + auto_type_deduction + auto_deduction_vs_template_deduction +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") -add_executable(auto_type_deduction auto_type_deduction.cpp) -add_executable(auto_deduction_vs_template_deduction auto_deduction_vs_template_deduction.cpp) -add_executable(function_return_type_deduction function_return_type_deduction.cpp) +subitems_add_executables("function_return_type_deduction" "ADD_FLAG_FOR_CXX_14") diff --git a/Item03_Understand_decltype/CMakeLists.txt b/Item03_Understand_decltype/CMakeLists.txt index 7426dc5..195479f 100644 --- a/Item03_Understand_decltype/CMakeLists.txt +++ b/Item03_Understand_decltype/CMakeLists.txt @@ -1,11 +1,13 @@ -# TODO: check C++14 code! +## TODO: check C++14 code! +subitem_add_executable( + NAME decltype_for_complicated_lvalues + SRC decltype_for_complicated_lvalues.cpp +) -add_executable(decltype_for_complicated_lvalues decltype_for_complicated_lvalues.cpp) - -add_library(item02 problem.cpp -# decltype_auto_for_function_return_types_cpp14.cpp -# decltype_auto_for_normal_auto_variables_cpp14.cpp - typical_cases.cpp - string_deque.cpp -# return_statements_cpp14.cpp - ) +subitem_add_library( + NAME item02 + SRC problem.cpp typical_cases.cpp string_deque.cpp + # decltype_auto_for_function_return_types_cpp14.cpp + # return_statements_cpp14.cpp + ADD_FLAG_FOR_CXX_11 +) diff --git a/Item04_Know_how_to_view_deduced_types/CMakeLists.txt b/Item04_Know_how_to_view_deduced_types/CMakeLists.txt index a1bcfef..816309a 100644 --- a/Item04_Know_how_to_view_deduced_types/CMakeLists.txt +++ b/Item04_Know_how_to_view_deduced_types/CMakeLists.txt @@ -1,9 +1,9 @@ -add_executable(runtime_output01 runtime_output01.cpp) -add_executable(runtime_output02 runtime_output02.cpp) -add_executable(boost_type_index boost_type_index.cpp) +SET(NAMES_SAMPLES + runtime_output01 + runtime_output02 +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") -add_library(ide_editors.cpp - compiler_diagnostics.cpp) +subitems_add_executables("boost_type_index" "ADD_FLAG_FOR_CXX_11;USE_BOOST") -find_package(Boost 1.56 REQUIRED) -target_link_libraries(boost_type_index ${Boost_LIBRARIES}) +subitem_add_library(NAME ide_editors SRC compiler_diagnostics.cpp ADD_FLAG_FOR_CXX_11) diff --git a/Item05_Prefer_auto_to_explicit_type_declarations/CMakeLists.txt b/Item05_Prefer_auto_to_explicit_type_declarations/CMakeLists.txt index be33eb1..009caaf 100644 --- a/Item05_Prefer_auto_to_explicit_type_declarations/CMakeLists.txt +++ b/Item05_Prefer_auto_to_explicit_type_declarations/CMakeLists.txt @@ -1,5 +1,3 @@ -set_source_files_properties(auto.cpp PROPERTIES COMPILE_FLAGS -std=c++14) - #TODO: wait until clang bug fixed!!! add_executable(auto auto.cpp) -add_library(simple_joys simple_joys.cpp) +subitems_add_libraries("simple_joys" "ADD_FLAG_FOR_CXX_11") diff --git a/Item06_Use_the_explicitly_typed_initializer_idiom_when_auto_deduces_undesired_types/CMakeLists.txt b/Item06_Use_the_explicitly_typed_initializer_idiom_when_auto_deduces_undesired_types/CMakeLists.txt index b5b0867..5df9fdf 100644 --- a/Item06_Use_the_explicitly_typed_initializer_idiom_when_auto_deduces_undesired_types/CMakeLists.txt +++ b/Item06_Use_the_explicitly_typed_initializer_idiom_when_auto_deduces_undesired_types/CMakeLists.txt @@ -1,3 +1,13 @@ -add_executable(invisible_proxy_types invisible_proxy_types.cpp) -#add_executable(matrix_example matrix_example.cpp) -add_executable(typed_initializer_idiom typed_initializer_idiom.cpp) +# ps: exception in execution ! +# TODO: debug this sample ! +SET(NAME_SAMPLES + invisible_proxy_types + typed_initializer_idiom +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") + +#subitem_add_executable( +# NAME matrix_example +# SRC matrix_example.cpp +# ADD_FLAG_FOR_CXX_11 +#) diff --git a/Item07_Distinguish_between()_and_{}_when_creating_objects/CMakeLists.txt b/Item07_Distinguish_between()_and_{}_when_creating_objects/CMakeLists.txt index 51a9f1f..315a2b5 100644 --- a/Item07_Distinguish_between()_and_{}_when_creating_objects/CMakeLists.txt +++ b/Item07_Distinguish_between()_and_{}_when_creating_objects/CMakeLists.txt @@ -1,7 +1,10 @@ -add_executable(constructor_overload_resolution constructor_overload_resolution.cpp) -add_executable(initialization_values initialization_values.cpp) -add_executable(initializer_lists1 initializer_lists1.cpp) -add_executable(initializer_lists2 initializer_lists2.cpp) -add_executable(initializer_lists3 initializer_lists3.cpp) -add_executable(initializer_lists4 initializer_lists4.cpp) -add_executable(parenthesis_braces_in_templates parenthesis_braces_in_templates.cpp) +SET(NAMES_SAMPLES + constructor_overload_resolution + initialization_values + initializer_lists1 + initializer_lists2 + initializer_lists3 + initializer_lists4 + parenthesis_braces_in_templates +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") diff --git a/Item08_Prefer_nullptr_to_0_and_NULL/CMakeLists.txt b/Item08_Prefer_nullptr_to_0_and_NULL/CMakeLists.txt index c998884..f0bec1b 100644 --- a/Item08_Prefer_nullptr_to_0_and_NULL/CMakeLists.txt +++ b/Item08_Prefer_nullptr_to_0_and_NULL/CMakeLists.txt @@ -1,9 +1,9 @@ -add_executable(pointer_overload pointer_overload.cpp) -add_executable(code_clarity code_clarity.cpp) -add_executable(template_example_flat template_example_flat.cpp) +SET(NAMES_SAMPLES + pointer_overload + code_clarity + template_example_flat + template_example_templatized +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") -# TODO: compile this as C++11 -# add_executable(template_example_templatized template_example_templatized.cpp) - -# TODO: compile this as C++14 -#add_executable(template_example_templatized_cpp14 template_example_templatized_cpp14.cpp) +subitems_add_executables("template_example_templatized_cpp14" "ADD_FLAG_FOR_CXX_14") diff --git a/Item09_Prefer_alias_declarations_to_typedefs/CMakeLists.txt b/Item09_Prefer_alias_declarations_to_typedefs/CMakeLists.txt index 7644091..aca8227 100644 --- a/Item09_Prefer_alias_declarations_to_typedefs/CMakeLists.txt +++ b/Item09_Prefer_alias_declarations_to_typedefs/CMakeLists.txt @@ -1,7 +1,9 @@ -add_library(item09 function_pointers.cpp - linked_list_synonym_with_alias_template01.cpp - linked_list_synonym_with_alias_template02.cpp - linked_list_synonym_with_typedef01.cpp - linked_list_synonym_with_typedef02.cpp - # TODO wine.cpp - ) +SET(SRC_FILES + function_pointers.cpp + linked_list_synonym_with_alias_template01.cpp + linked_list_synonym_with_alias_template02.cpp + linked_list_synonym_with_typedef01.cpp + linked_list_synonym_with_typedef02.cpp + # TODO wine.cpp +) +subitem_add_library(NAME item09 SRC ${SRC_FILES} ADD_FLAG_FOR_CXX_11) diff --git a/Item10_Prefer_scoped_enums_to_unscoped_enums/CMakeLists.txt b/Item10_Prefer_scoped_enums_to_unscoped_enums/CMakeLists.txt index fd69711..fea0d01 100644 --- a/Item10_Prefer_scoped_enums_to_unscoped_enums/CMakeLists.txt +++ b/Item10_Prefer_scoped_enums_to_unscoped_enums/CMakeLists.txt @@ -1,14 +1,16 @@ -add_library(item10 unscoped_enums.cpp - scoped_enums.cpp - semantic_travesties01.cpp - semantic_travesties02.cpp - semantic_travesties03.cpp - forward_declaring.cpp - forward_declared_enums.cpp - underlying_type01.cpp - underlying_type02.cpp - unscoped_enums_useful01.cpp - unscoped_enums_useful02.cpp - unscoped_enums_useful03.cpp - unscoped_enums_useful04.cpp) +SET(SRC_FILES + scoped_enums.cpp + semantic_travesties01.cpp + semantic_travesties02.cpp + semantic_travesties03.cpp + forward_declaring.cpp + forward_declared_enums.cpp + underlying_type02.cpp + unscoped_enums_useful01.cpp + unscoped_enums_useful02.cpp + unscoped_enums_useful03.cpp + unscoped_enums_useful04.cpp +) +subitem_add_library(NAME item10 SRC ${SRC_FILES} ADD_FLAG_FOR_CXX_11) + # TODO: compile C++14 code unscoped_enums_useful04_cpp14.cpp) diff --git a/Item11_Prefer_deleted_functions_to_private_undefined_ones/CMakeLists.txt b/Item11_Prefer_deleted_functions_to_private_undefined_ones/CMakeLists.txt index 1e45242..e60e914 100644 --- a/Item11_Prefer_deleted_functions_to_private_undefined_ones/CMakeLists.txt +++ b/Item11_Prefer_deleted_functions_to_private_undefined_ones/CMakeLists.txt @@ -1,3 +1,7 @@ -add_executable(Item12 Item12.cpp) -add_library(pointer_example pointer_example.cpp) -add_library(Widget Widget.cpp) +subitems_add_executables("Item12" "ADD_FLAG_FOR_CXX_11") + +SET(NAMES_SAMPLES + pointer_example + Widget +) +subitems_add_libraries("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") diff --git a/Item12_Declare_overriding_functions_override/CMakeLists.txt b/Item12_Declare_overriding_functions_override/CMakeLists.txt index 2faea90..d14d47f 100644 --- a/Item12_Declare_overriding_functions_override/CMakeLists.txt +++ b/Item12_Declare_overriding_functions_override/CMakeLists.txt @@ -1,9 +1,18 @@ add_executable(overriding_explained overriding_explained.cpp) -add_executable(reference_qualifiers_example reference_qualifiers_example.cpp) -add_library(typical_errors typical_errors.cpp) -add_library(typical_errors_with_override_added typical_errors_with_override_added.cpp) -add_library(typical_errors_corrected typical_errors_corrected.cpp) + +SET(NAMES_SAMPLES + reference-qualified_member_functions_use_case_problem_demonstration + reference-qualified_member_functions_use_case_problem_solution + reference_qualifiers_example +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") + +SET(NAMES_SAMPLES + typical_errors + typical_errors_with_override_added + typical_errors_corrected + override_games +) +subitems_add_libraries("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11") + add_library(override_legacy_code override_legacy_code.cpp) -add_library(override_games override_games.cpp) -add_executable(reference-qualified_member_functions_use_case_problem_demonstration reference-qualified_member_functions_use_case_problem_demonstration.cpp) -add_executable(reference-qualified_member_functions_use_case_problem_solution reference-qualified_member_functions_use_case_problem_solution.cpp) diff --git a/Item13_Prefer_const_iterators_to_iterators/CMakeLists.txt b/Item13_Prefer_const_iterators_to_iterators/CMakeLists.txt index c0282cd..d94675d 100644 --- a/Item13_Prefer_const_iterators_to_iterators/CMakeLists.txt +++ b/Item13_Prefer_const_iterators_to_iterators/CMakeLists.txt @@ -1,3 +1,5 @@ add_executable(insertion_cpp98 insertion_cpp98.cpp) -# TODO: find out why this doesn't compile!!! add_executable(insertion_cpp11 insertion_cpp11.cpp) -# TODO add_executable(insertion_cpp11 insertion_cpp14.cpp) + +subitems_add_executables("insertion_cpp11" "ADD_FLAG_FOR_CXX_11") + +subitems_add_executables("insertion_cpp14" "ADD_FLAG_FOR_CXX_14") diff --git a/Item13_Prefer_const_iterators_to_iterators/insertion_cpp14.cpp b/Item13_Prefer_const_iterators_to_iterators/insertion_cpp14.cpp index 9252af3..bec814c 100644 --- a/Item13_Prefer_const_iterators_to_iterators/insertion_cpp14.cpp +++ b/Item13_Prefer_const_iterators_to_iterators/insertion_cpp14.cpp @@ -17,11 +17,20 @@ void findAndInsert(C& container, // in container, find const V& targetVal, // first occurrence const V& insertVal) // of targetVal, then { // insert insertVal - using std::cbegin; // there - using std::cend; +// using std::cbegin; // there +// using std::cend; - auto it = std::find(cbegin(container), // non-member cbegin - cend(container), // non-member cend +// auto it = std::find(cbegin(container), // non-member cbegin +// cend(container), // non-member cend +// targetVal); + + // Support of `std::cbegin()`in C++14 + // url: http://stackoverflow.com/a/31038315 + using std::begin; // there + using std::end; + + auto it = std::find(begin(container), // non-member cbegin + end(container), // non-member cend targetVal); container.insert(it, insertVal); @@ -32,6 +41,6 @@ int main() std::vector values; // as before - findAndInsert(values, 1983, 1998) + findAndInsert(values, 1983, 1998); } diff --git a/Item14_Declare_functions_noexcept_if_they_wont_emit_exceptions/CMakeLists.txt b/Item14_Declare_functions_noexcept_if_they_wont_emit_exceptions/CMakeLists.txt index 35fc7cd..ee6da6f 100644 --- a/Item14_Declare_functions_noexcept_if_they_wont_emit_exceptions/CMakeLists.txt +++ b/Item14_Declare_functions_noexcept_if_they_wont_emit_exceptions/CMakeLists.txt @@ -1,4 +1,13 @@ -add_library(never_throw_exception never_throw_exception.cpp) +subitem_add_library( + NAME never_throw_exception + SRC never_throw_exception.cpp + ADD_FLAG_FOR_CXX_11 +) + add_library(example_with_move example_with_move.cpp) -add_executable(inconsistencies inconsistencies.cpp) +subitem_add_executable( + NAME inconsistencies + SRC inconsistencies.cpp + ADD_FLAG_FOR_CXX_11 +) diff --git a/Item16_Make_const_member_functions_thread-safe/CMakeLists.txt b/Item16_Make_const_member_functions_thread-safe/CMakeLists.txt index 0ee4bcc..99ef38d 100644 --- a/Item16_Make_const_member_functions_thread-safe/CMakeLists.txt +++ b/Item16_Make_const_member_functions_thread-safe/CMakeLists.txt @@ -1,4 +1,14 @@ -# TODO: complete this CMakeLists.txt -#add_executable(insertion_cpp98 insertion_cpp98.cpp) -add_executable(undefined_behavior undefined_behavior.cpp) +subitem_add_executable( + NAME expensive_int + SRC expensive_int.cpp + ADD_FLAG_FOR_CXX_11 +) + +subitem_add_executable( + NAME undefined_behavior + SRC undefined_behavior.cpp + ADD_FLAG_FOR_CXX_11 + USE_PTHREAD +) + #add_library(roots Polynomial.cpp) diff --git a/Item17_Understand_special_member_function_generation/CMakeLists.txt b/Item17_Understand_special_member_function_generation/CMakeLists.txt index c111d52..baa8d06 100644 --- a/Item17_Understand_special_member_function_generation/CMakeLists.txt +++ b/Item17_Understand_special_member_function_generation/CMakeLists.txt @@ -1,3 +1,9 @@ +SET(SRC_FILES_Cxx11 + Widget01.cpp + Widget02.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") + add_library(itemXX Widget01.cpp Widget02.cpp Widget03.cpp diff --git a/Item18_Use_std_unique_ptr_for_exclusive-ownership_resource_management/CMakeLists.txt b/Item18_Use_std_unique_ptr_for_exclusive-ownership_resource_management/CMakeLists.txt index 7f06878..2162235 100644 --- a/Item18_Use_std_unique_ptr_for_exclusive-ownership_resource_management/CMakeLists.txt +++ b/Item18_Use_std_unique_ptr_for_exclusive-ownership_resource_management/CMakeLists.txt @@ -1,2 +1,6 @@ -#add_library(investments_cpp11 investments_cpp11.cpp) +#subitem_add_library( +# NAME investments_cpp11 +# SRC investments_cpp11.cpp +# ADD_FLAG_FOR_CXX_11 +#) #add_library(investments_cpp14 investments_cpp14.cpp) diff --git a/Item19_Use_std_shared_ptr_for_shared-ownership_resource_management/CMakeLists.txt b/Item19_Use_std_shared_ptr_for_shared-ownership_resource_management/CMakeLists.txt index 9f92139..bf17dbf 100644 --- a/Item19_Use_std_shared_ptr_for_shared-ownership_resource_management/CMakeLists.txt +++ b/Item19_Use_std_shared_ptr_for_shared-ownership_resource_management/CMakeLists.txt @@ -1,3 +1,6 @@ -add_library(custom_deleters custom_deleters.cpp) -add_library(multiple_control_blocks1 multiple_control_blocks1.cpp) -add_library(multiple_control_blocks2 multiple_control_blocks2.cpp) +SET(SAMPLES_NAMES + custom_deleters + multiple_control_blocks1 + multiple_control_blocks2 +) +subitems_add_libraries("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") diff --git a/Item20_Use_std_weak_ptr_for_std_shared_ptr-like_pointers_that_can_dangle/CMakeLists.txt b/Item20_Use_std_weak_ptr_for_std_shared_ptr-like_pointers_that_can_dangle/CMakeLists.txt index 2486727..231a4b8 100644 --- a/Item20_Use_std_weak_ptr_for_std_shared_ptr-like_pointers_that_can_dangle/CMakeLists.txt +++ b/Item20_Use_std_weak_ptr_for_std_shared_ptr-like_pointers_that_can_dangle/CMakeLists.txt @@ -1 +1,5 @@ -add_executable(creation creation.cpp) +subitem_add_executable( + NAME creation + SRC creation + ADD_FLAG_FOR_CXX_11 +) diff --git a/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/CMakeLists.txt b/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/CMakeLists.txt index e2c39eb..1da8880 100644 --- a/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/CMakeLists.txt +++ b/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/CMakeLists.txt @@ -1,5 +1,5 @@ -set_source_files_properties(reasons_for_preferring_make_functions.cpp PROPERTIES COMPILE_FLAGS -std=c++14) -set_source_files_properties(limitations_of_make_functions.cpp PROPERTIES COMPILE_FLAGS -std=c++14) - -#add_executable(reasons_for_preferring_make_functions reasons_for_preferring_make_functions.cpp) -#add_executable(limitations_of_make_functions limitations_of_make_functions.cpp) +SET(SAMPLES_NAMES + reasons_for_preferring_make_functions + limitations_of_make_functions +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_14") diff --git a/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/limitations_of_make_functions.cpp b/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/limitations_of_make_functions.cpp index 87d667c..fce9820 100644 --- a/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/limitations_of_make_functions.cpp +++ b/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/limitations_of_make_functions.cpp @@ -44,7 +44,7 @@ int main() std::unique_ptr upw(new Widget(), widgetDeleter); - std::shared_ptr spw(new Widget(), widgetDeleter); +// std::shared_ptr spw(new Widget(), widgetDeleter); // Limitation 2: within the make function, the perfect forwarding code uses @@ -82,7 +82,7 @@ int main() // final std::weak_ptr to object destroyed here; // memory for control block and object is released - std::shared_ptr pBigObj(new ReallyBigType); + std::shared_ptr pBigObj_2(new ReallyBigType); // create very large // object via new diff --git a/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/reasons_for_preferring_make_functions.cpp b/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/reasons_for_preferring_make_functions.cpp index b93a314..c555892 100644 --- a/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/reasons_for_preferring_make_functions.cpp +++ b/Item21_Prefer_std_make_unique_and_std_make_shared_to_direct_use_of_new/reasons_for_preferring_make_functions.cpp @@ -50,7 +50,7 @@ int main() // leak! processWidget(std::make_shared(), // no potential - computePriority(); // resource leak + computePriority()); // resource leak // Reason 3: a special feature of make_shared is improved efficiency. diff --git a/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/CMakeLists.txt b/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/CMakeLists.txt index d1bc764..f238ae0 100644 --- a/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/CMakeLists.txt +++ b/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/CMakeLists.txt @@ -1,4 +1,10 @@ +# TODO: need to be clean ! + #add_subdirectory(no_pimpl) + add_subdirectory(pimpl_raw_ptr) + #TODO: fix linker error!!! add_subdirectory(pimpl_unique_ptr) +add_subdirectory(pimpl_unique_ptr) + add_subdirectory(pimpl_shared_ptr) diff --git a/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_shared_ptr/CMakeLists.txt b/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_shared_ptr/CMakeLists.txt index 5e2e32d..ab27dd7 100644 --- a/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_shared_ptr/CMakeLists.txt +++ b/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_shared_ptr/CMakeLists.txt @@ -1,3 +1,9 @@ +SET(SRC_FILES_Cxx11 + widget.cpp + client.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") + include_directories(../) add_library(widget_pimpl_shared_ptr widget.cpp) diff --git a/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_unique_ptr/CMakeLists.txt b/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_unique_ptr/CMakeLists.txt index 8ca8d1f..3b45cf8 100644 --- a/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_unique_ptr/CMakeLists.txt +++ b/Item22_When_using_the_Pimpl_Idiom_define_special_member_functions_in_the_implementation_file/pimpl_unique_ptr/CMakeLists.txt @@ -1,6 +1,12 @@ +SET(SRC_FILES_Cxx11 + client.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") + include_directories(../) -set_source_files_properties(widget.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#set_source_files_properties(widget.cpp PROPERTIES COMPILE_FLAGS "-std=c++14") +add_std_cxx14_flag("widget.cpp") add_library(widget_pimpl_unique_ptr widget.cpp) diff --git a/Item23_Understand_std_move_and_std_forward/CMakeLists.txt b/Item23_Understand_std_move_and_std_forward/CMakeLists.txt index 88be2c0..af3172c 100644 --- a/Item23_Understand_std_move_and_std_forward/CMakeLists.txt +++ b/Item23_Understand_std_move_and_std_forward/CMakeLists.txt @@ -1,4 +1,12 @@ -set_source_files_properties(std_move_in_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +SET(SRC_FILES_Cxx11 + std_move_in_cpp11.cpp + annotation.cpp + move_constructor_with_move.cpp + move_constructor_with_forward.cpp + typical_use_of_std_forward.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") +add_std_cxx14_flag("std_move_in_cpp14.cpp") add_executable(typical_use_of_std_forward typical_use_of_std_forward.cpp) diff --git a/Item24_Distinguish_universal_references_from_rvalue_references/CMakeLists.txt b/Item24_Distinguish_universal_references_from_rvalue_references/CMakeLists.txt index e792c4d..536d0ec 100644 --- a/Item24_Distinguish_universal_references_from_rvalue_references/CMakeLists.txt +++ b/Item24_Distinguish_universal_references_from_rvalue_references/CMakeLists.txt @@ -1,7 +1,16 @@ +SET(SRC_FILES_Cxx11 + rrefs_examples.cpp + urefs_contexts.cpp + no_type_deduction.cpp + urefs_form.cpp + urefs_initialization.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") + # TODO: use Boost TypeIndex instead. include_directories(../../3rd_party) -set_source_files_properties(record_time.cpp PROPERTIES COMPILE_FLAGS "-std=c++14") +add_std_cxx14_flag("record_time.cpp") add_executable(urefs_initialization urefs_initialization.cpp) add_executable(urefs_form urefs_form.cpp) diff --git a/Item25_Use_std_move_on_rvalue_references_std_forward_on_universal_references/CMakeLists.txt b/Item25_Use_std_move_on_rvalue_references_std_forward_on_universal_references/CMakeLists.txt index 1141c01..3278ee2 100644 --- a/Item25_Use_std_move_on_rvalue_references_std_forward_on_universal_references/CMakeLists.txt +++ b/Item25_Use_std_move_on_rvalue_references_std_forward_on_universal_references/CMakeLists.txt @@ -1,3 +1,13 @@ +SET(SRC_FILES_Cxx11 + dont_use_move_with_urefs.cpp + dont_use_move_with_urefs_solution.cpp + rrefs_are_moveable.cpp + urefs_maybe_moveable.cpp + reduce_and_copy.cpp + matrix.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") + add_executable(dont_use_move_with_urefs dont_use_move_with_urefs.cpp) add_executable(dont_use_move_with_urefs_solution dont_use_move_with_urefs_solution.cpp) diff --git a/Item26_Avoid_overloading_on_universal_references/CMakeLists.txt b/Item26_Avoid_overloading_on_universal_references/CMakeLists.txt index f60e8d0..32161e1 100644 --- a/Item26_Avoid_overloading_on_universal_references/CMakeLists.txt +++ b/Item26_Avoid_overloading_on_universal_references/CMakeLists.txt @@ -1,4 +1,7 @@ -add_executable(logAndAdd_inefficient logAndAdd_inefficient.cpp) -add_executable(logAndAdd_with_uref logAndAdd_with_uref.cpp) -add_executable(logAndAdd_with_uref_overloaded logAndAdd_with_uref_overloaded.cpp) -add_executable(Person Person.cpp) +SET(SAMPLES_NAMES + Person + logAndAdd_inefficient + logAndAdd_with_uref + logAndAdd_with_uref_overloaded +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") diff --git a/Item27_Familiarize_yourself_with_alternatives_to_overloading_on_universal_references/CMakeLists.txt b/Item27_Familiarize_yourself_with_alternatives_to_overloading_on_universal_references/CMakeLists.txt index 3901517..5f29cc3 100644 --- a/Item27_Familiarize_yourself_with_alternatives_to_overloading_on_universal_references/CMakeLists.txt +++ b/Item27_Familiarize_yourself_with_alternatives_to_overloading_on_universal_references/CMakeLists.txt @@ -1,6 +1,18 @@ -set_source_files_properties(constraining_templates02_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -set_source_files_properties(constraining_templates03_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -set_source_files_properties(constraining_templates04_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +SET(SRC_FILES_Cxx11 + alternative_pass_by_value.cpp + tag_dispatch01.cpp + tag_dispatch02.cpp + constraining_templates01.cpp + constraining_templates02_cpp11.cpp +) +add_std_cxx11_flag("${SRC_FILES_Cxx11}") + +SET(SRC_FILES_Cxx14 + constraining_templates02_cpp14.cpp + constraining_templates03_cpp14.cpp + constraining_templates04_cpp14.cpp +) +add_std_cxx14_flag("${SRC_FILES_Cxx14}") add_library(alternatives_to_uref_overloading alternative_pass_by_value.cpp tag_dispatch01.cpp diff --git a/Item28_Understand_reference_collapsing/CMakeLists.txt b/Item28_Understand_reference_collapsing/CMakeLists.txt index e632f42..658ea41 100644 --- a/Item28_Understand_reference_collapsing/CMakeLists.txt +++ b/Item28_Understand_reference_collapsing/CMakeLists.txt @@ -1,11 +1,9 @@ -#include_directories(../../utils) - -add_executable(refs_to_refs1 - refs_to_refs.cpp) -#target_link_libraries(refs_to_refs effc++11) +SET(SAMPLES_NAMES + reference_collapsing_contexts01 + reference_collapsing_contexts02 +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") -add_executable(reference_collapsing_contexts01 - reference_collapsing_contexts01.cpp) +#include_directories(../../utils) -add_executable(reference_collapsing_contexts02 - reference_collapsing_contexts02.cpp) +add_executable(refs_to_refs1 refs_to_refs.cpp) diff --git a/Item29_Assume_that_move_operations_are_not_present_not_cheap_and_not_used/CMakeLists.txt b/Item29_Assume_that_move_operations_are_not_present_not_cheap_and_not_used/CMakeLists.txt index 9badcc9..5779627 100644 --- a/Item29_Assume_that_move_operations_are_not_present_not_cheap_and_not_used/CMakeLists.txt +++ b/Item29_Assume_that_move_operations_are_not_present_not_cheap_and_not_used/CMakeLists.txt @@ -1,2 +1,4 @@ -add_executable(moving_containers - moving_containers.cpp) +SET(SAMPLES_NAMES + moving_containers +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") diff --git a/Item30_Familiarize_yourself_with_perfect_forwarding_failure_cases/CMakeLists.txt b/Item30_Familiarize_yourself_with_perfect_forwarding_failure_cases/CMakeLists.txt index 8cc36ec..e1d357f 100644 --- a/Item30_Familiarize_yourself_with_perfect_forwarding_failure_cases/CMakeLists.txt +++ b/Item30_Familiarize_yourself_with_perfect_forwarding_failure_cases/CMakeLists.txt @@ -1,10 +1,8 @@ -add_executable(braced_initializers - braced_initializers.cpp) -add_executable(null_pointers - null_pointers.cpp) -add_executable(declaration-only_integral_static_const_data_members - declaration-only_integral_static_const_data_members.cpp) -add_executable(overloaded_function_names_and_template_names - overloaded_function_names_and_template_names.cpp) -add_executable(bitfields - bitfields.cpp) +SET(SAMPLES_NAMES + braced_initializers + null_pointers + declaration-only_integral_static_const_data_members + overloaded_function_names_and_template_names + bitfields +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") diff --git a/Item31_Avoid_default_capture_modes/CMakeLists.txt b/Item31_Avoid_default_capture_modes/CMakeLists.txt index 097fe5a..ca8e4f2 100644 --- a/Item31_Avoid_default_capture_modes/CMakeLists.txt +++ b/Item31_Avoid_default_capture_modes/CMakeLists.txt @@ -1,4 +1,6 @@ -set_source_files_properties(by_reference_capture.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") - -add_executable(by_reference_capture - Widget.cpp by_reference_capture.cpp) +SET(SRC_FILES_Cxx14 + by_reference_capture.cpp + Widget.cpp +) +add_std_cxx14_flag("${SRC_FILES_Cxx14}") +add_executable(by_reference_capture ${SRC_FILES_Cxx14}) diff --git a/Item31_Avoid_default_capture_modes/Widget.cpp b/Item31_Avoid_default_capture_modes/Widget.cpp index 2001f81..d48ac70 100644 --- a/Item31_Avoid_default_capture_modes/Widget.cpp +++ b/Item31_Avoid_default_capture_modes/Widget.cpp @@ -3,6 +3,8 @@ void Widget::addFilter() const { + Filters::FilterContainer& filters = Filters::get_instance()->filters; + // default by-value capture filters.emplace_back( [=](int value) { return value % divisor == 0; } diff --git a/Item31_Avoid_default_capture_modes/by_reference_capture.cpp b/Item31_Avoid_default_capture_modes/by_reference_capture.cpp index 77beb5c..5cfc45a 100644 --- a/Item31_Avoid_default_capture_modes/by_reference_capture.cpp +++ b/Item31_Avoid_default_capture_modes/by_reference_capture.cpp @@ -34,6 +34,8 @@ void addDivisorFilter() auto divisor = computeDivisor(calc1, calc2); + Filters::FilterContainer& filters = Filters::get_instance()->filters; + filters.emplace_back( // danger! [&](int value) { return value % divisor == 0; } // ref to // divisor @@ -60,6 +62,8 @@ void addDivisorFilter2() static auto divisor = // now static computeDivisor(calc1, calc2); + Filters::FilterContainer& filters = Filters::get_instance()->filters; + filters.emplace_back( [=](int value) // captures nothing! { return value % divisor == 0; } // refers to above static @@ -120,6 +124,8 @@ void doSomeWork() int main() { + Filters::FilterContainer& filters = Filters::get_instance()->filters; + filters.emplace_back( // see Item 42 for [](int value) { return value % 5 == 0; } // info on ); // emplace_back diff --git a/Item31_Avoid_default_capture_modes/utils.h b/Item31_Avoid_default_capture_modes/utils.h index db8351e..8c8e71d 100644 --- a/Item31_Avoid_default_capture_modes/utils.h +++ b/Item31_Avoid_default_capture_modes/utils.h @@ -4,10 +4,53 @@ #include #include -using FilterContainer = // see Item 9 for +#include + +// url: http://enki-tech.blogspot.fr/2012/08/c11-generic-singleton.html +template +class Singleton +{ +public: + template + static + T* get_instance(Args... args) + { + if (!instance_) + { + instance_ = new T(std::forward(args)...); + } + + return instance_; + } + + static + void destroy_instance() + { + delete instance_; + instance_ = nullptr; + } + +private: + static T* instance_; +}; + +template T* Singleton::instance_ = nullptr; + +class Filters: public Singleton +{ + friend class Singleton; + +private: + Filters() {} + +public: + using FilterContainer = // see Item 9 for std::vector>; // "using", Item 2 - // for std::function + // for std::function + + FilterContainer filters; +}; -FilterContainer filters; // filtering funcs +// filtering funcs #endif /* UTILS_H */ diff --git a/Item32_Use_init_capture_to_move_objects_into_closures/CMakeLists.txt b/Item32_Use_init_capture_to_move_objects_into_closures/CMakeLists.txt index 3ea74fe..5092f34 100644 --- a/Item32_Use_init_capture_to_move_objects_into_closures/CMakeLists.txt +++ b/Item32_Use_init_capture_to_move_objects_into_closures/CMakeLists.txt @@ -1,20 +1,12 @@ -set_source_files_properties(init_capture_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(init_capture_cpp14 - init_capture_cpp14.cpp) - -set_source_files_properties(init_capture_cpp11.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(init_capture_cpp11 - init_capture_cpp11.cpp - Widget.cpp) - -set_source_files_properties(move_capture1_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(move_capture1_cpp14 - move_capture1_cpp14.cpp) - -add_executable(move_capture1_cpp11 - move_capture1_cpp11.cpp) - -set_source_files_properties(move_capture2.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(move_capture2 - move_capture2.cpp) - +SET(SAMPLES_NAMES + move_capture1_cpp11 +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11") + +SET(SAMPLES_NAMES + init_capture_cpp14 + init_capture_cpp11 + move_capture1_cpp14 + move_capture2 +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_14") diff --git a/Item33_Use_decltype_on_autorr_parameters_to_std_forward_them/CMakeLists.txt b/Item33_Use_decltype_on_autorr_parameters_to_std_forward_them/CMakeLists.txt index 4ff47a7..4cf14e0 100644 --- a/Item33_Use_decltype_on_autorr_parameters_to_std_forward_them/CMakeLists.txt +++ b/Item33_Use_decltype_on_autorr_parameters_to_std_forward_them/CMakeLists.txt @@ -1,2 +1 @@ -set_source_files_properties(item35.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(item35 item35.cpp) +subitems_add_executables("item35" "ADD_FLAG_FOR_CXX_14") diff --git a/Item34_Prefer_lambdas_to_std_bind/CMakeLists.txt b/Item34_Prefer_lambdas_to_std_bind/CMakeLists.txt index 1f0b648..174e58f 100644 --- a/Item34_Prefer_lambdas_to_std_bind/CMakeLists.txt +++ b/Item34_Prefer_lambdas_to_std_bind/CMakeLists.txt @@ -1,37 +1,38 @@ +#set_source_files_properties(alarm_with_lambda_cpp11.cpp PROPERTIES COMPILE_FLAGS "-std=gnu++14 -std=c++14") add_executable(alarm_with_lambda_cpp11 alarm_with_lambda_cpp11.cpp) -set_source_files_properties(alarm_with_lambda_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(alarm_with_lambda_cpp14 alarm_with_lambda_cpp14.cpp) +#set_source_files_properties(alarm_with_lambda_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(alarm_with_lambda_cpp14 alarm_with_lambda_cpp14.cpp) -set_source_files_properties(alarm_with_lambda_and_overload_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(alarm_with_lambda_and_overload_cpp14 alarm_with_lambda_and_overload_cpp14.cpp) +#set_source_files_properties(alarm_with_lambda_and_overload_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(alarm_with_lambda_and_overload_cpp14 alarm_with_lambda_and_overload_cpp14.cpp) add_executable(alarm_with_std_bind_incorrect alarm_with_std_bind_incorrect.cpp) add_executable(alarm_with_std_bind_cpp11 alarm_with_std_bind_cpp11.cpp) -set_source_files_properties(alarm_with_std_bind_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(alarm_with_std_bind_cpp14 alarm_with_std_bind_cpp14.cpp) +#set_source_files_properties(alarm_with_std_bind_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(alarm_with_std_bind_cpp14 alarm_with_std_bind_cpp14.cpp) -set_source_files_properties(alarm_with_std_bind_and_overload_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(alarm_with_std_bind_and_overload_cpp14 alarm_with_std_bind_and_overload_cpp14.cpp) +#set_source_files_properties(alarm_with_std_bind_and_overload_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(alarm_with_std_bind_and_overload_cpp14 alarm_with_std_bind_and_overload_cpp14.cpp) add_executable(between_with_lambda_cpp11 between_with_lambda_cpp11.cpp) -set_source_files_properties(between_with_lambda_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(between_with_lambda_cpp14 between_with_lambda_cpp14.cpp) +#set_source_files_properties(between_with_lambda_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(between_with_lambda_cpp14 between_with_lambda_cpp14.cpp) -add_executable(between_with_std_bind_cpp11 between_with_std_bind_cpp11.cpp) +#add_executable(between_with_std_bind_cpp11 between_with_std_bind_cpp11.cpp) -set_source_files_properties(between_with_std_bind_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(between_with_std_bind_cpp14 between_with_std_bind_cpp14.cpp) +#set_source_files_properties(between_with_std_bind_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(between_with_std_bind_cpp14 between_with_std_bind_cpp14.cpp) add_executable(compress_with_lambda compress_with_lambda.cpp) add_executable(compress_with_std_bind compress_with_std_bind.cpp) -set_source_files_properties(polymorphic_function_objects_with_lambda_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(polymorphic_function_objects_with_lambda_cpp14 polymorphic_function_objects_with_lambda_cpp14.cpp) +#set_source_files_properties(polymorphic_function_objects_with_lambda_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") +#add_executable(polymorphic_function_objects_with_lambda_cpp14 polymorphic_function_objects_with_lambda_cpp14.cpp) add_executable(polymorphic_function_objects_with_std_bind polymorphic_function_objects_with_std_bind.cpp) diff --git a/Item34_Prefer_lambdas_to_std_bind/alarm.h b/Item34_Prefer_lambdas_to_std_bind/alarm.h index 899b772..cb4f3fc 100644 --- a/Item34_Prefer_lambdas_to_std_bind/alarm.h +++ b/Item34_Prefer_lambdas_to_std_bind/alarm.h @@ -1,6 +1,8 @@ #ifndef ALARM_H #define ALARM_H +#include + // typedef for a point in time (see Item 9 for syntax) using Time = std::chrono::steady_clock::time_point; diff --git a/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_cpp11.cpp b/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_cpp11.cpp index 19177e1..6b432e6 100644 --- a/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_cpp11.cpp +++ b/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_cpp11.cpp @@ -1,4 +1,5 @@ #include "alarm.h" +#include using namespace std::chrono; // as above using namespace std::placeholders; diff --git a/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_incorrect.cpp b/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_incorrect.cpp index 01505b7..4fbe016 100644 --- a/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_incorrect.cpp +++ b/Item34_Prefer_lambdas_to_std_bind/alarm_with_std_bind_incorrect.cpp @@ -1,4 +1,5 @@ #include "alarm.h" +#include using namespace std::chrono; // as above using namespace std::literals; diff --git a/Item35_Prefer_task-based_programming_to_thread-based/CMakeLists.txt b/Item35_Prefer_task-based_programming_to_thread-based/CMakeLists.txt index 637463f..706461b 100644 --- a/Item35_Prefer_task-based_programming_to_thread-based/CMakeLists.txt +++ b/Item35_Prefer_task-based_programming_to_thread-based/CMakeLists.txt @@ -1,3 +1,6 @@ -add_executable(software_threads software_threads.cpp) -add_executable(task_based_approach task_based_approach.cpp) -add_executable(thread_based_approach thread_based_approach.cpp) +SET(SAMPLES_NAMES + software_threads + task_based_approach + thread_based_approach +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11;USE_PTHREAD") diff --git a/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/CMakeLists.txt b/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/CMakeLists.txt index 0f02f53..bed3997 100644 --- a/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/CMakeLists.txt +++ b/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/CMakeLists.txt @@ -1,16 +1,16 @@ -add_executable(default_launch_policy default_launch_policy.cpp) +SET(NAMES_SAMPLES + default_launch_policy + guaranteeing_truly_asynchronous_execution + interesting_implications + really_async_cpp11 + thread_local_storage +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_11;USE_PTHREAD") -add_executable(guaranteeing_truly_asynchronous_execution guaranteeing_truly_asynchronous_execution.cpp) - -add_executable(interesting_implications interesting_implications.cpp) - -add_executable(really_async_cpp11 really_async_cpp11.cpp) - -set_source_files_properties(really_async_cpp14.cpp PROPERTIES COMPILE_FLAGS "-std=c++14 -stdlib=libc++") -add_executable(really_async_cpp14 really_async_cpp14.cpp) - -add_executable(thread_local_storage thread_local_storage.cpp) - -add_executable(wait-based_loops wait-based_loops.cpp) - -add_executable(wait-based_loops_fixed wait-based_loops_fixed.cpp) +SET(NAMES_SAMPLES + really_async_cpp14 + wait-based_loops + wait-based_loops_fixed +) +subitems_add_executables("${NAMES_SAMPLES}" "ADD_FLAG_FOR_CXX_14;USE_PTHREAD") +# url: http://stackoverflow.com/questions/35856969/chrono-literals-is-not-a-namespace-name diff --git a/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops.cpp b/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops.cpp index 0e58985..9879afd 100644 --- a/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops.cpp +++ b/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops.cpp @@ -19,7 +19,7 @@ void f() // f sleeps for 1 second, int main() { - auto fut = std::async(f) // run f asynchronously + auto fut = std::async(f); // run f asynchronously // (conceptually) while (fut.wait_for(100ms) != // loop until f has diff --git a/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops_fixed.cpp b/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops_fixed.cpp index 804a7b4..740566a 100644 --- a/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops_fixed.cpp +++ b/Item36_Specify_std_launch_async_if_asynchronicity_is_essential/wait-based_loops_fixed.cpp @@ -17,7 +17,7 @@ void f() int main() { - auto fut = std::async(f) // as above + auto fut = std::async(f); // as above if (fut.wait_for(0s) == // if task is std::future_status::deferred) // deferred... diff --git a/Item37_Make_std_threads_unjoinable_on_all_paths/CMakeLists.txt b/Item37_Make_std_threads_unjoinable_on_all_paths/CMakeLists.txt index 471e330..c74e3e3 100644 --- a/Item37_Make_std_threads_unjoinable_on_all_paths/CMakeLists.txt +++ b/Item37_Make_std_threads_unjoinable_on_all_paths/CMakeLists.txt @@ -1,5 +1,5 @@ -add_executable(example1 example1.cpp) -target_link_libraries(example1 pthread) - -add_executable(example2 example2.cpp) -target_link_libraries(example2 pthread) +SET(SAMPLES_NAMES + example1 + example2 +) +subitems_add_executables("${SAMPLES_NAMES}" "ADD_FLAG_FOR_CXX_11;USE_PTHREAD") diff --git a/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/CMakeLists.txt b/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/CMakeLists.txt index f2d1b90..2aea3ae 100644 --- a/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/CMakeLists.txt +++ b/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/CMakeLists.txt @@ -1,7 +1,14 @@ -# TODO: improve this CMakeLists.txt file. +subitem_add_library( + NAME item38_example1 + SRC example1.cpp + ADD_FLAG_FOR_CXX_11 + USE_PTHREAD +) -add_library(item38_example1 example1.cpp) -target_link_libraries(item38_example1 pthread) - -add_executable(item38_example2 example2.cpp) -target_link_libraries(item38_example2 pthread) +subitem_add_executable( + NAME item38_example2 + SRC example2.cpp + ADD_FLAG_FOR_CXX_11 + USE_PTHREAD +) +#ps: execution result -> 'terminate called without an active exception' diff --git a/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/example2.cpp b/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/example2.cpp index bfbc1b8..9764b28 100644 --- a/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/example2.cpp +++ b/Item38_Be_aware_of_varying_thread_handle_destructor_behavior/example2.cpp @@ -2,7 +2,7 @@ #include #include -int calcValue() {}; // func to run +int calcValue() {} // func to run int main() { // begin block diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..8fdbe53 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +cd ../build/Effective-Modern-Cpp/gcc-4.9 + +make -j3 + +cd - \ No newline at end of file diff --git a/cmake/cmake_tools.cmake b/cmake/cmake_tools.cmake new file mode 100644 index 0000000..5c2e851 --- /dev/null +++ b/cmake/cmake_tools.cmake @@ -0,0 +1,111 @@ +macro(add_flags SRC_FILES FLAGS) +# MESSAGE("Flags list to add: ${FLAGS}") +# MESSAGE("to this sources files:") + foreach(src ${SRC_FILES}) +# MESSAGE("-> ${src}") + set_source_files_properties(${src} PROPERTIES COMPILE_FLAGS ${FLAGS}) + endforeach() +endmacro() + +macro(add_std_cxx14_flag SRC_FILES) + add_flags("${SRC_FILES}" "-std=c++14") +endmacro() + +macro(add_std_cxx11_flag SRC_FILES) + add_flags("${SRC_FILES}" "-std=c++11") +endmacro() + +function(SUBITEM_ADD) + # url: https://cmake.org/cmake/help/v3.0/module/CMakeParseArguments.html + set(options + IS_EXECUTABLE + IS_LIBRARY + USE_BOOST + USE_PTHREAD + ADD_FLAG_FOR_CXX_11 + ADD_FLAG_FOR_CXX_14 + PRINT_DEBUG) + set(oneValueArgs NAME) + set(multiValueArgs SRC) + cmake_parse_arguments(SUBITEM_ADD "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) + + ######################################################## + + IF(${SUBITEM_ADD_ADD_FLAG_FOR_CXX_11} MATCHES "TRUE") + add_std_cxx11_flag("${SUBITEM_ADD_SRC}") + ENDIF() + + IF(${SUBITEM_ADD_ADD_FLAG_FOR_CXX_14} MATCHES "TRUE") + add_std_cxx14_flag("${SUBITEM_ADD_SRC}") + ENDIF() + + ######################################################## + + IF(${SUBITEM_ADD_IS_EXECUTABLE} MATCHES "TRUE") + add_executable(${SUBITEM_ADD_NAME} ${SUBITEM_ADD_SRC}) + ENDIF() + + IF(${SUBITEM_ADD_IS_LIBRARY} MATCHES "TRUE") + add_library(${SUBITEM_ADD_NAME} ${SUBITEM_ADD_SRC}) + ENDIF() + + ######################################################## + + IF(${SUBITEM_ADD_USE_BOOST} MATCHES "TRUE") + target_link_libraries(${SUBITEM_ADD_NAME} ${Boost_LIBRARIES}) + ENDIF() + + IF(${SUBITEM_ADD_USE_PTHREAD} MATCHES "TRUE") + target_link_libraries( ${SUBITEM_ADD_NAME} ${CMAKE_THREAD_LIBS_INIT}) + ENDIF() + + ######################################################## + + IF(${SUBITEM_ADD_PRINT_DEBUG} MATCHES "TRUE") + foreach(option ${options}) + MESSAGE("- ${option} = ${SUBITEM_ADD_${option}}") + endforeach() + MESSAGE("* SUBITEM_ADD_NAME = ${SUBITEM_ADD_NAME}") + MESSAGE("# SUBITEM_ADD_SRC = ${SUBITEM_ADD_SRC}") + # +# #ps: problem with foreach => cause exception ! +# foreach(option ${oneValueArgs}) +# MESSAGE("- ${option} = ${SUBITEM_ADD_${option}}") +# foreach(option ${multiValueArgs}) +# MESSAGE("- ${option} = ${SUBITEM_ADD_${option}}") +# endforeach() + + MESSAGE("? IS_EXECUTABLE = ${SUBITEM_ADD_IS_EXECUTABLE}") + MESSAGE("? IS_LIBRARY = ${SUBITEM_ADD_IS_LIBRARY}") + ENDIF() + +endfunction() + +function(SUBITEM_ADD_EXECUTABLE) + subitem_add(IS_EXECUTABLE ${ARGN}) +endfunction(SUBITEM_ADD_EXECUTABLE) + +function(SUBITEM_ADD_LIBRARY) + subitem_add(IS_LIBRARY ${ARGN}) +endfunction(SUBITEM_ADD_LIBRARY) + +function(SUBITEMS_ADD_EXECUTABLES NAMES_SAMPLES FLAGS_FOR_SAMPLES) + foreach(name_sample ${NAMES_SAMPLES}) + subitem_add_executable( + NAME ${name_sample} + SRC ${name_sample}.cpp + ${FLAGS_FOR_SAMPLES} + ) + endforeach() +endfunction() + +function(SUBITEMS_ADD_LIBRARIES NAMES_SAMPLES FLAGS_FOR_SAMPLES) + foreach(name_sample ${NAMES_SAMPLES}) + subitem_add_library( + NAME ${name_sample} + SRC ${name_sample}.cpp + ${FLAGS_FOR_SAMPLES} + ) + endforeach() +endfunction() + diff --git a/configure.sh b/configure.sh new file mode 100755 index 0000000..214b717 --- /dev/null +++ b/configure.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +mkdir -p ../build/Effective-Modern-Cpp/gcc-4.9 +cd ../build/Effective-Modern-Cpp/gcc-4.9 + +# url: http://evadeflow.com/2013/11/static-analysis-with-clangscan-build/ +cmake ../../../Effective-Modern-Cpp/ + +cd - \ No newline at end of file