├── C++ 20语言特性.md ├── C++ 入门教程(41课时) - 阿里云大学.md ├── C++11常用新特性(一).md ├── C++11常用新特性(二).md ├── C++14新特性的所有知识点全在这儿啦!.md ├── C++基础入门.md ├── C++学习笔记.md ├── C++核心编程.md ├── README.md ├── cpp_11 ├── 001_meaning_keywords_README.md ├── 001_new_keywords_README.md ├── 002_grammar_POD.cpp ├── 002_grammar_SFINAE.cpp ├── 002_grammar_Uniform_initialization_syntax_and_semantics.cpp ├── 002_grammar_align.cpp ├── 002_grammar_alignas.cpp ├── 002_grammar_aligned_storage.cpp ├── 002_grammar_alignment_of.cpp ├── 002_grammar_alignof.cpp ├── 002_grammar_c99.cpp ├── 002_grammar_constexpr.cpp ├── 002_grammar_cpluscplus.h ├── 002_grammar_cpp_11_pragma.cpp ├── 002_grammar_explicit.cpp ├── 002_grammar_extended_friend_syntax.cpp ├── 002_grammar_extended_integer_types.cpp ├── 002_grammar_for_loop.cpp ├── 002_grammar_initializer_lists01.cpp ├── 002_grammar_initializer_lists02.cpp ├── 002_grammar_lamda.cpp ├── 002_grammar_lnline.cpp ├── 002_grammar_long_long.cpp ├── 002_grammar_max_align_t.cpp ├── 002_grammar_move_semantics.cpp ├── 002_grammar_nullptr.cpp ├── 002_grammar_numeric_limits.cpp ├── 002_grammar_pragma.cpp ├── 002_grammar_preventing_narrowing.cpp ├── 002_grammar_random_device.cpp ├── 002_grammar_raw_string.cpp ├── 002_grammar_rvalue_reference.cpp ├── 002_grammar_scoped_and_strongly_typed_enums.cpp ├── 002_grammar_static_assert.cpp ├── 002_grammar_stdref_stdcref.cpp ├── 002_grammar_trailing_return_type_syntax.cpp ├── 002_grammar_unrestricted_union.cpp ├── 002_grammar_user_defined_literals.cpp ├── 003_class_Inheritance_constructor.cpp ├── 003_class_delegating_constructors.cpp ├── 003_class_functor.cpp ├── 003_class_inheritance_constructor.cpp ├── 003_class_member_init.cpp ├── 003_class_move_assignment_operator.cpp ├── 003_class_move_constructor.cpp ├── 003_class_type_alias.cpp ├── 003_class_type_property_init.cpp ├── 003_error_std_errc.cpp ├── 003_error_std_error_category.cpp ├── 003_error_std_error_code.cpp ├── 003_error_std_error_condition.cpp ├── 003_error_std_generic_category.cpp ├── 003_error_std_system_error.cpp ├── 003_exception_std_current_exception.cpp ├── 003_exception_std_exception_ptr.cpp ├── 003_exception_std_make_exception_ptr.cpp ├── 003_exception_std_nested_exception.cpp ├── 003_exception_std_rethrow_exception.cpp ├── 003_exception_std_rethrow_if_nested.cpp ├── 003_exception_std_throw_with_nested.cpp ├── 003_function_CV_restricted.cpp ├── 003_function_object_template_mem_fn.cpp ├── 003_function_object_template_std_bad_function_call.cpp ├── 003_function_object_template_std_bind.cpp ├── 003_function_object_template_std_function.cpp ├── 003_function_references_qualifiers.cpp ├── 003_regex_basic_regex.cpp ├── 003_regex_basic_sub_match.cpp ├── 003_regex_match_results.cpp ├── 003_rtti_alignment_of.cpp ├── 003_rtti_extent.cpp ├── 003_rtti_rank.cpp ├── 003_rtti_std_has_unique_object_representations.cpp ├── 003_rtti_std_has_virtual_destructor.cpp ├── 003_rtti_std_is_abstract.cpp ├── 003_rtti_std_is_aggregate.cpp ├── 003_rtti_std_is_arithmetic.cpp ├── 003_rtti_std_is_array.cpp ├── 003_rtti_std_is_assignable.cpp ├── 003_rtti_std_is_base_of.cpp ├── 003_rtti_std_is_bounded_array.cpp ├── 003_rtti_std_is_class.cpp ├── 003_rtti_std_is_compound.cpp ├── 003_rtti_std_is_const.cpp ├── 003_rtti_std_is_constructible.cpp ├── 003_rtti_std_is_convertible.cpp ├── 003_rtti_std_is_copy_assignable.cpp ├── 003_rtti_std_is_copy_constructible.cpp ├── 003_rtti_std_is_default_constructible.cpp ├── 003_rtti_std_is_destructible.cpp ├── 003_rtti_std_is_empty.cpp ├── 003_rtti_std_is_enum.cpp ├── 003_rtti_std_is_final.cpp ├── 003_rtti_std_is_floating_point.cpp ├── 003_rtti_std_is_function.cpp ├── 003_rtti_std_is_fundamental.cpp ├── 003_rtti_std_is_integral.cpp ├── 003_rtti_std_is_lvalue_reference.cpp ├── 003_rtti_std_is_member_function_pointer.cpp ├── 003_rtti_std_is_member_object_pointer.cpp ├── 003_rtti_std_is_member_pointer.cpp ├── 003_rtti_std_is_move_assignable.cpp ├── 003_rtti_std_is_move_constructible.cpp ├── 003_rtti_std_is_nothrow_assignable.cpp ├── 003_rtti_std_is_nothrow_constructible.cpp ├── 003_rtti_std_is_nothrow_copy_assignable.cpp ├── 003_rtti_std_is_nothrow_copy_constructiblee.cpp ├── 003_rtti_std_is_nothrow_default_constructible.cpp ├── 003_rtti_std_is_nothrow_destructible.cpp ├── 003_rtti_std_is_nothrow_move_assignable.cpp ├── 003_rtti_std_is_nothrow_move_constructible.cpp ├── 003_rtti_std_is_nothrow_swappable.cpp ├── 003_rtti_std_is_nothrow_swappable_with.cpp ├── 003_rtti_std_is_null_pointer.cpp ├── 003_rtti_std_is_object.cpp ├── 003_rtti_std_is_pod.cpp ├── 003_rtti_std_is_pointer.cpp ├── 003_rtti_std_is_polymorphic.cpp ├── 003_rtti_std_is_reference.cpp ├── 003_rtti_std_is_rvalue_reference.cpp ├── 003_rtti_std_is_same.cpp ├── 003_rtti_std_is_scalar.cpp ├── 003_rtti_std_is_signed.cpp ├── 003_rtti_std_is_standard_layout.cpp ├── 003_rtti_std_is_swappable.cpp ├── 003_rtti_std_is_swappable_with.cpp ├── 003_rtti_std_is_trivial.cpp ├── 003_rtti_std_is_trivially_assignable.cpp ├── 003_rtti_std_is_trivially_constructible.cpp ├── 003_rtti_std_is_trivially_copy_assignable.cpp ├── 003_rtti_std_is_trivially_copy_constructible.cp ├── 003_rtti_std_is_trivially_copyable.cpp ├── 003_rtti_std_is_trivially_default_constructible.cpp ├── 003_rtti_std_is_trivially_destructible.cpp ├── 003_rtti_std_is_trivially_move_assignable.cpp ├── 003_rtti_std_is_trivially_move_constructible.cpp ├── 003_rtti_std_is_unbounded_array.cpp ├── 003_rtti_std_is_union.cpp ├── 003_rtti_std_is_unsigned.cpp ├── 003_rtti_std_is_void.cpp ├── 003_rtti_std_is_volatile.cpp ├── 003_std_atomic_bool.cpp ├── 003_std_atomic_char.cpp ├── 003_std_atomic_char16_t.cpp ├── 003_std_atomic_char32_t.cpp ├── 003_std_atomic_char8_t.cpp ├── 003_std_atomic_int.cpp ├── 003_std_atomic_long.cpp ├── 003_std_atomic_long_long.cpp ├── 003_std_atomic_short.cpp ├── 003_std_atomic_signed_char.cpp ├── 003_std_atomic_std_int16_t.cpp ├── 003_std_atomic_std_int32_t.cpp ├── 003_std_atomic_std_int64_t.cpp ├── 003_std_atomic_std_int8_t.cpp ├── 003_std_atomic_std_int_fast16_t.cpp ├── 003_std_atomic_std_int_fast32_t.cpp ├── 003_std_atomic_std_int_fast64_t.cpp ├── 003_std_atomic_std_int_fast8_t.cpp ├── 003_std_atomic_std_int_least16_t.cpp ├── 003_std_atomic_std_int_least32_t.cpp ├── 003_std_atomic_std_int_least64_t.cpp ├── 003_std_atomic_std_int_least8_t.cpp ├── 003_std_atomic_std_intmax_t.cpp ├── 003_std_atomic_std_intptr_t.cpp ├── 003_std_atomic_std_ptrdiff_t.cpp ├── 003_std_atomic_std_size_t.cpp ├── 003_std_atomic_std_uint16_t.cpp ├── 003_std_atomic_std_uint32_t.cpp ├── 003_std_atomic_std_uint64_t.cpp ├── 003_std_atomic_std_uint8_t.cpp ├── 003_std_atomic_std_uint_fast16_t.cpp ├── 003_std_atomic_std_uint_fast32_t.cpp ├── 003_std_atomic_std_uint_fast64_t.cpp ├── 003_std_atomic_std_uint_fast8_t.cpp ├── 003_std_atomic_std_uint_least16_t.cpp ├── 003_std_atomic_std_uint_least32_t.cpp ├── 003_std_atomic_std_uint_least64_t.cpp ├── 003_std_atomic_std_uint_least8_t.cpp ├── 003_std_atomic_std_uintmax_t.cpp ├── 003_std_atomic_std_uintptr_t.cpp ├── 003_std_atomic_unsigned_char.cpp ├── 003_std_atomic_unsigned_int.cpp ├── 003_std_atomic_unsigned_long.cpp ├── 003_std_atomic_unsigned_long_long.cpp ├── 003_std_atomic_unsigned_short.cpp ├── 003_std_atomic_wchar_t.cpp ├── 003_std_thread_async.cpp ├── 003_std_thread_std_atomic.cpp ├── 003_std_thread_std_call_once.cpp ├── 003_std_thread_std_cond_ition_variable.cpp ├── 003_std_thread_std_future.cpp ├── 003_std_thread_std_lock.cpp ├── 003_std_thread_std_mutex.cpp ├── 003_std_thread_std_thread.cpp ├── 003_std_thread_std_thread_local.cpp ├── 003_std_thread_volatile.cpp ├── 003_stl_auto_ptr.cpp ├── 003_stl_container_init.cpp ├── 003_stl_emplace.cpp ├── 003_stl_std_array.cpp ├── 003_stl_std_atomic_bool.cpp ├── 003_stl_std_atomic_char.cpp ├── 003_stl_std_atomic_char16_t.cpp ├── 003_stl_std_atomic_char32_t.cpp ├── 003_stl_std_atomic_char8_t.cpp ├── 003_stl_std_atomic_int.cpp ├── 003_stl_std_atomic_long.cpp ├── 003_stl_std_atomic_long_long.cpp ├── 003_stl_std_atomic_short.cpp ├── 003_stl_std_atomic_signed_char.cpp ├── 003_stl_std_atomic_std_int16_t.cpp ├── 003_stl_std_atomic_std_int32_t.cpp ├── 003_stl_std_atomic_std_int64_t.cpp ├── 003_stl_std_atomic_std_int8_t.cpp ├── 003_stl_std_atomic_std_int_fast16_t.cpp ├── 003_stl_std_atomic_std_int_fast32_t.cpp ├── 003_stl_std_atomic_std_int_fast64_t.cpp ├── 003_stl_std_atomic_std_int_fast8_t.cpp ├── 003_stl_std_atomic_std_int_least16_t.cpp ├── 003_stl_std_atomic_std_int_least32_t.cpp ├── 003_stl_std_atomic_std_int_least64_t.cpp ├── 003_stl_std_atomic_std_int_least8_t.cpp ├── 003_stl_std_atomic_std_intmax_t.cpp ├── 003_stl_std_atomic_std_intptr_t.cpp ├── 003_stl_std_atomic_std_ptrdiff_t.cpp ├── 003_stl_std_atomic_std_size_t.cpp ├── 003_stl_std_atomic_std_uint16_t.cpp ├── 003_stl_std_atomic_std_uint32_t.cpp ├── 003_stl_std_atomic_std_uint64_t.cpp ├── 003_stl_std_atomic_std_uint8_t.cpp ├── 003_stl_std_atomic_std_uint_fast16_t.cpp ├── 003_stl_std_atomic_std_uint_fast32_t.cpp ├── 003_stl_std_atomic_std_uint_fast64_t.cpp ├── 003_stl_std_atomic_std_uint_fast8_t.cpp ├── 003_stl_std_atomic_std_uint_least16_t.cpp ├── 003_stl_std_atomic_std_uint_least32_t.cpp ├── 003_stl_std_atomic_std_uint_least64_t.cpp ├── 003_stl_std_atomic_std_uint_least8_t.cpp ├── 003_stl_std_atomic_std_uintmax_t.cpp ├── 003_stl_std_atomic_std_uintptr_t.cpp ├── 003_stl_std_atomic_unsigned_char.cpp ├── 003_stl_std_atomic_unsigned_int.cpp ├── 003_stl_std_atomic_unsigned_long.cpp ├── 003_stl_std_atomic_unsigned_long_long.cpp ├── 003_stl_std_atomic_unsigned_short.cpp ├── 003_stl_std_atomic_wchar_t.cpp ├── 003_stl_std_begin.cpp ├── 003_stl_std_end.cpp ├── 003_stl_std_forward_list.cpp ├── 003_stl_std_get.cpp ├── 003_stl_std_hash_std_bitset.cpp ├── 003_stl_std_hash_std_error_code.cpp ├── 003_stl_std_hash_std_string.cpp ├── 003_stl_std_hash_std_type_index.cpp ├── 003_stl_std_hash_std_u16string.cpp ├── 003_stl_std_hash_std_u32string.cpp ├── 003_stl_std_hash_std_vector_bool.cpp ├── 003_stl_std_hash_std_wstring.cpp ├── 003_stl_std_make_tuple.cpp ├── 003_stl_std_move.cpp ├── 003_stl_std_shared_ptr.cpp ├── 003_stl_std_tie.cpp ├── 003_stl_std_unique_ptr.cpp ├── 003_stl_std_unordered_map.cpp ├── 003_stl_std_unordered_multimap.cpp ├── 003_stl_std_unordered_multiset.cpp ├── 003_stl_std_unordered_set.cpp ├── 003_stl_std_weak_ptr.cpp ├── 003_template_alias.cpp ├── 003_template_aliases.cpp ├── 003_template_angle_bracket.cpp ├── 003_template_default_template_parameters.cpp ├── 003_template_external.cpp ├── 003_template_external_template.cpp ├── 003_template_variable_parameter_template.cpp ├── 003_thread_std_async.cpp ├── 003_thread_std_atomic.cpp ├── 003_thread_std_call_once.cpp ├── 003_thread_std_condition_variable.cpp ├── 003_thread_std_future.cpp ├── 003_thread_std_lock.cpp ├── 003_thread_std_mutex.cpp ├── 003_thread_std_thread.cpp ├── 003_thread_std_thread_local.cpp ├── 003_thread_std_volatile.cpp └── README.md ├── cpp_14 ├── 001_initializer_list_crbegin.cpp ├── 001_initializer_list_crend.cpp ├── 001_initializer_list_rbegin.cpp ├── 001_initializer_list_rend.cpp ├── 001_iterator_make_reverse_iterator.cpp ├── 001_stl_begin.cpp ├── 001_stl_cbegin.cpp ├── 001_stl_cend.cpp ├── 001_stl_crbegin.cpp ├── 001_stl_crend.cpp ├── 001_stl_end.cpp ├── 001_stl_rbegin.cpp ├── 001_stl_rend.cpp ├── 001_type_traits_is_final.cpp ├── 001_type_traits_is_null_pointer.cpp ├── 001_utility_exchange.cpp ├── 001_utility_integer_sequence.cpp ├── 002_type_traits_bool_constant.cpp ├── 002_type_traits_byte.cpp ├── 002_type_traits_conjunction.cpp ├── 002_type_traits_disjunction.cpp ├── 002_type_traits_invoke_result.cpp ├── 002_type_traits_is_aggregate.cpp ├── 002_type_traits_is_invocable.cpp ├── 002_type_traits_is_invocable_r.cpp ├── 002_type_traits_is_nothrow_invocable.cpp ├── 002_type_traits_is_nothrow_invocable_r.cpp ├── 002_type_traits_is_nothrow_swappable.cpp ├── 002_type_traits_is_nothrow_swappable_with.cpp ├── 002_type_traits_is_swappable.cpp ├── 002_type_traits_is_swappable_with.cpp ├── 002_type_traits_ndisjunctionegation.cpp ├── 002_type_traits_void_t.cpp ├── 003_utility_as_const.cpp ├── 003_utility_in_place.cpp ├── 003_utility_in_place_index.cpp ├── 003_utility_in_place_index_t.cpp ├── 003_utility_in_place_t.cpp ├── 003_utility_in_place_type.cpp ├── 003_utility_in_place_type_t.cpp ├── 004_tuple_apply.cpp ├── 004_tuple_make_from_tuple.cpp ├── 005_optional_bad_optional_access.cpp ├── 005_optional_in_place.cpp ├── 005_optional_in_place_index.cpp ├── 005_optional_in_place_index_t.cpp ├── 005_optional_in_place_t.cpp1 ├── 005_optional_in_place_type.cpp ├── 005_optional_in_place_type_t.cpp ├── 005_optional_make_optional.cpp ├── 005_optional_nullopt.cpp ├── 005_optional_nullopt_t.cpp ├── 005_optional_optional.cpp ├── 005_optional_std_hash.cpp ├── 005_optional_std_swap.cpp ├── 006_variant_get_if.cpp ├── 006_variant_holds_alternative.cpp ├── 006_variant_std_get.cpp ├── 006_variant_std_swap.cpp ├── 006_variant_variant.cpp ├── 006_variant_visit.cpp └── README.md ├── cpp_17 ├── 001_keywords_README.md ├── 002_type_traits_byte.cpp ├── 002_type_traits_conjunction.cpp ├── 002_type_traits_disjunction.cpp ├── 002_type_traits_integral_constant.cpp ├── 002_type_traits_invoke_result.cpp ├── 002_type_traits_is_aggregate.cpp ├── 002_type_traits_is_invocable.cpp ├── 002_type_traits_is_invocable_r.cpp ├── 002_type_traits_is_nothrow_invocable.cpp ├── 002_type_traits_is_nothrow_invocable_r.cpp ├── 002_type_traits_is_nothrow_swappable.cpp ├── 002_type_traits_is_nothrow_swappable_with.cpp ├── 002_type_traits_is_swappable.cpp ├── 002_type_traits_is_swappable_with.cpp ├── 002_type_traits_ndisjunctionegation.cpp ├── 002_type_traits_void_t.cpp ├── 003_utility_as_const.cpp ├── 003_utility_in_place.cpp ├── 003_utility_in_place_index.cpp ├── 003_utility_in_place_index_t.cpp ├── 003_utility_in_place_t.cpp ├── 003_utility_in_place_type.cpp ├── 003_utility_in_place_type_t.cpp ├── 004_tuple_apply.cpp ├── 004_tuple_make_from_tuple.cpp ├── 005_optional_bad_optional_access.cpp ├── 005_optional_in_place.cpp ├── 005_optional_in_place_index.cpp ├── 005_optional_in_place_index_t.cpp ├── 005_optional_in_place_t.cpp ├── 005_optional_in_place_type.cpp ├── 005_optional_in_place_type_t.cpp ├── 005_optional_make_optional.cpp ├── 005_optional_nullopt.cpp ├── 005_optional_nullopt_t.cpp ├── 005_optional_optional.cpp ├── 005_optional_std_hash.cpp ├── 005_optional_std_swap.cpp ├── 006_variant_bad_variant_access.cpp ├── 006_variant_get_if.cpp ├── 006_variant_holds_alternative.cpp ├── 006_variant_monostate.cpp ├── 006_variant_std_get.cpp ├── 006_variant_std_hash.cpp ├── 006_variant_std_swap.cpp ├── 006_variant_variant.cpp ├── 006_variant_variant_alternative.cpp ├── 006_variant_variant_alternative_t.cpp ├── 006_variant_variant_npos.cpp ├── 006_variant_variant_size.cpp ├── 006_variant_variant_size_v.cpp ├── 006_variant_visit.cpp ├── 007_any_any.cpp ├── 007_any_any_cast.cpp ├── 007_any_bad_any_cast.cpp ├── 007_any_make_any.cpp ├── 007_any_std_swap.cpp ├── 008_charconv_chars_format.cpp ├── 008_charconv_from_chars.cpp ├── 008_charconv_to_chars.cpp ├── 009_initializer_list_data.cpp ├── 009_initializer_list_empty.cpp ├── 010_map_extract.cpp ├── 010_map_insert_or_assign.cpp ├── 010_map_merge.cpp ├── 010_map_try_emplace.cpp ├── 011_unordered_map_extract.cpp ├── 011_unordered_map_insert_or_assign.cpp ├── 011_unordered_map_merge.cpp ├── 011_unordered_map_try_emplace.cpp ├── 012_stl_data.cpp ├── 012_stl_empty.cpp ├── 012_stl_size.cpp └── README.md ├── cpp_20 ├── 001_keywords_README.md ├── 002_rtti_basic_common_reference.cpp ├── 002_rtti_common_reference.cpp ├── 002_rtti_is_bounded_array.cpp ├── 002_rtti_is_constant_evaluated.cpp ├── 002_rtti_is_corresponding_member.cpp ├── 002_rtti_is_layout_compatible.cpp ├── 002_rtti_is_nothrow_convertible.cpp ├── 002_rtti_is_pointer_interconvertible_base_of.cpp ├── 002_rtti_is_pointer_interconvertible_with_class.cpp ├── 002_rtti_is_unbounded_array.cpp ├── 002_rtti_remove_cvref.cpp ├── 002_rtti_type_identity.cpp ├── 003_rtti_coroutine_handle.cpp ├── 003_rtti_coroutine_traits.cpp ├── 003_rtti_noop_coroutine.cpp ├── 003_rtti_noop_coroutine_handle.cpp ├── 003_rtti_noop_coroutine_promise.cpp ├── 003_rtti_suspend_always.cpp ├── 003_rtti_suspend_never.cpp ├── 004_rtti_compare_common_comparison_category.cpp ├── 004_rtti_compare_compare_partial_order_fallback.cpp ├── 004_rtti_compare_compare_strong_order_fallback.cpp ├── 004_rtti_compare_compare_three_way.cpp ├── 004_rtti_compare_compare_three_way_result.cpp ├── 004_rtti_compare_compare_weak_order_fallback.cpp ├── 004_rtti_compare_is_eq.cpp ├── 004_rtti_compare_is_gt.cpp ├── 004_rtti_compare_is_gteq.cpp ├── 004_rtti_compare_is_lt.cpp ├── 004_rtti_compare_is_lteq.cpp ├── 004_rtti_compare_is_neq.cpp ├── 004_rtti_compare_partial_order.cpp ├── 004_rtti_compare_partial_ordering.cpp ├── 004_rtti_compare_std_coroutine_handle.cpp ├── 004_rtti_compare_std_coroutine_traits.cpp ├── 004_rtti_compare_strong_order.cpp ├── 004_rtti_compare_strong_ordering.cpp ├── 004_rtti_compare_three_way_comparable.cpp ├── 004_rtti_compare_three_way_comparable_with.cpp ├── 004_rtti_compare_weak_order.cpp ├── 004_rtti_compare_weak_ordering.cpp ├── 005_rtti_concepts_ranges_swap.cpp ├── 006_utility_cmp_equal.cpp ├── 006_utility_cmp_greater_equal.cpp ├── 006_utility_cmp_less.cpp ├── 006_utility_cmp_less_equal.cpp ├── 006_utility_cmp_not_equal.cpp ├── 006_utility_in_range.cpp ├── 007_format_basic_format_arg.cpp ├── 007_format_basic_format_args.cpp ├── 007_format_basic_format_context.cpp ├── 007_format_basic_format_parse_context.cpp ├── 007_format_format.cpp ├── 007_format_format_args.cpp ├── 007_format_format_context.cpp ├── 007_format_format_error.cpp ├── 007_format_format_parse_context.cpp ├── 007_format_format_to.cpp ├── 007_format_format_to_n.cpp ├── 007_format_formatted_size.cpp ├── 007_format_formatter.cpp ├── 007_format_make_format_args.cpp ├── 007_format_make_wformat_args.cpp ├── 007_format_vformat.cpp ├── 007_format_vformat_to.cpp ├── 007_format_visit_format_arg.cpp ├── 007_format_wformat_args.cpp ├── 007_format_wformat_context.cpp ├── 007_format_wformat_parse_context.cpp ├── 008_memory_destroy.cpp ├── 008_memory_destroy_at.cpp ├── 008_memory_destroy_n.cpp ├── 008_memory_uninitialized_default_construct.cpp ├── 008_memory_uninitialized_default_construct_n.cpp ├── 008_memory_uninitialized_move.cpp ├── 008_memory_uninitialized_move_n.cpp ├── 008_memory_uninitialized_value_construct.cpp ├── 008_memory_uninitialized_value_construct_n.cpp ├── 009_memory_resource_get_default_resource.cpp ├── 009_memory_resource_memory_resource.cpp ├── 009_memory_resource_monotonic_buffer_resource.cpp ├── 009_memory_resource_new_delete_resource.cpp ├── 009_memory_resource_null_memory_resource.cpp ├── 009_memory_resource_polymorphic_allocator.cpp ├── 009_memory_resource_pool_options.cpp ├── 009_memory_resource_set_default_resource.cpp ├── 009_memory_resource_synchronized_pool_resource.cpp ├── 009_memory_resource_unsynchronized_pool_resource.cpp ├── 010_concepts_assignable_from.cpp ├── 010_concepts_boolean_testable.cpp ├── 010_concepts_common_reference_with.cpp ├── 010_concepts_common_with.cpp ├── 010_concepts_constructible_from.cpp ├── 010_concepts_convertible_to.cpp ├── 010_concepts_copy_constructible.cpp ├── 010_concepts_copyable.cpp ├── 010_concepts_default_initializable.cpp ├── 010_concepts_derived_from.cpp ├── 010_concepts_destructible.cpp ├── 010_concepts_equality_comparable.cpp ├── 010_concepts_equality_comparable_with.cpp ├── 010_concepts_equivalence_relation.cpp ├── 010_concepts_floating_point.cpp ├── 010_concepts_integral.cpp ├── 010_concepts_invocable.cpp ├── 010_concepts_movable.cpp ├── 010_concepts_move_constructible.cpp ├── 010_concepts_predicate.cpp ├── 010_concepts_regular.cpp ├── 010_concepts_regular_invocable.cpp ├── 010_concepts_relation.cpp ├── 010_concepts_same_as.cpp ├── 010_concepts_semiregular.cpp ├── 010_concepts_signed_integral.cpp ├── 010_concepts_strict_weak_order.cpp ├── 010_concepts_swappable.cpp ├── 010_concepts_swappable_with.cpp ├── 010_concepts_totally_ordered.cpp ├── 010_concepts_totally_ordered_with.cpp ├── 010_concepts_unsigned_integral.cpp ├── 011_memory_assume_aligned.cpp ├── 011_memory_construct_at.cpp ├── 011_memory_make_obj_using_allocator.cpp ├── 011_memory_no-throw-forward-iterator.cpp ├── 011_memory_no-throw-forward-range.cpp ├── 011_memory_no-throw-input-iterator.cpp ├── 011_memory_no-throw-input-range.cpp ├── 011_memory_no-throw-sentinel-for.cpp ├── 011_memory_ranges_construct_at.cpp ├── 011_memory_ranges_destroy.cpp ├── 011_memory_ranges_destroy_at.cpp ├── 011_memory_ranges_destroy_n.cpp ├── 011_memory_ranges_uninitialized_copy.cpp ├── 011_memory_ranges_uninitialized_copy_n.cpp ├── 011_memory_ranges_uninitialized_default_construct.cpp ├── 011_memory_ranges_uninitialized_default_construct_n.cpp ├── 011_memory_ranges_uninitialized_fill.cpp ├── 011_memory_ranges_uninitialized_fill_n.cpp ├── 011_memory_ranges_uninitialized_move.cpp ├── 011_memory_ranges_uninitialized_move_n.cpp ├── 011_memory_ranges_uninitialized_value_construct.cpp ├── 011_memory_ranges_uninitialized_value_construct_n.cpp ├── 011_memory_to_address.cpp ├── 011_memory_uninitialized_construct_using_allocator.cpp ├── 011_memory_uses_allocator_construction_args.cpp ├── 012_chrono_ambiguous_local_time.cpp ├── 012_chrono_choose.cpp ├── 012_chrono_clock_cast.cpp ├── 012_chrono_clock_time_conversion.cpp ├── 012_chrono_current_zone.cpp ├── 012_chrono_day.cpp ├── 012_chrono_file_clock.cpp ├── 012_chrono_get_tzdb.cpp ├── 012_chrono_get_tzdb_list.cpp ├── 012_chrono_gps_clock.cpp ├── 012_chrono_is_am.cpp ├── 012_chrono_is_clock.cpp ├── 012_chrono_is_clock_v.cpp ├── 012_chrono_is_pm.cpp ├── 012_chrono_last_spec.cpp ├── 012_chrono_leap_second.cpp ├── 012_chrono_local_info.cpp ├── 012_chrono_local_t.cpp ├── 012_chrono_locate_zone.cpp ├── 012_chrono_make12.cpp ├── 012_chrono_make24.cpp ├── 012_chrono_month.cpp ├── 012_chrono_month_day.cpp ├── 012_chrono_month_day_last.cpp ├── 012_chrono_month_weekday.cpp ├── 012_chrono_month_weekday_last.cpp ├── 012_chrono_nonexistent_local_time.cpp ├── 012_chrono_operator.cpp ├── 012_chrono_parse.cpp ├── 012_chrono_reload_tzdb.cpp ├── 012_chrono_remote_version.cpp ├── 012_chrono_sys_info.cpp ├── 012_chrono_tai_clock.cpp ├── 012_chrono_time_of_day.cpp ├── 012_chrono_time_zone.cpp ├── 012_chrono_time_zone_link.cpp ├── 012_chrono_tzdb.cpp ├── 012_chrono_tzdb_list.cpp ├── 012_chrono_utc_clock.cpp ├── 012_chrono_weekday.cpp ├── 012_chrono_weekday_indexed.cpp ├── 012_chrono_weekday_last.cpp ├── 012_chrono_year.cpp ├── 012_chrono_year_month.cpp ├── 012_chrono_year_month_day.cpp ├── 012_chrono_year_month_day_last.cpp ├── 012_chrono_year_month_weekday.cpp ├── 012_chrono_year_month_weekday_last.cpp ├── 012_chrono_zoned_time.cpp ├── 012_chrono_zoned_traits.cpp ├── 013_string_ends_with.cpp ├── 013_string_starts_with.cpp ├── 014_string_view_ends_with.cpp ├── 014_string_view_starts_with.cpp ├── 015_cuchar_c8rtomb.cpp ├── 015_cuchar_mbrtoc8.cpp ├── 016_array_to_array.cpp ├── 017_vector_erase.cpp ├── 017_vector_erase_if.cpp ├── 018_map_contains.cpp ├── 018_map_erase_if.cpp ├── 019_unordered_map_contains.cpp ├── 019_unordered_map_erase_if.cpp ├── 020_span_back.cpp ├── 020_span_begin.cpp ├── 020_span_dynamic_extent.cpp ├── 020_span_end.cpp ├── 020_span_front.cpp ├── 020_span_rbegin.cpp ├── 020_span_rend.cpp ├── 021_std_bidirectional_iterator.cpp ├── 021_std_bidirectional_iterator_tag.cpp ├── 021_std_contiguous_iterator.cpp ├── 021_std_contiguous_iterator_tag.cpp ├── 021_std_forward_iterator.cpp ├── 021_std_forward_iterator_tag.cpp ├── 021_std_incrementable.cpp ├── 021_std_indirectly_readable.cpp ├── 021_std_indirectly_readable_traits.cpp ├── 021_std_indirectly_writable.cpp ├── 021_std_input_iterator.cpp ├── 021_std_input_iterator_tag.cpp ├── 021_std_input_or_output_iterator.cpp ├── 021_std_iter_common_reference_t.cpp ├── 021_std_iter_difference_t.cpp ├── 021_std_iter_reference_t.cpp ├── 021_std_iter_rvalue_reference_t.cpp ├── 021_std_iter_value_t.cpp ├── 021_std_iterator_traits.cpp ├── 021_std_output_iterator.cpp ├── 021_std_output_iterator_tag.cpp ├── 021_std_random_access_iterator.cpp ├── 021_std_random_access_iterator_tag.cpp ├── 021_std_sentinel_for.cpp ├── 021_std_sized_sentinel_for.cpp ├── 021_std_weakly_incrementable.cpp ├── 022_ranges_iter_move.cpp ├── 022_ranges_iter_swap.cpp ├── 023_std_bidirectional_iterator.cpp ├── 023_std_bidirectional_iterator_tag.cpp ├── 023_std_contiguous_iterator.cpp ├── 023_std_contiguous_iterator_tag.cpp ├── 023_std_forward_iterator.cpp ├── 023_std_forward_iterator_tag.cpp ├── 023_std_incrementable.cpp ├── 023_std_incrementable_traits.cpp ├── 023_std_indirectly_readable.cpp ├── 023_std_indirectly_readable_traits.cpp ├── 023_std_indirectly_writable.cpp ├── 023_std_input_iterator.cpp ├── 023_std_input_iterator_tag.cpp ├── 023_std_input_or_output_iterator.cpp ├── 023_std_iter_common_reference_t.cpp ├── 023_std_iter_difference_t.cpp ├── 023_std_iter_reference_t.cpp ├── 023_std_iter_rvalue_reference_t.cpp ├── 023_std_iter_value_t.cpp ├── 023_std_iterator_traits.cpp ├── 023_std_output_iterator.cpp ├── 023_std_output_iterator_tag.cpp ├── 023_std_random_access_iterator.cpp ├── 023_std_random_access_iterator_tag.cpp ├── 023_std_sentinel_for.cpp ├── 023_std_sized_sentinel_for.cpp ├── 023_std_weakly_incrementable.cpp ├── 024_iterator_common_iterator.cpp ├── 024_iterator_counted_iterator.cpp ├── 024_iterator_default_sentinel_t.cpp ├── 024_iterator_indirect_binary_predicate.cpp ├── 024_iterator_indirect_equivalence_relation.cpp ├── 024_iterator_indirect_result_t.cpp ├── 024_iterator_indirect_strict_weak_order.cpp ├── 024_iterator_indirect_unary_predicate.cpp ├── 024_iterator_indirectly_comparable.cpp ├── 024_iterator_indirectly_copyable.cpp ├── 024_iterator_indirectly_copyable_storable.cpp ├── 024_iterator_indirectly_movable.cpp ├── 024_iterator_indirectly_movable_storable.cpp ├── 024_iterator_indirectly_regular_unary_invocable.cpp ├── 024_iterator_indirectly_swappable.cpp ├── 024_iterator_indirectly_unary_invocable.cpp ├── 024_iterator_mergeable.cpp ├── 024_iterator_move_sentinel.cpp ├── 024_iterator_permutable.cpp ├── 024_iterator_projected.cpp ├── 024_iterator_sortable.cpp ├── 024_iterator_unreachable_sentinel_t.cpp ├── 025_iterator_ranges_advanc.cpp ├── 025_iterator_ranges_distance.cpp ├── 025_iterator_ranges_next.cpp ├── 025_iterator_ranges_prev.cpp ├── 026_std_ssize.cpp ├── 027_ranges_ranges_begin.cpp ├── 027_ranges_ranges_cbegin.cpp ├── 027_ranges_ranges_cdata.cpp ├── 027_ranges_ranges_cend.cpp ├── 027_ranges_ranges_crbegin.cpp ├── 027_ranges_ranges_crend.cpp ├── 027_ranges_ranges_data.cpp ├── 027_ranges_ranges_empty.cpp ├── 027_ranges_ranges_end.cpp ├── 027_ranges_ranges_rbegin.cpp ├── 027_ranges_ranges_rend.cpp ├── 027_ranges_ranges_size.cpp ├── 027_ranges_ranges_ssize.cpp └── README.md ├── cpp_23 ├── 001_rtti_is_scoped_enum.cpp ├── 002_utility_to_underlying.cpp ├── 003_stacktrace_basic_stacktrace.cpp ├── 003_stacktrace_stacktrace_entry.cpp ├── 004_memory_allocate_at_least.cpp ├── 004_memory_allocation_result.cpp ├── 004_memory_inout_ptr.cpp ├── 004_memory_inout_ptr_t.cpp ├── 004_memory_out_ptr.cpp ├── 004_memory_out_ptr_t.cpp ├── 005_string_contains.cpp ├── 005_string_view_contains.cpp └── README.md ├── 「Notes」C++14新特性浅谈.md ├── 吐血整理:C++11新特性.md ├── 在c++项目中你必须真正使用的15个c++特性.md ├── 如何在 C++11 中使用 Lambda 表达式.md ├── 学习笔记:C++ 11新特性.md ├── 总结归纳:C++17新特性.md ├── 每个c++开发人员都应该使用的10个c++ 11特性.md └── 深入理解C++11.md /cpp_11/002_grammar_alignas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | 6 | alignas(double) unsigned char c[1024]; //字符数组,但是却以double数据的形式对齐数据 7 | alginas(16) char d[100]; //以16字节对齐 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_alignment_of.cpp: -------------------------------------------------------------------------------- 1 | // alignment_of example 2 | #include 3 | #include 4 | 5 | int main() { 6 | std::cout << "alignment_of:" << std::endl; 7 | std::cout << "char: " << std::alignment_of::value << std::endl; 8 | std::cout << "int: " << std::alignment_of::value << std::endl; 9 | std::cout << "int[20]: " << std::alignment_of::value << std::endl; 10 | std::cout << "long long int: " << std::alignment_of::value << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_c99.cpp: -------------------------------------------------------------------------------- 1 | //C++对以下C99特性的支持纳入了新标准之中: 2 | //1、C99中的预定义宏 3 | //2、__func__预定义标识符 4 | //3、_Pragma操作符 5 | //4、不定参数宏定义以及__VA_ARGS__ 6 | //5、宽窄字符串连接 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | cout << "Standerd Clib" << __STDC_HOSTED__ << endl; 14 | cout << "Standerd C" << __STDC__ << endl; 15 | //cout << "C Standerd version " << __STDC_VERSION__ << endl; 16 | //cout << "ISO/IEC" << __STDC_ISO_10646__ << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_cpluscplus.h: -------------------------------------------------------------------------------- 1 | //_cplusplus宏经常出现在C与C++混合编写的代码中, 一般放在头文件中,比如 2 | 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | //some code ... 8 | 9 | #ifdef __cplusplus 10 | } 11 | #endif 12 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_cpp_11_pragma.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_explicit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ConvertTo 5 | { 6 | }; 7 | 8 | class Convertable 9 | { 10 | public: 11 | explicit operator ConvertTo () const 12 | { 13 | cout << "callCount : " << ++(m_nCallCnt) << endl; 14 | return ConvertTo(); 15 | } 16 | 17 | static int m_nCallCnt; 18 | }; 19 | 20 | int Convertable::m_nCallCnt = 0; 21 | 22 | void Func(ConvertTo ct) { } 23 | 24 | int main() 25 | { 26 | Convertable c; 27 | ConvertTo ct(c); // 直接初始化,通过 28 | // ConvertTo ct2 = c; // 拷贝构造初始化,编译失败 29 | ConvertTo ct3 = static_cast(c); // 强制转化,通过 30 | // Func(c); // 拷贝构造初始化,编译失败 31 | system("pause"); 32 | } 33 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_extended_friend_syntax.cpp: -------------------------------------------------------------------------------- 1 | class Poly; 2 | typedef Poly P; 3 | 4 | class LiLei 5 | { 6 | friend class Poly; // C++98通过, C++11通过 7 | }; 8 | 9 | class Jim 10 | { 11 | friend Poly; // C++98失败, C++11通过 12 | }; 13 | 14 | class HanMei 15 | { 16 | friend P; // C++98失败, C++11通过 17 | }; 18 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_extended_integer_types.cpp: -------------------------------------------------------------------------------- 1 | //对于有符号的,下面的类型是等价的: 2 | //long long、signed long long、long long int、signed long long int; 而unsigned long long 和 unsigned long long int 也是等价的。 3 | 4 | //与 long long 整型相关的一共有3个: 5 | //LONG_MIN、LONG_MAX 和ULONG_MAX, 它们分别代表了平台上最小的long long 值、最大的long long 值,以及最大的unsigned long long 值。 6 | 7 | int main() { 8 | long long int lli=-900000000000000LL; // 有符号的long long 变量lli 9 | unsigned long long int ulli=-900000000000ULL; // 无符号的 unsigned long long 变量ulli 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_initializer_lists01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | //初始化列表 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | vector iv = {1,2,3,4,5}; 13 | list li = {1,2,3,4,5}; 14 | map mis = {{1,"c"},{2,"c++"}, 15 | {3,"java"},{4,"scala"}, 16 | {5,"python"}}; 17 | mis.insert({6,"ruby"}); 18 | // map::iterator itr = mis.begin(); 19 | // for(; itr != mis.end(); ++itr) 20 | // { 21 | // cout<first<< itr->second< 2 | #include 3 | using namespace std; 4 | template 5 | class MyArray 6 | { 7 | private: 8 | vector m_Array; 9 | public: 10 | MyArray() { } 11 | MyArray(const initializer_list& il) 12 | { 13 | for (auto x : il) 14 | m_Array.push_back(x); 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | MyArray foo = { 3, 4, 6, 9 }; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_lnline.cpp: -------------------------------------------------------------------------------- 1 | namespace Parent 2 | { 3 | 4 | inline namespace Child1 5 | { 6 | struct child1_data{int a;} ; 7 | } 8 | inline namespace Child2 9 | { 10 | struct child2_data{int b;} ; 11 | } 12 | namespace child3 13 | { 14 | child1_data data1; 15 | child2_data data2; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_long_long.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 002_grammar_long_long.cpp.cpp 3 | */ 4 | #include 5 | #include 6 | 7 | int main(int argc, char** argv){ 8 | long long int lli=-900000000000000LL; // 有符号的long long 变量lli 9 | unsigned long long int ulli=-900000000000ULL; // 无符号的 unsigned long long 变量ulli。 10 | 11 | int64_t value = 0xFFFFFFFFFFFF; 12 | printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t)); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_max_align_t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | std::cout << alignof(std::max_align_t) << '\n'; 6 | } 7 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_pragma.cpp: -------------------------------------------------------------------------------- 1 | __pragma("once") 2 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_random_device.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | std::vector randomGenerate(const unsigned low, const unsigned high) 8 | { 9 | static std::default_random_engine e(time(0)); 10 | static std::uniform_int_distribution u(low, high); 11 | 12 | std::vector vec; 13 | for (int i = 0; i < 10; i++) 14 | vec.push_back(u(e)); 15 | return vec; 16 | } 17 | 18 | int main() 19 | { 20 | for (int i = 0; i < 10; i++) { 21 | std::vector vec = randomGenerate(0, 30); 22 | for (auto &i : vec) 23 | std::cout << i << " "; 24 | std::cout << std::endl; 25 | } 26 | 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_static_assert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template int bit_copy(T& a, U& b) 6 | { 7 | assert(sizeof(b) == sizeof(a)); 8 | //static_assert(sizeof(b) == sizeof(a), "template parameter size no equal!"); 9 | memcpy(&a, &b, sizeof(b)); 10 | }; 11 | 12 | int main() 13 | { 14 | int varA = 0x2468; 15 | double varB; 16 | bit_copy(varA, varB); 17 | getchar(); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_stdref_stdcref.cpp: -------------------------------------------------------------------------------- 1 | //std::ref 用于包装按引用传递的值。 2 | //std::cref 用于包装按const引用传递的值。 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | void foo( int &a) 11 | { 12 | cout<<"thread :"<< a++ < 4 | #include 5 | #include 6 | #include 7 | 8 | int func(int, int); 9 | auto func2(int, int)->int; // 指定返回类型 10 | 11 | template 12 | auto sum(const T1 &t1, const T2 &t2)->decltype(t1+t2) // 指定返回类型 13 | { 14 | return t1 + t2; 15 | } 16 | 17 | template 18 | auto mul(const T1 &t1, const T2 &t2)->decltype(t1*t2) // 指定返回类型 19 | { 20 | return t1 * t2; 21 | } 22 | 23 | void mytest() 24 | { 25 | auto a = 3; 26 | auto b = 4L; 27 | auto pi = 3.14f; 28 | 29 | auto c = mul(sum(a, b), pi); 30 | std::cout << c << std::endl; // 21.98 31 | 32 | return; 33 | } 34 | 35 | int main() 36 | { 37 | mytest(); 38 | 39 | system("pause"); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /cpp_11/002_grammar_unrestricted_union.cpp: -------------------------------------------------------------------------------- 1 | //非受限联合体:C++98中并不是所有数据类型都能够成为union的数据成员,不允许联合体拥有非POD(Plain Old Data)、静态或引用类型的成员。 2 | //C++11中取消了联合体对于数据成员的限制,任何非引用类型都可以成为联合体的数据成员,成为非受限联合体。 3 | 4 | struct Student 5 | { 6 | Student(bool g, int a): gender(g), age(a){} 7 | bool gender; 8 | int age; 9 | }; 10 | 11 | union T 12 | { 13 | Student s; //C++98下编译失败,不是一个POD类型 14 | int id; 15 | char name[10]; 16 | }; 17 | 18 | int main() 19 | { 20 | return 0; 21 | } 22 | 23 | //编译选项:g++ -std=c++98 union.cpp 24 | -------------------------------------------------------------------------------- /cpp_11/003_class_Inheritance_constructor.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_class_delegating_constructors.cpp: -------------------------------------------------------------------------------- 1 | class A 2 | { 3 | private: 4 | int a; 5 | int b; 6 | char c; 7 | char d; 8 | public: 9 | A(int num0,int num1,char C):a(num0),b(num1),c(C){} 10 | A(int num0,char C):A(num0,0,C){}//b默认初始化为0 11 | A(int num0):A(num0,'p'){b=1;}//b重新赋值为1 12 | void getMembers() 13 | { 14 | cout< 2 | using namespace std; 3 | 4 | struct A 5 | { 6 | A(int i = 10) : m_a(i) 7 | {} 8 | 9 | int m_a; 10 | }; 11 | 12 | struct B : A 13 | { 14 | using A::A; // 继承构造函数 15 | 16 | int m_b{ 100 }; 17 | }; 18 | 19 | int main() 20 | { 21 | B b; 22 | cout << b.m_a << endl; // 10 23 | cout << b.m_b << endl; // 100 24 | 25 | B bb(200); 26 | cout << bb.m_a << endl; // 200 27 | cout << bb.m_b << endl; // 100 28 | } 29 | -------------------------------------------------------------------------------- /cpp_11/003_class_member_init.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Mem 5 | { 6 | public: 7 | Mem(int i) : m(i) 8 | {} 9 | 10 | private: 11 | int m; 12 | }; 13 | 14 | class Group 15 | { 16 | public: 17 | Group() {} 18 | Group(int a) : data(a) {} 19 | Group(Mem m) : mem(m) {} 20 | Group(int a, Mem m, string n) : data(a), mem(m), name(n) 21 | {} 22 | 23 | private: 24 | int data = 1; 25 | Mem mem{0}; 26 | string name{" Group"}; 27 | }; 28 | -------------------------------------------------------------------------------- /cpp_11/003_class_move_assignment_operator.cpp: -------------------------------------------------------------------------------- 1 | class HasPtr { 2 | public: 3 | HasPtr (const std::string &s = std::string () ): 4 | ps (new std::string(s) ) , i(0) { } 5 | //对ps指向的string,每个HasPtr对象都有自己的拷贝 6 | HasPtr(const HasPtr &p): 7 | ps (new std::string (*p.ps)), i(p.i) { } 8 | HasPtr& operator=(const HasPtr &); 9 | ~HasPtr() { delete ps; } 10 | private: 11 | std::string *ps; 12 | int i; 13 | ); 14 | 15 | class HasPtr { 16 | public: 17 | 18 | //添加的移动构造函数 19 | HasPtr(HasPtr &&p) noexcept : ps(p.ps), i(p.i) {p.ps = 0 ; } 20 | //赋值运算符既是移动赋值运算符,也是拷贝赋值运算符 21 | HasPtr& operator= (HasPtr rhs) 22 | { swap (*this, rhs) ; return *this; } 23 | //其他成员的定义,同13.2.1节(第453页) 24 | }; 25 | -------------------------------------------------------------------------------- /cpp_11/003_class_type_alias.cpp: -------------------------------------------------------------------------------- 1 | //传统的定义类型别名的方法是使用关键字typedef: 2 | typedef double wages; 3 | 4 | //也可以使用#define来定义别名(使用预处理器) 5 | #define wages double 6 | 7 | //在C++11中使用关键字using来进行别名声明,假设我们现在定义了结构体MyStruct,那么我们可以使用using为Mystruct类型设置一个别名: 8 | using MS=MyStruct; 9 | 10 | //在定义该结构体类型变量时,就可以使用MS作为类型名了 11 | MS TempStruct; 12 | -------------------------------------------------------------------------------- /cpp_11/003_class_type_property_init.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_error_std_errc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | try { 8 | std::thread().detach(); // detaching a not-a-thread 9 | } catch (const std::system_error& e) { 10 | std::cout << "Caught a system_error\n"; 11 | if(e.code() == std::errc::invalid_argument) 12 | std::cout << "The error condition is std::errc::invalid_argument\n"; 13 | std::cout << "the error description is " << e.what() << '\n'; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_error_std_error_category.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::error_condition econd = std::system_category().default_error_condition(EDOM); 9 | std::cout << "Category: " << econd.category().name() << '\n' 10 | << "Value: " << econd.value() << '\n' 11 | << "Message: " << econd.message() << '\n'; 12 | 13 | econd = std::system_category().default_error_condition(10001); 14 | std::cout << "Category: " << econd.category().name() << '\n' 15 | << "Value: " << econd.value() << '\n' 16 | << "Message: " << econd.message() << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_error_std_error_code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | enum class YourErrorCode { 5 | kSuccess = 0, // 别忘了 0 应该表示无错误 6 | kNetworkError, 7 | kBadRequest, 8 | kServerError, 9 | }; 10 | 11 | // 特化模版,启用对应的重载 12 | namespace std { 13 | template<> 14 | struct is_error_code_enum: true_type {}; 15 | } 16 | 17 | // 提供工厂函数 18 | // 工厂函数不必要写在 std 中 19 | std::error_code make_error_code(YourErrorCode code) 20 | { 21 | return { 22 | static_cast(code), 23 | std::generic_category(), // 这里暂时用自带的 category 24 | }; 25 | } 26 | 27 | int main() 28 | { 29 | std::error_code e = YourErrorCode::kBadRequest; 30 | std::cout << e << '\n'; // 自带一个输出流的重载 31 | } 32 | -------------------------------------------------------------------------------- /cpp_11/003_error_std_generic_category.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main() 6 | { 7 | std::error_condition econd = std::generic_category().default_error_condition(EDOM); 8 | std::cout << "Category: " << econd.category().name() << '\n' 9 | << "Value: " << econd.value() << '\n' 10 | << "Message: " << econd.message() << '\n'; 11 | } 12 | -------------------------------------------------------------------------------- /cpp_11/003_error_std_system_error.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | try { 8 | std::thread().detach(); // attempt to detach a non-thread 9 | } catch(const std::system_error& e) { 10 | std::cout << "Caught system_error with code " << e.code() 11 | << " meaning " << e.what() << '\n'; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_exception_std_current_exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void handle_eptr(std::exception_ptr eptr) // 按值传递 ok 7 | { 8 | try { 9 | if (eptr) { 10 | std::rethrow_exception(eptr); 11 | } 12 | } catch(const std::exception& e) { 13 | std::cout << "Caught exception \"" << e.what() << "\"\n"; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | std::exception_ptr eptr; 20 | try { 21 | std::string().at(1); // 这生成一个 std::out_of_range 22 | } catch(...) { 23 | eptr = std::current_exception(); // 捕获 24 | } 25 | handle_eptr(eptr); 26 | } // std::out_of_range 的析构函数调用于此,在 ept 析构时 27 | -------------------------------------------------------------------------------- /cpp_11/003_exception_std_exception_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void handle_eptr(std::exception_ptr eptr) // passing by value is ok 7 | { 8 | try { 9 | if (eptr) { 10 | std::rethrow_exception(eptr); 11 | } 12 | } catch(const std::exception& e) { 13 | std::cout << "Caught exception \"" << e.what() << "\"\n"; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | std::exception_ptr eptr; 20 | try { 21 | std::string().at(1); // this generates an std::out_of_range 22 | } catch(...) { 23 | eptr = std::current_exception(); // capture 24 | } 25 | handle_eptr(eptr); 26 | } // destructor for std::out_of_range called here, when the eptr is destructed 27 | -------------------------------------------------------------------------------- /cpp_11/003_exception_std_make_exception_ptr.cpp: -------------------------------------------------------------------------------- 1 | // make_exception_ptr example 2 | #include // std::cout 3 | #include // std::make_exception_ptr, std::rethrow_exception 4 | #include // std::logic_error 5 | 6 | int main () { 7 | auto p = std::make_exception_ptr(std::logic_error("logic_error")); 8 | 9 | try { 10 | std::rethrow_exception (p); 11 | } catch (const std::exception& e) { 12 | std::cout << "exception caught: " << e.what() << '\n'; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_exception_std_rethrow_exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void handle_eptr(std::exception_ptr eptr) // passing by value is ok 7 | { 8 | try { 9 | if (eptr) { 10 | std::rethrow_exception(eptr); 11 | } 12 | } catch(const std::exception& e) { 13 | std::cout << "Caught exception \"" << e.what() << "\"\n"; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | std::exception_ptr eptr; 20 | try { 21 | std::string().at(1); // this generates an std::out_of_range 22 | } catch(...) { 23 | eptr = std::current_exception(); // capture 24 | } 25 | handle_eptr(eptr); 26 | } // destructor for std::out_of_range called here, when the eptr is destructed 27 | -------------------------------------------------------------------------------- /cpp_11/003_function_CV_restricted.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_function_object_template_mem_fn.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_function_object_template_std_bad_function_call.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_function_object_template_std_bind.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_function_object_template_std_function.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_function_references_qualifiers.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_regex_basic_regex.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_regex_basic_sub_match.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_regex_match_results.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_alignment_of.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::alignment_of::value << '\n'; 9 | std::cout << std::alignment_of() << '\n'; // 另一种语法 10 | std::cout << std::alignment_of_v << '\n'; // c++17 另一种语法 11 | } 12 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_extent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::extent::value << '\n'; // < 默认维度为 0 7 | std::cout << std::extent::value << '\n'; 8 | std::cout << std::extent::value << '\n'; 9 | std::cout << std::extent::value << '\n'; 10 | std::cout << std::extent::value << '\n'; 11 | 12 | const auto ext = std::extent{}; 13 | std::cout << ext << '\n'; // < 隐式转换到 std::size_t 14 | 15 | const int ints[] = {1,2,3,4}; 16 | std::cout << std::extent::value << '\n'; // < 数组大小 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_rank.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::rank::value << '\n'; 7 | std::cout << std::rank::value << '\n'; 8 | std::cout << std::rank::value << '\n'; 9 | } 10 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_has_unique_object_representations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | int m1; 10 | private: 11 | int m2; 12 | }; 13 | 14 | struct C { 15 | virtual void foo(); 16 | }; 17 | 18 | int main() 19 | { 20 | std::cout << std::boolalpha; 21 | std::cout << std::is_pod::value << '\n'; 22 | std::cout << std::is_pod::value << '\n'; 23 | std::cout << std::is_pod::value << '\n'; 24 | } 25 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_has_virtual_destructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha 9 | << "std::string has a virtual destructor? " 10 | << std::has_virtual_destructor::value << '\n' 11 | << "std::runtime_error has a virtual destructor? " 12 | << std::has_virtual_destructor::value << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_abstract.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | virtual void foo(); 10 | }; 11 | 12 | struct C { 13 | virtual void foo() = 0; 14 | }; 15 | 16 | struct D : C {}; 17 | 18 | int main() 19 | { 20 | std::cout << std::boolalpha; 21 | std::cout << std::is_abstract::value << '\n'; 22 | std::cout << std::is_abstract::value << '\n'; 23 | std::cout << std::is_abstract::value << '\n'; 24 | std::cout << std::is_abstract::value << '\n'; 25 | } 26 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_aggregate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // 于 p 所指向的未初始化内存构造 T 6 | // 用聚合体的列表初始化,否则使用非列表初始化 7 | template 8 | T* construct(T* p, Args&&... args) { 9 | if constexpr(std::is_aggregate_v) { 10 | return ::new (static_cast(p)) T{std::forward(args)...}; 11 | } 12 | else { 13 | return ::new (static_cast(p)) T(std::forward(args)...); 14 | } 15 | } 16 | 17 | struct A { int x, y; }; 18 | struct B { B(int, const char*) { } }; 19 | 20 | int main() { 21 | std::aligned_union_t<1, A, B> storage; 22 | A* a = construct(reinterpret_cast(&storage), 1, 2); 23 | B* b = construct(reinterpret_cast(&storage), 1, "hello"); 24 | } 25 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class A {}; 6 | 7 | int main() 8 | { 9 | std::cout << std::boolalpha; 10 | std::cout << std::is_array::value << '\n'; 11 | std::cout << std::is_array::value << '\n'; 12 | std::cout << std::is_array::value << '\n'; 13 | std::cout << std::is_array::value << '\n'; 14 | std::cout << std::is_array::value << '\n'; 15 | std::cout << std::is_array::value << '\n'; 16 | std::cout << std::is_array::value << '\n'; 17 | std::cout << std::is_array>::value << '\n'; 18 | } 19 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_base_of.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | class B : A {}; 7 | 8 | class C {}; 9 | 10 | int main() 11 | { 12 | std::cout << std::boolalpha; 13 | std::cout << "a2b: " << std::is_base_of::value << '\n'; 14 | std::cout << "b2a: " << std::is_base_of::value << '\n'; 15 | std::cout << "c2b: " << std::is_base_of::value << '\n'; 16 | std::cout << "same type: " << std::is_base_of::value << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_bounded_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_bounded_array_v << '\n'; 10 | std::cout << std::is_bounded_array_v << '\n'; 11 | std::cout << std::is_bounded_array_v << '\n'; 12 | std::cout << std::is_bounded_array_v << '\n'; 13 | std::cout << std::is_bounded_array_v << '\n'; 14 | std::cout << std::is_bounded_array_v << '\n'; 15 | std::cout << std::is_bounded_array_v << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A {}; 5 | 6 | class B {}; 7 | 8 | enum class C {}; 9 | 10 | int main() 11 | { 12 | std::cout << std::boolalpha; 13 | std::cout << std::is_class::value << '\n'; 14 | std::cout << std::is_class::value << '\n'; 15 | std::cout << std::is_class::value << '\n'; 16 | std::cout << std::is_class::value << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_compound.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | class cls {}; 6 | std::cout << (std::is_compound::value 7 | ? "T is compound" 8 | : "T is not a compound") << '\n'; 9 | std::cout << (std::is_compound::value 10 | ? "T is compound" 11 | : "T is not a compound") << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_const.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::boolalpha 7 | << std::is_const_v << '\n' // false 8 | << std::is_const_v << '\n' // true 9 | << std::is_const_v /*false*/ 10 | << " because the pointer itself can be changed but not the int pointed at\n" 11 | << std::is_const_v /*true*/ 12 | << " because the pointer itself can't be changed but the int pointed at can\n" 13 | << std::is_const_v << '\n' // false 14 | << std::is_const_v> << '\n' // true 15 | ; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_copy_assignable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { int n; }; 5 | int main() { 6 | std::cout << std::boolalpha 7 | << "Foo is trivally copy-assignable? " 8 | << std::is_trivially_copy_assignable::value << '\n' 9 | << "int[2] is copy-assignable? " 10 | << std::is_copy_assignable::value << '\n' 11 | << "int is nothrow copy-assignable? " 12 | << std::is_nothrow_copy_assignable::value << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_copy_constructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // 成员拥有非平凡复制构造函数 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2(const Ex2&) = default; // 平凡且不抛出 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is copy-constructible? " 14 | << std::is_copy_constructible::value << '\n' 15 | << "Ex1 is trivially copy-constructible? " 16 | << std::is_trivially_copy_constructible::value << '\n' 17 | << "Ex2 is trivially copy-constructible? " 18 | << std::is_trivially_copy_constructible::value << '\n' 19 | << "Ex2 is nothrow copy-constructible? " 20 | << std::is_nothrow_copy_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_default_constructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // 成员拥有非平凡默认构造函数 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2() = default; // 平凡且不抛出 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is default-constructible? " 14 | << std::is_default_constructible::value << '\n' 15 | << "Ex1 is trivially default-constructible? " 16 | << std::is_trivially_default_constructible::value << '\n' 17 | << "Ex2 is trivially default-constructible? " 18 | << std::is_trivially_default_constructible::value << '\n' 19 | << "Ex2 is nothrow default-constructible? " 20 | << std::is_nothrow_default_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_destructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { 5 | std::string str; 6 | ~Foo() noexcept {}; 7 | }; 8 | struct Bar { 9 | ~Bar() = default; 10 | }; 11 | int main() { 12 | std::cout << std::boolalpha 13 | << "std::string is destructible? " 14 | << std::is_destructible::value << '\n' 15 | << "Foo is nothrow destructible? " 16 | << std::is_nothrow_destructible::value << '\n' 17 | << "Bar is trivally destructible? " 18 | << std::is_trivially_destructible::value << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_enum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | enum E {}; 7 | 8 | enum class Ec : int {}; 9 | 10 | int main() 11 | { 12 | std::cout << std::boolalpha; 13 | std::cout << std::is_enum::value << '\n'; 14 | std::cout << std::is_enum::value << '\n'; 15 | std::cout << std::is_enum::value << '\n'; 16 | std::cout << std::is_enum::value << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_final.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | class B final {}; 6 | 7 | int main() 8 | { 9 | std::cout 10 | << std::boolalpha 11 | << std::is_final::value << '\n' 12 | << std::is_final::value << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_floating_point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_floating_point::value << '\n'; 10 | std::cout << std::is_floating_point::value << '\n'; 11 | std::cout << std::is_floating_point::value << '\n'; 12 | std::cout << std::is_floating_point::value << '\n'; 13 | std::cout << std::is_floating_point::value << '\n'; 14 | std::cout << std::is_floating_point::value << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int fun() const&; 6 | }; 7 | 8 | template 9 | struct PM_traits {}; 10 | 11 | template 12 | struct PM_traits { 13 | using member_type = U; 14 | }; 15 | 16 | int f(); 17 | 18 | int main() 19 | { 20 | std::cout << std::boolalpha; 21 | std::cout << std::is_function::value << '\n'; 22 | std::cout << std::is_function::value << '\n'; 23 | std::cout << std::is_function::value << '\n'; 24 | std::cout << std::is_function::value << '\n'; 25 | 26 | using T = PM_traits::member_type; // T 为 int() const& 27 | std::cout << std::is_function::value << '\n'; 28 | } 29 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_fundamental.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << "A\t" << std::is_fundamental::value << '\n'; 10 | std::cout << "int\t" << std::is_fundamental::value << '\n'; 11 | std::cout << "int&\t" << std::is_fundamental::value << '\n'; 12 | std::cout << "int*\t" << std::is_fundamental::value << '\n'; 13 | std::cout << "float\t" << std::is_fundamental::value << '\n'; 14 | std::cout << "float&\t" << std::is_fundamental::value << '\n'; 15 | std::cout << "float*\t" << std::is_fundamental::value << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_integral.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | enum E : int {}; 7 | 8 | template 9 | T f(T i) 10 | { 11 | static_assert(std::is_integral::value, "Integral required."); 12 | return i; 13 | } 14 | 15 | int main() 16 | { 17 | std::cout << std::boolalpha; 18 | std::cout << std::is_integral::value << '\n'; 19 | std::cout << std::is_integral::value << '\n'; 20 | std::cout << std::is_integral::value << '\n'; 21 | std::cout << std::is_integral::value << '\n'; 22 | std::cout << std::is_integral::value << '\n'; 23 | std::cout << f(123) << '\n'; 24 | } 25 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_lvalue_reference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_lvalue_reference::value << '\n'; 10 | std::cout << std::is_lvalue_reference::value << '\n'; 11 | std::cout << std::is_lvalue_reference::value << '\n'; 12 | std::cout << std::is_lvalue_reference::value << '\n'; 13 | std::cout << std::is_lvalue_reference::value << '\n'; 14 | std::cout << std::is_lvalue_reference::value << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_member_function_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class A { 4 | public: 5 | void member() { } 6 | }; 7 | 8 | int main() 9 | { 10 | // 若 A::member 是数据成员而非函数,则在编译时失败 11 | static_assert(std::is_member_function_pointer::value, 12 | "A::member is not a member function."); 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_member_object_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | class cls {}; 6 | std::cout << (std::is_member_object_pointer::value 7 | ? "T is member object pointer" 8 | : "T is not a member object pointer") << '\n'; 9 | std::cout << (std::is_member_object_pointer::value 10 | ? "T is member object pointer" 11 | : "T is not a member object pointer") << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_member_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | class cls {}; 6 | std::cout << (std::is_member_pointer::value 7 | ? "T is member pointer" 8 | : "T is not a member pointer") << '\n'; 9 | std::cout << (std::is_member_pointer::value 10 | ? "T is member pointer" 11 | : "T is not a member pointer") << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_nothrow_copy_assignable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { int n; }; 5 | int main() { 6 | std::cout << std::boolalpha 7 | << "Foo is trivally copy-assignable? " 8 | << std::is_trivially_copy_assignable::value << '\n' 9 | << "int[2] is copy-assignable? " 10 | << std::is_copy_assignable::value << '\n' 11 | << "int is nothrow copy-assignable? " 12 | << std::is_nothrow_copy_assignable::value << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_nothrow_copy_constructiblee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // 成员拥有非平凡复制构造函数 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2(const Ex2&) = default; // 平凡且不抛出 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is copy-constructible? " 14 | << std::is_copy_constructible::value << '\n' 15 | << "Ex1 is trivially copy-constructible? " 16 | << std::is_trivially_copy_constructible::value << '\n' 17 | << "Ex2 is trivially copy-constructible? " 18 | << std::is_trivially_copy_constructible::value << '\n' 19 | << "Ex2 is nothrow copy-constructible? " 20 | << std::is_nothrow_copy_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_nothrow_default_constructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // 成员拥有非平凡默认构造函数 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2() = default; // 平凡且不抛出 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is default-constructible? " 14 | << std::is_default_constructible::value << '\n' 15 | << "Ex1 is trivially default-constructible? " 16 | << std::is_trivially_default_constructible::value << '\n' 17 | << "Ex2 is trivially default-constructible? " 18 | << std::is_trivially_default_constructible::value << '\n' 19 | << "Ex2 is nothrow default-constructible? " 20 | << std::is_nothrow_default_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_nothrow_destructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { 5 | std::string str; 6 | ~Foo() noexcept {}; 7 | }; 8 | struct Bar { 9 | ~Bar() = default; 10 | }; 11 | int main() { 12 | std::cout << std::boolalpha 13 | << "std::string is destructible? " 14 | << std::is_destructible::value << '\n' 15 | << "Foo is nothrow destructible? " 16 | << std::is_nothrow_destructible::value << '\n' 17 | << "Bar is trivally destructible? " 18 | << std::is_trivially_destructible::value << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_nothrow_swappable.cpp: -------------------------------------------------------------------------------- 1 | 暂无示例 2 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_nothrow_swappable_with.cpp: -------------------------------------------------------------------------------- 1 | 暂无示例 2 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_null_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::boolalpha 7 | << std::is_null_pointer< decltype(nullptr) >::value << ' ' 8 | << std::is_null_pointer< int* >::value << '\n' 9 | << std::is_pointer< decltype(nullptr) >::value << ' ' 10 | << std::is_pointer::value << '\n'; 11 | } 12 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_object.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | class cls {}; 6 | std::cout << std::boolalpha; 7 | std::cout << std::is_object::value << '\n'; 8 | std::cout << std::is_object::value << '\n'; 9 | std::cout << std::is_object::value << '\n'; 10 | std::cout << std::is_object::value << '\n'; 11 | } 12 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_pod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | int m1; 10 | private: 11 | int m2; 12 | }; 13 | 14 | struct C { 15 | virtual void foo(); 16 | }; 17 | 18 | int main() 19 | { 20 | std::cout << std::boolalpha; 21 | std::cout << std::is_pod::value << '\n'; 22 | std::cout << std::is_pod::value << '\n'; 23 | std::cout << std::is_pod::value << '\n'; 24 | } 25 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_pointer::value << '\n'; 10 | std::cout << std::is_pointer::value << '\n'; 11 | std::cout << std::is_pointer::value << '\n'; 12 | std::cout << std::is_pointer::value << '\n'; 13 | std::cout << std::is_pointer::value << '\n'; 14 | std::cout << std::is_pointer::value << '\n'; 15 | std::cout << std::is_pointer::value << '\n'; 16 | std::cout << std::is_pointer::value << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_polymorphic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | virtual void foo(); 10 | }; 11 | 12 | struct C : B {}; 13 | 14 | int main() 15 | { 16 | std::cout << std::boolalpha; 17 | std::cout << std::is_polymorphic::value << '\n'; 18 | std::cout << std::is_polymorphic::value << '\n'; 19 | std::cout << std::is_polymorphic::value << '\n'; 20 | } 21 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_reference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_reference::value << '\n'; 10 | std::cout << std::is_reference::value << '\n'; 11 | std::cout << std::is_reference::value << '\n'; 12 | std::cout << std::is_reference::value << '\n'; 13 | std::cout << std::is_reference::value << '\n'; 14 | std::cout << std::is_reference::value << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_rvalue_reference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_rvalue_reference::value << '\n'; 10 | std::cout << std::is_rvalue_reference::value << '\n'; 11 | std::cout << std::is_rvalue_reference::value << '\n'; 12 | std::cout << std::is_rvalue_reference::value << '\n'; 13 | std::cout << std::is_rvalue_reference::value << '\n'; 14 | std::cout << std::is_rvalue_reference::value << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_scalar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | class cls {}; 6 | std::cout << (std::is_scalar::value 7 | ? "T is a scalar" 8 | : "T is not a scalar") << '\n'; 9 | std::cout << (std::is_scalar::value 10 | ? "T is a scalar" 11 | : "T is not a scalar") << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_signed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | enum B : int {}; 6 | enum class C : int {}; 7 | 8 | int main() 9 | { 10 | std::cout << std::boolalpha; 11 | std::cout << std::is_signed::value << '\n'; 12 | std::cout << std::is_signed::value << '\n'; 13 | std::cout << std::is_signed::value << '\n'; 14 | std::cout << std::is_signed::value << '\n'; 15 | std::cout << std::is_signed::value << '\n'; 16 | std::cout << std::is_signed::value << '\n'; 17 | 18 | // 简短的形式: 19 | std::cout << std::is_signed() << '\n'; 20 | std::cout << std::is_signed() << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_standard_layout.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | int m1; 10 | private: 11 | int m2; 12 | }; 13 | 14 | struct C { 15 | virtual void foo(); 16 | }; 17 | 18 | int main() 19 | { 20 | std::cout << std::boolalpha; 21 | std::cout << std::is_standard_layout::value << '\n'; 22 | std::cout << std::is_standard_layout::value << '\n'; 23 | std::cout << std::is_standard_layout::value << '\n'; 24 | } 25 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_swappable.cpp: -------------------------------------------------------------------------------- 1 | 暂无示例 2 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_swappable_with.cpp: -------------------------------------------------------------------------------- 1 | 暂无示例 2 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_trivial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | B() {} 10 | }; 11 | 12 | int main() 13 | { 14 | std::cout << std::boolalpha; 15 | std::cout << std::is_trivial::value << '\n'; 16 | std::cout << std::is_trivial::value << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_trivially_copy_assignable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { int n; }; 5 | int main() { 6 | std::cout << std::boolalpha 7 | << "Foo is trivally copy-assignable? " 8 | << std::is_trivially_copy_assignable::value << '\n' 9 | << "int[2] is copy-assignable? " 10 | << std::is_copy_assignable::value << '\n' 11 | << "int is nothrow copy-assignable? " 12 | << std::is_nothrow_copy_assignable::value << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_trivially_copy_constructible.cp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // 成员拥有非平凡复制构造函数 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2(const Ex2&) = default; // 平凡且不抛出 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is copy-constructible? " 14 | << std::is_copy_constructible::value << '\n' 15 | << "Ex1 is trivially copy-constructible? " 16 | << std::is_trivially_copy_constructible::value << '\n' 17 | << "Ex2 is trivially copy-constructible? " 18 | << std::is_trivially_copy_constructible::value << '\n' 19 | << "Ex2 is nothrow copy-constructible? " 20 | << std::is_nothrow_copy_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_trivially_copyable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | B(const B&) {} 10 | }; 11 | 12 | struct C { 13 | virtual void foo(); 14 | }; 15 | 16 | int main() 17 | { 18 | std::cout << std::boolalpha; 19 | std::cout << std::is_trivially_copyable::value << '\n'; 20 | std::cout << std::is_trivially_copyable::value << '\n'; 21 | std::cout << std::is_trivially_copyable::value << '\n'; 22 | } 23 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_trivially_default_constructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // 成员拥有非平凡默认构造函数 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2() = default; // 平凡且不抛出 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is default-constructible? " 14 | << std::is_default_constructible::value << '\n' 15 | << "Ex1 is trivially default-constructible? " 16 | << std::is_trivially_default_constructible::value << '\n' 17 | << "Ex2 is trivially default-constructible? " 18 | << std::is_trivially_default_constructible::value << '\n' 19 | << "Ex2 is nothrow default-constructible? " 20 | << std::is_nothrow_default_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_trivially_destructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { 5 | std::string str; 6 | ~Foo() noexcept {}; 7 | }; 8 | struct Bar { 9 | ~Bar() = default; 10 | }; 11 | int main() { 12 | std::cout << std::boolalpha 13 | << "std::string is destructible? " 14 | << std::is_destructible::value << '\n' 15 | << "Foo is nothrow destructible? " 16 | << std::is_nothrow_destructible::value << '\n' 17 | << "Bar is trivally destructible? " 18 | << std::is_trivially_destructible::value << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_unbounded_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_unbounded_array_v << '\n'; 10 | std::cout << std::is_unbounded_array_v << '\n'; 11 | std::cout << std::is_unbounded_array_v << '\n'; 12 | std::cout << std::is_unbounded_array_v << '\n'; 13 | std::cout << std::is_unbounded_array_v << '\n'; 14 | std::cout << std::is_unbounded_array_v << '\n'; 15 | std::cout << std::is_unbounded_array_v << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_union.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A {}; 5 | 6 | typedef union { 7 | int a; 8 | float b; 9 | } B; 10 | 11 | struct C { 12 | B d; 13 | }; 14 | 15 | int main() 16 | { 17 | std::cout << std::boolalpha; 18 | std::cout << std::is_union::value << '\n'; 19 | std::cout << std::is_union::value << '\n'; 20 | std::cout << std::is_union::value << '\n'; 21 | std::cout << std::is_union::value << '\n'; 22 | } 23 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_unsigned.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | enum B : unsigned {}; 6 | enum class C : unsigned {}; 7 | 8 | int main() 9 | { 10 | std::cout << std::boolalpha; 11 | std::cout << std::is_unsigned::value << '\n'; 12 | std::cout << std::is_unsigned::value << '\n'; 13 | std::cout << std::is_unsigned::value << '\n'; 14 | std::cout << std::is_unsigned::value << '\n'; 15 | std::cout << std::is_unsigned::value << '\n'; 16 | std::cout << std::is_unsigned::value << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_void.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::boolalpha; 7 | std::cout << std::is_void::value << '\n'; 8 | std::cout << std::is_void::value << '\n'; 9 | } 10 | -------------------------------------------------------------------------------- /cpp_11/003_rtti_std_is_volatile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::boolalpha; 7 | std::cout << std::is_volatile::value << '\n'; 8 | std::cout << std::is_volatile::value << '\n'; 9 | } 10 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_long_long.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | std::atomic data{10}; 7 | std::array return_values{}; 8 | 9 | void do_work(int thread_num) 10 | { 11 | long long val = data.fetch_add(1, std::memory_order_relaxed); 12 | return_values[thread_num] = val; 13 | } 14 | 15 | int main() 16 | { 17 | { 18 | std::jthread th0{do_work, 0}; 19 | std::jthread th1{do_work, 1}; 20 | std::jthread th2{do_work, 2}; 21 | std::jthread th3{do_work, 3}; 22 | std::jthread th4{do_work, 4}; 23 | } 24 | 25 | std::cout << "Result : " << data << '\n'; 26 | 27 | for (long long val : return_values) { 28 | std::cout << "Seen return value : " << val << std::endl; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_std_intmax_t.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_std_intptr_t.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_std_ptrdiff_t.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_std_size_t.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_std_uintmax_t.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_std_atomic_std_uintptr_t.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_std_thread_std_thread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void foo() 6 | { 7 | // simulate expensive operation 8 | std::this_thread::sleep_for(std::chrono::seconds(1)); 9 | } 10 | 11 | void bar() 12 | { 13 | // simulate expensive operation 14 | std::this_thread::sleep_for(std::chrono::seconds(1)); 15 | } 16 | 17 | int main() 18 | { 19 | std::cout << "starting first helper...\n"; 20 | std::thread helper1(foo); 21 | 22 | std::cout << "starting second helper...\n"; 23 | std::thread helper2(bar); 24 | 25 | std::cout << "waiting for helpers to finish..." << std::endl; 26 | helper1.join(); 27 | helper2.join(); 28 | 29 | std::cout << "done!\n"; 30 | } 31 | -------------------------------------------------------------------------------- /cpp_11/003_std_thread_std_thread_local.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | thread_local int g_n = 1; 4 | 5 | void f() 6 | { 7 | g_n++; 8 | printf("id=%d, n=%d\n", std::this_thread::get_id(),g_n); 9 | } 10 | 11 | void foo() 12 | { 13 | thread_local int i=0; 14 | printf("id=%d, n=%d\n", std::this_thread::get_id(), i); 15 | i++; 16 | } 17 | 18 | void f2() 19 | { 20 | foo(); 21 | foo(); 22 | } 23 | 24 | int main() 25 | { 26 | g_n++; 27 | f(); 28 | std::thread t1(f); 29 | std::thread t2(f); 30 | 31 | t1.join(); 32 | t2.join(); 33 | 34 | 35 | f2(); 36 | std::thread t4(f2); 37 | std::thread t5(f2); 38 | 39 | t4.join(); 40 | t5.join(); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /cpp_11/003_std_thread_volatile.cpp: -------------------------------------------------------------------------------- 1 | //volatile作用: 作为指令关键字,确保本条指令不会受到编译器的优化而省略,而且要求每次直接读值。 2 | 3 | //定义: 4 | 5 | volatile int nTest; 6 | //volatile关键字是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改,比如: 7 | //操作系统、硬件或者其它线程等。遇到这个关键字声明的变量,编译器对访问该变量的代码就不再进行优化, 8 | //从而可以提供对特殊地址的稳定访问。 9 | -------------------------------------------------------------------------------- /cpp_11/003_stl_auto_ptr.cpp: -------------------------------------------------------------------------------- 1 | 弃用 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_container_init.cpp: -------------------------------------------------------------------------------- 1 | //C++ 11 统一初始化方法 2 | //变量,数组,STL容器,类的构造的初始化都可以使用{}方法 3 | class test { 4 | int a ,b,c[4]; 5 | int *d = new int[3]{1,2,3,4};//C++ 11提供的独有的初始化方式 6 | vector vec = {1,2,3}; //c++ 11 独有的 7 | map _map = {{"hello",1},{"world",2}};//c++ 11 独有的 8 | public: 9 | test(int i ,int j):a(i),b(j),c{2,3,4,2}{}; 10 | }; 11 | 12 | int main(int argc, const char * argv[]) 13 | { 14 | 15 | test t{1,2};//初始化test类 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_bool.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_char.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_char16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_char32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_char8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_int.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_long.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_long_long.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_short.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_signed_char.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int64_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_fast16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_fast32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_fast64_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_fast8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_least16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_least32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_least64_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_int_least8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_intmax_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_intptr_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_ptrdiff_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_size_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint64_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_fast16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_fast32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_fast64_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_fast8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_least16_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_least32_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_least64_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uint_least8_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uintmax_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_std_uintptr_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_unsigned_char.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_unsigned_int.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_unsigned_long.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_unsigned_long_long.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_unsigned_short.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_atomic_wchar_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_begin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v = { 3, 1, 4 }; 8 | auto vi = std::begin(v); 9 | std::cout << std::showpos << *vi << '\n'; 10 | 11 | int a[] = { -5, 10, 15 }; 12 | auto ai = std::begin(a); 13 | std::cout << *ai << '\n'; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::vector v = { 3, 1, 4 }; 9 | if (std::find(std::begin(v), std::end(v), 5) != std::end(v)) { 10 | std::cout << "found a 5 in vector v!\n"; 11 | } 12 | 13 | int a[] = { 5, 10, 15 }; 14 | if (std::find(std::begin(a), std::end(a), 5) != std::end(a)) { 15 | std::cout << "found a 5 in array a!\n"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_forward_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::forward_list numbers; 7 | std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n'; 8 | 9 | numbers.push_front(42); 10 | numbers.push_front(13317); 11 | std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_get.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | auto t = std::make_tuple(1, "Foo", 3.14); 8 | // index-based access 9 | std::cout << "(" << std::get<0>(t) << ", " << std::get<1>(t) 10 | << ", " << std::get<2>(t) << ")\n"; 11 | // type-based access (C++14 or later) 12 | std::cout << "(" << std::get(t) << ", " << std::get(t) 13 | << ", " << std::get(t) << ")\n"; 14 | // Note: std::tie and structured binding may also be used to decompose a tuple 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_hash_std_bitset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::bitset<4> b1{0}, b2{42}; 8 | std::bitset<8> b3{0}, b4{42}; 9 | 10 | std::hash> hash_fn4; 11 | std::hash> hash_fn8; 12 | using bin64 = std::bitset<64>; 13 | 14 | std::cout 15 | << bin64{hash_fn4(b1)} << " = " << hash_fn4(b1) << '\n' 16 | << bin64{hash_fn4(b2)} << " = " << hash_fn4(b2) << '\n' 17 | << bin64{hash_fn8(b3)} << " = " << hash_fn8(b3) << '\n' 18 | << bin64{hash_fn8(b4)} << " = " << hash_fn8(b4) << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_hash_std_error_code.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_hash_std_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std::literals; 7 | 8 | int main() 9 | { 10 | auto sv = "Stand back! I've got jimmies!"sv; 11 | std::string s(sv); 12 | std::pmr::string pmrs(sv); // use default allocator 13 | 14 | std::cout << std::hash{}(sv) << '\n'; 15 | std::cout << std::hash{}(s) << '\n'; 16 | std::cout << std::hash{}(pmrs) << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_hash_std_type_index.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_make_tuple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | std::tuple f() // this function returns multiple values 6 | { 7 | int x = 5; 8 | return std::make_tuple(x, 7); // return {x,7}; in C++17 9 | } 10 | 11 | int main() 12 | { 13 | // heterogeneous tuple construction 14 | int n = 1; 15 | auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n); 16 | n = 7; 17 | std::cout << "The value of t is " << "(" 18 | << std::get<0>(t) << ", " << std::get<1>(t) << ", " 19 | << std::get<2>(t) << ", " << std::get<3>(t) << ", " 20 | << std::get<4>(t) << ")\n"; 21 | 22 | // function returning multiple values 23 | int a, b; 24 | std::tie(a, b) = f(); 25 | std::cout << a << " " << b << "\n"; 26 | } 27 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_move.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main() 6 | { 7 | std::string str = "Hello"; 8 | std::vector v; 9 | //调用常规的拷贝构造函数,新建字符数组,拷贝数据 10 | v.push_back(str); 11 | std::cout << "After copy, str is \"" << str << "\"\n"; 12 | //调用移动构造函数,掏空str,掏空后,最好不要使用str 13 | v.push_back(std::move(str)); 14 | std::cout << "After move, str is \"" << str << "\"\n"; 15 | std::cout << "The contents of the vector are \"" << v[0] 16 | << "\", \"" << v[1] << "\"\n"; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_shared_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void fun(std::shared_ptr sp) 5 | { 6 | std::cout << "fun: sp.use_count() == " << sp.use_count() << '\n'; 7 | } 8 | 9 | int main() 10 | { 11 | auto sp1 = std::make_shared(5); 12 | std::cout << "sp1.use_count() == " << sp1.use_count() << '\n'; 13 | 14 | fun(sp1); 15 | } 16 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_tie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct S { 7 | int n; 8 | std::string s; 9 | float d; 10 | bool operator<(const S& rhs) const 11 | { 12 | // compares n to rhs.n, 13 | // then s to rhs.s, 14 | // then d to rhs.d 15 | return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d); 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | std::set set_of_s; // S is LessThanComparable 22 | 23 | S value{42, "Test", 3.14}; 24 | std::set::iterator iter; 25 | bool inserted; 26 | 27 | // unpacks the return value of insert into iter and inserted 28 | std::tie(iter, inserted) = set_of_s.insert(value); 29 | 30 | if (inserted) 31 | std::cout << "Value was inserted successfully\n"; 32 | } 33 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_unique_ptr.cpp: -------------------------------------------------------------------------------- 1 | struct Foo { 2 | Foo(int _val) : val(_val) { std::cout << "Foo...\n"; } 3 | ~Foo() { std::cout << "~Foo...\n"; } 4 | int val; 5 | }; 6 | 7 | int main() 8 | { 9 | std::unique_ptr up1(new Foo(1)); 10 | std::unique_ptr up2(new Foo(2)); 11 | 12 | up1.swap(up2); 13 | 14 | std::cout << "up1->val:" << up1->val << std::endl; 15 | std::cout << "up2->val:" << up2->val << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_unordered_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // 创建三个 string 的 unordered_map (映射到 string ) 8 | std::unordered_map u = { 9 | {"RED","#FF0000"}, 10 | {"GREEN","#00FF00"}, 11 | {"BLUE","#0000FF"} 12 | }; 13 | 14 | // 迭代并打印 unordered_map 的关键和值 15 | for( const auto& n : u ) { 16 | std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; 17 | } 18 | 19 | // 添加新入口到 unordered_map 20 | u["BLACK"] = "#000000"; 21 | u["WHITE"] = "#FFFFFF"; 22 | 23 | // 用关键输出值 24 | std::cout << "The HEX of color RED is:[" << u["RED"] << "]\n"; 25 | std::cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n"; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_unordered_multimap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::unordered_multimap numbers; 8 | std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n'; 9 | 10 | numbers.emplace(42, 13); 11 | numbers.insert(std::make_pair(13317, 123)); 12 | std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_unordered_multiset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template Os& operator<<(Os& os, const Co& co) { 5 | os << "{"; 6 | for (auto const& i : co) { os << ' ' << i; } 7 | return os << " } "; 8 | } 9 | 10 | int main() 11 | { 12 | std::unordered_multiset a1{3, 1, 3, 2}, a2{5, 4, 5}; 13 | 14 | auto it1 = std::next(a1.begin()); 15 | auto it2 = std::next(a2.begin()); 16 | 17 | const int& ref1 = *(a1.begin()); 18 | const int& ref2 = *(a2.begin()); 19 | 20 | std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n'; 21 | a1.swap(a2); 22 | std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n'; 23 | 24 | // 注意交换前指代一个容器中的元素的每个迭代器在交换后都指代同一元素。对于引用同为真。 25 | } 26 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_unordered_set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | std::multiset c = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 6 | // 从 c 擦除所有奇数 7 | for(auto it = c.begin(); it != c.end(); ) 8 | if(*it % 2 == 1) 9 | it = c.erase(it); 10 | else 11 | ++it; 12 | for(int n : c) 13 | std::cout << n << ' '; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_11/003_stl_std_weak_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void observe(std::weak_ptr weak) 5 | { 6 | if (auto observe = weak.lock()) { 7 | std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n"; 8 | } else { 9 | std::cout << "\tobserve() unable to lock weak_ptr<>\n"; 10 | } 11 | } 12 | 13 | int main() 14 | { 15 | std::weak_ptr weak; 16 | std::cout << "weak_ptr<> not yet initialized\n"; 17 | observe(weak); 18 | 19 | { 20 | auto shared = std::make_shared(42); 21 | weak = shared; 22 | std::cout << "weak_ptr<> initialized with shared_ptr.\n"; 23 | observe(weak); 24 | } 25 | 26 | std::cout << "shared_ptr<> has been destructed due to scope exit.\n"; 27 | observe(weak); 28 | } 29 | -------------------------------------------------------------------------------- /cpp_11/003_template_alias.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_template_aliases.cpp: -------------------------------------------------------------------------------- 1 | //gcc (GCC) 7.3.0 2 | #include 3 | #include 4 | using namespace std; 5 | template 6 | using ptr=std::shared_ptr; 7 | 8 | int main() 9 | { 10 | //这里借助模板别名,用ptr 来代替std::shared_ptr 11 | ptr p=std::make_shared(); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_11/003_template_angle_bracket.cpp: -------------------------------------------------------------------------------- 1 | template 2 | struct Foo 3 | { 4 | typedef T type; 5 | }; 6 | 7 | template 8 | class A 9 | { 10 | // ... 11 | }; 12 | 13 | int main() 14 | { 15 | Foo>::type xx; //C++11之前编译出错,要写成 Foo > 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /cpp_11/003_template_default_template_parameters.cpp: -------------------------------------------------------------------------------- 1 | template 2 | void f(T t = 0, U u = 0) {}; 3 | void g() 4 | { 5 | f(1, 'c'); // f(1, 'c') 6 | f(1); // f(1, 0), 使用了默认模板参数double 7 | f(); // 错误: T无法被推导出来 8 | f(); // f(0, 0), 使用了默认模板参数double 9 | f(); // f(0, 0) 10 | } 11 | -------------------------------------------------------------------------------- /cpp_11/003_template_external.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_11/003_template_external_template.cpp: -------------------------------------------------------------------------------- 1 | // test.h 2 | template 3 | void fun(T) { } 4 | 5 | // test1.cpp 6 | #include "test.h" 7 | void test1() 8 | { 9 | fun(1); 10 | } 11 | 12 | // test2.cpp 13 | #include "test.h" 14 | void test2() 15 | { 16 | fun(2); 17 | } 18 | -------------------------------------------------------------------------------- /cpp_11/003_template_variable_parameter_template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int end_fun(int& a){ 4 | return a; 5 | } 6 | 7 | template 8 | int end_fun(T& arg,Args... args){ 9 | return arg + end_fun(args...); 10 | } 11 | 12 | 13 | int main(int argc,char *argv[]){ 14 | 15 | char a = 'a'; 16 | int b = 2; 17 | int c = 3; 18 | int res = end_fun(a,b,c); 19 | std::cout<<"Res: "< 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v = { 3, 1, 4 }; 8 | auto vi = std::rbegin(v); // the type of `vi` is std::vector::reverse_iterator 9 | std::cout << "*vi = " << *vi << '\n'; 10 | 11 | *std::rbegin(v) = 42; // OK: after assignment v[2] == 42 12 | // *std::crbegin(v) = 13; // error: the location is read-only 13 | 14 | int a[] = { -5, 10, 15 }; 15 | auto ai = std::rbegin(a); // the type of `ai` is std::reverse_iterator 16 | std::cout << "*ai = " << *ai << '\n'; 17 | 18 | auto il = { 3, 1, 4 }; 19 | // the type of `it` below is std::reverse_iterator: 20 | for (auto it = std::rbegin(il); it != std::rend(il); ++it) 21 | std::cout << *it << ' '; 22 | } 23 | -------------------------------------------------------------------------------- /cpp_14/001_initializer_list_crend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int a[] = {4, 6, -3, 9, 10}; 9 | std::cout << "Array backwards: "; 10 | std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator(std::cout, " ")); 11 | 12 | std::cout << "\nVector backwards: "; 13 | std::vector v = {4, 6, -3, 9, 10}; 14 | std::copy(std::rbegin(v), std::crend(v), std::ostream_iterator(std::cout, " ")); 15 | } 16 | -------------------------------------------------------------------------------- /cpp_14/001_initializer_list_rbegin.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/001_initializer_list_rend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | auto il = { 3, 1, 4 }; 7 | for (auto it = std::rbegin(il); it != std::rend(il); ++it) 8 | std::cout << *it << ' '; 9 | } 10 | -------------------------------------------------------------------------------- /cpp_14/001_iterator_make_reverse_iterator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | std::vector v{ 1, 3, 10, 8, 22 }; 8 | 9 | std::sort(v.begin(), v.end()); 10 | std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, ", ")); 11 | 12 | std::cout << '\n'; 13 | 14 | std::copy( 15 | std::make_reverse_iterator(v.end()), 16 | std::make_reverse_iterator(v.begin()), 17 | std::ostream_iterator(std::cout, ", ")); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_14/001_stl_begin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int ia[] = {0,1,2,3}; 8 | int *beg = begin(ia); 9 | int *last = end(ia); 10 | cout << *beg << endl; 11 | cout << *(last-1) << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_14/001_stl_cbegin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main () 7 | { 8 | set myset= {"Java", "C++","SQL"}; 9 | 10 | // 显示内容: 11 | for (auto it = myset.cbegin(); it != myset.cend(); ++it) 12 | cout <<*it << '\n'; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_14/001_stl_cend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main () 7 | { 8 | set myset= {"Java", "C++","SQL"}; 9 | 10 | // 显示内容: 11 | for (auto it = myset.cbegin(); it != myset.cend(); ++it) 12 | cout <<*it << '\n'; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_14/001_stl_crbegin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | // initialize container 8 | map mp; 9 | 10 | // insert elements in random order 11 | mp.insert({ 2, 30 }); 12 | mp.insert({ 1, 40 }); 13 | mp.insert({ 3, 60 }); 14 | mp.insert({ 4, 20 }); 15 | mp.insert({ 5, 50 }); 16 | 17 | // prints the elements 18 | cout << "\nThe map in reverse order is:\n"; 19 | cout << "KEY\tELEMENT\n"; 20 | for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { 21 | cout << itr->first 22 | << '\t' << itr->second << '\n'; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /cpp_14/001_stl_crend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | // initialize container 8 | map mp; 9 | 10 | // insert elements in random order 11 | mp.insert({ 2, 30 }); 12 | mp.insert({ 1, 40 }); 13 | mp.insert({ 3, 60 }); 14 | mp.insert({ 4, 20 }); 15 | mp.insert({ 5, 50 }); 16 | 17 | // prints the elements 18 | cout << "\nThe map in reverse order is:\n"; 19 | cout << "KEY\tELEMENT\n"; 20 | for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { 21 | cout << itr->first 22 | << '\t' << itr->second << '\n'; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /cpp_14/001_stl_end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int ia[] = {0,1,2,3}; 8 | int *beg = begin(ia); 9 | int *last = end(ia); 10 | cout << *beg << endl; 11 | cout << *(last-1) << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_14/001_stl_rbegin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | deque dq = { 'a', 'b', 'c', 'd', 'e' }; 7 | 8 | cout << "The deque in reverse order: "; 9 | 10 | // prints the elements in reverse order 11 | for (auto it = dq.rbegin(); it != dq.rend(); ++it) 12 | cout << *it << " "; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_14/001_stl_rend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | deque dq = { 'a', 'b', 'c', 'd', 'e' }; 7 | 8 | cout << "The deque in reverse order: "; 9 | 10 | // prints the elements in reverse order 11 | for (auto it = dq.rbegin(); it != dq.rend(); ++it) 12 | cout << *it << " "; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_14/001_type_traits_is_final.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | class B final {}; 6 | 7 | int main() 8 | { 9 | std::cout 10 | << std::boolalpha 11 | << std::is_final::value << '\n' 12 | << std::is_final::value << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /cpp_14/001_type_traits_is_null_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void f(int*) 5 | { 6 | std::cout << "Pointer to integer overload\n"; 7 | } 8 | 9 | void f(double*) 10 | { 11 | std::cout << "Pointer to double overload\n"; 12 | } 13 | 14 | void f(std::nullptr_t) 15 | { 16 | std::cout << "null pointer overload\n"; 17 | } 18 | 19 | int main() 20 | { 21 | int* pi {}; double* pd {}; 22 | 23 | f(pi); 24 | f(pd); 25 | f(nullptr); // would be ambiguous without void f(nullptr_t) 26 | // f(0); // ambiguous call: all three functions are candidates 27 | // f(NULL); // ambiguous if NULL is an integral null pointer constant 28 | // (as is the case in most implementations) 29 | } 30 | -------------------------------------------------------------------------------- /cpp_14/001_utility_integer_sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | template 6 | decltype(auto) apply_impl(F f, const T& t, std::index_sequence) 7 | { 8 | return f(std::get(t)...); 9 | } 10 | 11 | template 12 | decltype(auto) apply(F f, const T& t) 13 | { 14 | return apply_impl(f, t, std::make_index_sequence::value>()); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | auto tp = std::make_tuple(1, 'A', 1.2); 21 | 22 | auto ld = [](int arg1, char arg2, double arg3)->int{ 23 | std::cout << "call func(" << arg1 << ", " << arg2 << ", " << arg3 << ")" << std::endl; 24 | return 0; 25 | }; 26 | 27 | int ret= apply(ld, tp); 28 | } 29 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_bool_constant.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_byte.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_conjunction.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_disjunction.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_invoke_result.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_aggregate.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_invocable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_invocable_r.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_nothrow_invocable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_nothrow_invocable_r.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_nothrow_swappable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_nothrow_swappable_with.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_swappable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_is_swappable_with.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_ndisjunctionegation.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/002_type_traits_void_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_as_const.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_in_place.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_in_place_index.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_in_place_index_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_in_place_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_in_place_type.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/003_utility_in_place_type_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/004_tuple_apply.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/004_tuple_make_from_tuple.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_bad_optional_access.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_in_place.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_in_place_index.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_in_place_index_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_in_place_t.cpp1: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_in_place_type.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_in_place_type_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_make_optional.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_nullopt.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_nullopt_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_optional.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_std_hash.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/005_optional_std_swap.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/006_variant_get_if.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/006_variant_holds_alternative.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/006_variant_std_get.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/006_variant_std_swap.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/006_variant_variant.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_14/006_variant_visit.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/001_keywords_README.md: -------------------------------------------------------------------------------- 1 | ####

C++17含义变化或者新增含义关键字(meaning changed or new meaning added)

2 | 3 | #####
register
4 | 5 | register在C++17中已弃用 6 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_byte.cpp: -------------------------------------------------------------------------------- 1 | #include // for std::byte 2 | 3 | std::byte b1{0x3F}; 4 | std::byte b2{0b1111'0000}; 5 | std::byte b4[4] {b1, b2, std::byte{1}}; // 4 bytes (last is 0) 6 | 7 | if (b1 == b4[0]) 8 | { 9 | b1 <<= 1; 10 | } 11 | 12 | std::cout << std::to_integer(b1) << '\n'; // outputs: \T{126} 13 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_conjunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 若所有 Ts... 都拥有等同于 T 的类型,则启用 func 5 | template 6 | std::enable_if_t...>> 7 | func(T, Ts...) { 8 | std::cout << "all types in pack are T\n"; 9 | } 10 | 11 | // 否则 12 | template 13 | std::enable_if_t...>> 14 | func(T, Ts...) { 15 | std::cout << "not all types in pack are T\n"; 16 | } 17 | 18 | int main() { 19 | func(1, 2, 3); 20 | func(1, 2, "hello!"); 21 | } 22 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_disjunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using result0 = 5 | std::disjunction, std::bool_constant, 6 | std::bool_constant>; 7 | using result1 = 8 | std::disjunction, std::bool_constant, 9 | std::bool_constant>; 10 | 11 | int main() 12 | { 13 | std::cout << std::boolalpha; 14 | std::cout << result0::value << '\n'; 15 | std::cout << result1::value << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_aggregate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | template 6 | struct D : std::string, std::complex 7 | { 8 | std::string data; 9 | }; 10 | 11 | int main(void) 12 | { 13 | D s{ {"hello"}, {4.5,6.7}, "world" }; // OK since C++17 14 | const auto result = std::is_aggregate::value; 15 | std::cout << result << std::endl; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_invocable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto func2(char) -> int (*)() 4 | { 5 | return nullptr; 6 | } 7 | 8 | int main() 9 | { 10 | static_assert( std::is_invocable_v ); 11 | static_assert( not std::is_invocable_v ); 12 | static_assert( std::is_invocable_r_v ); 13 | static_assert( not std::is_invocable_r_v ); 14 | static_assert( std::is_invocable_r_v ); 15 | static_assert( not std::is_invocable_r_v ); 16 | static_assert( std::is_invocable_r_v ); 17 | static_assert( not std::is_invocable_r_v ); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_invocable_r.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto func2(char) -> int (*)() 4 | { 5 | return nullptr; 6 | } 7 | 8 | int main() 9 | { 10 | static_assert( std::is_invocable_v ); 11 | static_assert( not std::is_invocable_v ); 12 | static_assert( std::is_invocable_r_v ); 13 | static_assert( not std::is_invocable_r_v ); 14 | static_assert( std::is_invocable_r_v ); 15 | static_assert( not std::is_invocable_r_v ); 16 | static_assert( std::is_invocable_r_v ); 17 | static_assert( not std::is_invocable_r_v ); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_nothrow_invocable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto func2(char) -> int (*)() 4 | { 5 | return nullptr; 6 | } 7 | 8 | int main() 9 | { 10 | static_assert( std::is_invocable_v ); 11 | static_assert( not std::is_invocable_v ); 12 | static_assert( std::is_invocable_r_v ); 13 | static_assert( not std::is_invocable_r_v ); 14 | static_assert( std::is_invocable_r_v ); 15 | static_assert( not std::is_invocable_r_v ); 16 | static_assert( std::is_invocable_r_v ); 17 | static_assert( not std::is_invocable_r_v ); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_nothrow_invocable_r.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto func2(char) -> int (*)() 4 | { 5 | return nullptr; 6 | } 7 | 8 | int main() 9 | { 10 | static_assert( std::is_invocable_v ); 11 | static_assert( not std::is_invocable_v ); 12 | static_assert( std::is_invocable_r_v ); 13 | static_assert( not std::is_invocable_r_v ); 14 | static_assert( std::is_invocable_r_v ); 15 | static_assert( not std::is_invocable_r_v ); 16 | static_assert( std::is_invocable_r_v ); 17 | static_assert( not std::is_invocable_r_v ); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_swappable.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_is_swappable_with.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | requires std::swappable_with 5 | void mySwap(T& t, U& u) 6 | { 7 | T temp = t; t = u; u = temp; 8 | } 9 | 10 | int main() 11 | { 12 | int x, y; 13 | mySwap(x, y); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_ndisjunctionegation.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/002_type_traits_void_t.cpp: -------------------------------------------------------------------------------- 1 | struct NoInnerType 2 | { 3 | int m_i; 4 | }; 5 | 6 | struct HaveInnerType 7 | { 8 | using type = int; //类型别名 9 | void myfunc() {} 10 | }; 11 | 12 | 13 | //泛化版本 14 | template > 15 | struct HasTypeMem : std::false_type //struct 默认是public ,class默认是private继承 16 | { 17 | }; 18 | //特化版本 19 | template 20 | struct HasTypeMem > : std::true_type 21 | { 22 | }; 23 | -------------------------------------------------------------------------------- /cpp_17/003_utility_in_place_index.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/003_utility_in_place_index_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/003_utility_in_place_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/003_utility_in_place_type.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/003_utility_in_place_type_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/004_tuple_make_from_tuple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Foo { 5 | Foo(int first, float second, int third) { 6 | std::cout << first << ", " << second << ", " << third << "\n"; 7 | } 8 | }; 9 | 10 | int main() 11 | { 12 | auto tuple = std::make_tuple(42, 3.14f, 0); 13 | std::make_from_tuple(std::move(tuple)); 14 | } 15 | -------------------------------------------------------------------------------- /cpp_17/005_optional_bad_optional_access.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_in_place.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_in_place_index.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_in_place_index_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_in_place_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_in_place_type.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_in_place_type_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_17/005_optional_make_optional.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | auto op1 = std::make_optional>({'a','b','c'}); 10 | std::cout << "op1: "; 11 | for (char c: op1.value()){ 12 | std::cout << c << ","; 13 | } 14 | auto op2 = std::make_optional>(5, 2); 15 | std::cout << "\nop2: "; 16 | for (int i: *op2){ 17 | std::cout << i << ","; 18 | } 19 | std::string str{"hello world"}; 20 | auto op3 = std::make_optional(std::move(str)); 21 | std::cout << "\nop3: " << quoted(op3.value_or("empty value")) << '\n'; 22 | std::cout << "str: " << std::quoted(str) << '\n'; 23 | } 24 | -------------------------------------------------------------------------------- /cpp_17/005_optional_nullopt.cpp: -------------------------------------------------------------------------------- 1 | std::vector> a; 2 | std::optional>& test(int b) 3 | { 4 | a.clear(); 5 | a.push_back(b); 6 | if(b) 7 | return a; 8 | else 9 | return std::nullopt; 10 | } 11 | -------------------------------------------------------------------------------- /cpp_17/005_optional_nullopt_t.cpp: -------------------------------------------------------------------------------- 1 | struct nullopt_t { 2 | explicit constexpr nullopt_t(int) {} 3 | }; 4 | -------------------------------------------------------------------------------- /cpp_17/005_optional_std_swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int a = 5, b = 3; 7 | 8 | // before 9 | std::cout << a << ' ' << b << '\n'; 10 | 11 | std::swap(a,b); 12 | 13 | // after 14 | std::cout << a << ' ' << b << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_17/006_variant_bad_variant_access.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::variant v; 7 | v = 12; 8 | try { 9 | std::get(v); 10 | } 11 | catch(const std::bad_variant_access& e) { 12 | std::cout << e.what() << '\n'; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /cpp_17/006_variant_get_if.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::variant v{12}; 7 | 8 | if(auto pval = std::get_if(&v)) 9 | std::cout << "variant value: " << *pval << '\n'; 10 | else 11 | std::cout << "failed to get value!" << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_17/006_variant_holds_alternative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | std::variant v = "abc"; 7 | std::cout << std::boolalpha 8 | << "variant holds int? " 9 | << std::holds_alternative(v) << '\n' 10 | << "variant holds string? " 11 | << std::holds_alternative(v) << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_17/006_variant_monostate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct S 6 | { 7 | S(int i) : i(i) {} 8 | int i; 9 | }; 10 | 11 | int main() { 12 | 13 | // Without the monostate type this declaration will fail. 14 | // This is because S is not default-constructible. 15 | 16 | std::variant var; 17 | assert(var.index() == 0); 18 | 19 | try { 20 | std::get(var); // throws! We need to assign a value 21 | } 22 | catch(const std::bad_variant_access& e) { 23 | std::cout << e.what() << '\n'; 24 | } 25 | 26 | var = 12; 27 | 28 | std::cout << std::get(var).i << '\n'; 29 | } 30 | -------------------------------------------------------------------------------- /cpp_17/006_variant_std_get.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::variant v{12}, w; 7 | int i = std::get(v); 8 | w = std::get(v); 9 | w = std::get<0>(v); // same effect as the previous line 10 | 11 | // std::get(v); // error: no double in [int, float] 12 | // std::get<3>(v); // error: valid index values are 0 and 1 13 | 14 | try { 15 | std::get(w); // w contains int, not float: will throw 16 | } 17 | catch (std::bad_variant_access&) {} 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/006_variant_std_swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int a = 5, b = 3; 7 | 8 | // before 9 | std::cout << a << ' ' << b << '\n'; 10 | 11 | std::swap(a,b); 12 | 13 | // after 14 | std::cout << a << ' ' << b << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_17/006_variant_variant_alternative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using my_variant = std::variant; 5 | static_assert(std::is_same_v 6 | >); 7 | static_assert(std::is_same_v 8 | >); 9 | // cv-qualification on the variant type propagates to the extracted alternative type. 10 | static_assert(std::is_same_v 11 | >); 12 | 13 | int main() 14 | { 15 | std::cout << "All static assertions passed.\n"; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/006_variant_variant_alternative_t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using my_variant = std::variant; 5 | static_assert(std::is_same_v 6 | >); 7 | static_assert(std::is_same_v 8 | >); 9 | // cv-qualification on the variant type propagates to the extracted alternative type. 10 | static_assert(std::is_same_v 11 | >); 12 | 13 | int main() 14 | { 15 | std::cout << "All static assertions passed.\n"; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/006_variant_variant_npos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct Demon 7 | { 8 | Demon(int) {} 9 | Demon(const Demon&) { throw std::domain_error("copy ctor"); } 10 | Demon& operator= (const Demon&) = default; 11 | }; 12 | 13 | int main() 14 | { 15 | std::variant var{42}; 16 | std::cout 17 | << std::boolalpha 18 | << "index == npos: " << (var.index() == std::variant_npos) << '\n'; 19 | 20 | try { var = Demon{666}; } catch (const std::domain_error& ex) 21 | { 22 | std::cout 23 | << "Exception: " << ex.what() << '\n' 24 | << "index == npos: " << (var.index() == std::variant_npos) << '\n' 25 | << "valueless: " << var.valueless_by_exception() << '\n'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /cpp_17/006_variant_variant_size_v.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static_assert(std::variant_size_v> == 0); 6 | static_assert(std::variant_size_v> == 1); 7 | static_assert(std::variant_size_v> == 2); 8 | static_assert(std::variant_size_v> == 3); 9 | static_assert(std::variant_size_v> == 3); 10 | static_assert(std::variant_size_v> == 2); 11 | static_assert(std::variant_size_v> == 2); 12 | static_assert(std::variant_size_v>> == 1); 13 | 14 | int main() { 15 | std::puts("All static assertions passed."); 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/006_variant_visit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template struct overloaded : Ts... { using Ts::operator()...; }; 9 | template overloaded(Ts...)->overloaded; 10 | 11 | int main() { 12 | std::vector> vec = { 10, 15l, 1.5, "hello" }; 13 | for (auto& v : vec) { 14 | std::visit(overloaded{ 15 | [](auto arg) { std::cout << arg << ' '; }, 16 | [](double arg) { std::cout << std::fixed << arg << ' '; }, 17 | [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, 18 | }, v); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cpp_17/007_any_bad_any_cast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Foo { virtual ~Foo() {} }; 5 | struct Bar { virtual ~Bar() {} }; 6 | 7 | int main() 8 | { 9 | Bar b; 10 | try { 11 | Foo& f = dynamic_cast(b); 12 | } catch(const std::bad_cast& e) 13 | { 14 | std::cout << e.what() << '\n'; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/007_any_std_swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int a = 5, b = 3; 7 | 8 | // before 9 | std::cout << a << ' ' << b << '\n'; 10 | 11 | std::swap(a,b); 12 | 13 | // after 14 | std::cout << a << ' ' << b << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_17/008_charconv_chars_format.cpp: -------------------------------------------------------------------------------- 1 | enum class chars_format { 2 | scientific = /*unspecified*/, 3 | fixed = /*unspecified*/, 4 | hex = /*unspecified*/, 5 | general = fixed | scientific 6 | }; 7 | -------------------------------------------------------------------------------- /cpp_17/008_charconv_from_chars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::array str{"42 xyz "}; 9 | int result; 10 | if(auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result); 11 | ec == std::errc()) 12 | std::cout << result << "\n" "p -> \"" << p << "\"\n"; 13 | 14 | std::string_view sv{"24 abc "}; 15 | if(auto [p, ec] = std::from_chars(data(sv), data(sv)+size(sv), result); 16 | ec == std::errc()) 17 | std::cout << result << "\n" "p -> \"" << p << "\"\n"; 18 | } 19 | -------------------------------------------------------------------------------- /cpp_17/008_charconv_to_chars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | std::array str; 10 | 11 | if(auto [ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), 42); 12 | ec == std::errc()) 13 | std::cout << std::string_view 14 | (str.data(), ptr); // C++20, uses string_view(first, last) 15 | // (str.data(), ptr - str.data()); // C++17, uses string_view(ptr, length) 16 | } 17 | -------------------------------------------------------------------------------- /cpp_17/009_initializer_list_data.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::string s {"Hello world!\n"}; 8 | 9 | char a[20]; // storage for a C-style string 10 | std::strcpy(a, std::data(s)); 11 | // [s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11 12 | 13 | std::cout << a; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_17/009_initializer_list_empty.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | void print(const T& container) 6 | { 7 | if ( std::empty(container) ) 8 | { 9 | std::cout << "Empty\n"; 10 | } 11 | else 12 | { 13 | std::cout << "Elements:"; 14 | for ( const auto& element : container ) 15 | std::cout << ' ' << element; 16 | std::cout << '\n'; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | std::vector c = { 1, 2, 3 }; 23 | print(c); 24 | c.clear(); 25 | print(c); 26 | 27 | int array[] = { 4, 5, 6 }; 28 | print(array); 29 | 30 | auto il = { 7, 8, 9 }; 31 | print(il); 32 | } 33 | -------------------------------------------------------------------------------- /cpp_17/010_map_extract.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | int main() 7 | { 8 | using namespace std::literals; 9 | std::unordered_map m; 10 | 11 | m.try_emplace("a", "a"s); 12 | m.try_emplace("b", "abcd"); 13 | m.try_emplace("c", 10, 'c'); 14 | m.try_emplace("c", "Won't be inserted"); 15 | 16 | for (const auto &p : m) { 17 | std::cout << p.first << " => " << p.second << '\n'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cpp_17/010_map_insert_or_assign.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | auto print_node = [](const auto &node) { 6 | std::cout << "[" << node.first << "] = " << node.second << '\n'; 7 | }; 8 | 9 | auto print_result = [](auto const &pair) { 10 | std::cout << (pair.second ? "inserted: " : "assigned: "); 11 | print_node(*pair.first); 12 | }; 13 | 14 | int main() 15 | { 16 | std::map myMap; 17 | 18 | print_result( myMap.insert_or_assign("a", "apple" ) ); 19 | print_result( myMap.insert_or_assign("b", "banana" ) ); 20 | print_result( myMap.insert_or_assign("c", "cherry" ) ); 21 | print_result( myMap.insert_or_assign("c", "clementine") ); 22 | 23 | for (const auto &node : myMap) { print_node(node); } 24 | } 25 | -------------------------------------------------------------------------------- /cpp_17/010_map_merge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::map ma {{1, "apple"}, {5, "pear"}, {10, "banana"}}; 8 | std::map mb {{2, "zorro"}, {4, "batman"}, {5, "X"}, {8, "alpaca"}}; 9 | std::map u; 10 | u.merge(ma); 11 | std::cout << "ma.size(): " << ma.size() << '\n'; 12 | u.merge(mb); 13 | std::cout << "mb.size(): " << mb.size() << '\n'; 14 | std::cout << "mb.at(5): " << mb.at(5) << '\n'; 15 | for(auto const &kv: u) 16 | std::cout << kv.first << ", " << kv.second << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_17/010_map_try_emplace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | int main() 7 | { 8 | using namespace std::literals; 9 | std::map m; 10 | 11 | m.try_emplace("a", "a"s); 12 | m.try_emplace("b", "abcd"); 13 | m.try_emplace("c", 10, 'c'); 14 | m.try_emplace("c", "Won't be inserted"); 15 | 16 | for (const auto &p : m) { 17 | std::cout << p.first << " => " << p.second << '\n'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cpp_17/011_unordered_map_insert_or_assign.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | auto print_node = [](const auto &node) { 6 | std::cout << "[" << node.first << "] = " << node.second << '\n'; 7 | }; 8 | 9 | auto print_result = [](auto const &pair) { 10 | std::cout << (pair.second ? "inserted: " : "assigned: "); 11 | print_node(*pair.first); 12 | }; 13 | 14 | int main() 15 | { 16 | std::unordered_map myMap; 17 | 18 | print_result( myMap.insert_or_assign("a", "apple" ) ); 19 | print_result( myMap.insert_or_assign("b", "banana" ) ); 20 | print_result( myMap.insert_or_assign("c", "cherry" ) ); 21 | print_result( myMap.insert_or_assign("c", "clementine") ); 22 | 23 | for (const auto &node : myMap) { print_node(node); } 24 | } 25 | -------------------------------------------------------------------------------- /cpp_17/011_unordered_map_try_emplace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | int main() 7 | { 8 | using namespace std::literals; 9 | std::unordered_map m; 10 | 11 | m.try_emplace("a", "a"s); 12 | m.try_emplace("b", "abcd"); 13 | m.try_emplace("c", 10, 'c'); 14 | m.try_emplace("c", "Won't be inserted"); 15 | 16 | for (const auto &p : m) { 17 | std::cout << p.first << " => " << p.second << '\n'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cpp_17/012_stl_data.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::string s {"Hello world!\n"}; 8 | 9 | char a[20]; // storage for a C-style string 10 | std::strcpy(a, std::data(s)); 11 | // [s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11 12 | 13 | std::cout << a; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_17/012_stl_empty.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | void print(const T& container) 6 | { 7 | if ( std::empty(container) ) 8 | { 9 | std::cout << "Empty\n"; 10 | } 11 | else 12 | { 13 | std::cout << "Elements:"; 14 | for ( const auto& element : container ) 15 | std::cout << ' ' << element; 16 | std::cout << '\n'; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | std::vector c = { 1, 2, 3 }; 23 | print(c); 24 | c.clear(); 25 | print(c); 26 | 27 | int array[] = { 4, 5, 6 }; 28 | print(array); 29 | 30 | auto il = { 7, 8, 9 }; 31 | print(il); 32 | } 33 | -------------------------------------------------------------------------------- /cpp_17/012_stl_size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v = { 3, 1, 4 }; 8 | std::cout << std::size(v) << '\n'; 9 | 10 | int a[] = { -5, 10, 15 }; 11 | std::cout << std::size(a) << '\n'; 12 | 13 | // since C++20 the signed size (ssize) can avail 14 | auto i = std::ssize(v); 15 | for (--i; i != -1; --i) { 16 | std::cout << v[i] << (i ? ' ' : '\n'); 17 | } 18 | std::cout << "i = " << i << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_17/README.md: -------------------------------------------------------------------------------- 1 | ##

C++17新特性

2 | 3 | ![C++17思维导图](https://www.0voice.com/uiwebsite/cpp_new_features/C++17_new_features.png) 4 | ----------- 5 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_basic_common_reference.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_common_reference.cpp: -------------------------------------------------------------------------------- 1 | using CR = std::iter_common_reference_t>; 2 | std::ranges::sort(r, std::less{}); 3 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_is_bounded_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_bounded_array_v
<< '\n'; 10 | std::cout << std::is_bounded_array_v << '\n'; 11 | std::cout << std::is_bounded_array_v << '\n'; 12 | std::cout << std::is_bounded_array_v << '\n'; 13 | std::cout << std::is_bounded_array_v << '\n'; 14 | std::cout << std::is_bounded_array_v << '\n'; 15 | std::cout << std::is_bounded_array_v << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_is_constant_evaluated.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int foo(int arg) { 4 | if (std::is_constant_evaluated()) { 5 | return 1; 6 | } else { 7 | return 0; 8 | } 9 | } 10 | 11 | int main() { 12 | const auto b = foo(0); 13 | return b; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_is_corresponding_member.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Foo { int x; }; 5 | struct Bar { int y; double z; }; 6 | 7 | struct Baz : Foo, Bar {}; // not standard-layout 8 | 9 | int main() 10 | { 11 | std::cout << std::boolalpha 12 | << std::is_same_v << '\n' 13 | << std::is_same_v << '\n' 14 | << std::is_corresponding_member(&Foo::x, &Bar::y) << '\n' 15 | << std::is_corresponding_member(&Baz::x, &Baz::y) << '\n' 16 | << std::is_corresponding_member(&Baz::x, &Baz::y) << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_is_pointer_interconvertible_with_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | int m; 6 | }; 7 | 8 | struct B { 9 | B(B const&) {} 10 | }; 11 | 12 | struct C { 13 | virtual void foo(); 14 | }; 15 | 16 | struct D { 17 | int m; 18 | 19 | D(D const&) = default; // -> trivially copyable 20 | D(int x): m(x+1) {} 21 | }; 22 | 23 | int main() 24 | { 25 | std::cout << std::boolalpha; 26 | std::cout << std::is_trivially_copyable::value << '\n'; 27 | std::cout << std::is_trivially_copyable::value << '\n'; 28 | std::cout << std::is_trivially_copyable::value << '\n'; 29 | std::cout << std::is_trivially_copyable::value << '\n'; 30 | } 31 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_is_unbounded_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_unbounded_array_v << '\n'; 10 | std::cout << std::is_unbounded_array_v << '\n'; 11 | std::cout << std::is_unbounded_array_v << '\n'; 12 | std::cout << std::is_unbounded_array_v << '\n'; 13 | std::cout << std::is_unbounded_array_v << '\n'; 14 | std::cout << std::is_unbounded_array_v << '\n'; 15 | std::cout << std::is_unbounded_array_v << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_remove_cvref.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::boolalpha 7 | << std::is_same_v, int> << '\n' 8 | << std::is_same_v, int> << '\n' 9 | << std::is_same_v, int> << '\n' 10 | << std::is_same_v, int> << '\n' 11 | << std::is_same_v, int[2]> << '\n' 12 | << std::is_same_v, int[2]> << '\n' 13 | << std::is_same_v, int(int)> << '\n'; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_20/002_rtti_type_identity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | T foo(T a, T b) { 6 | return a + b; 7 | } 8 | 9 | template 10 | T bar(T a, std::type_identity_t b) { 11 | return a + b; 12 | } 13 | 14 | int main() { 15 | // foo(4.2, 1); // error, deduced conflicting types for 'T' 16 | std::cout << bar(4.2, 1) << '\n'; // OK, calls bar 17 | } 18 | -------------------------------------------------------------------------------- /cpp_20/003_rtti_suspend_always.cpp: -------------------------------------------------------------------------------- 1 | struct promise_type { 2 | int current_value; 3 | static wrapper get_return_object_on_allocation_failure() noexcept { return wrapper{nullptr}; } 4 | wrapper get_return_object() { return wrapper{handle::from_promise(*this)}; } 5 | auto initial_suspend() { return std::suspend_always{}; } 6 | auto final_suspend() noexcept { return std::suspend_always{}; } 7 | void unhandled_exception() { std::terminate(); } 8 | void return_void() {} 9 | auto yield_value(int value) { 10 | current_value = value; 11 | return std::suspend_always{}; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_common_comparison_category.cpp: -------------------------------------------------------------------------------- 1 | namespace detail { 2 | 3 | template 4 | struct common_cmpcat_base { using type = void; }; 5 | template<> 6 | struct common_cmpcat_base<0u> { using type = std::strong_ordering; }; 7 | template<> 8 | struct common_cmpcat_base<2u> { using type = std::partial_ordering; }; 9 | template<> 10 | struct common_cmpcat_base<4u> { using type = std::weak_ordering; }; 11 | template<> 12 | struct common_cmpcat_base<6u> { using type = std::partial_ordering; }; 13 | 14 | } // namespace detail 15 | 16 | template 17 | struct common_comparison_category : 18 | detail::common_cmpcat_base<(0u | ... | 19 | (std::is_same_v ? 0u : 20 | std::is_same_v ? 4u : 21 | std::is_same_v ? 2u : 1u) 22 | )> {}; 23 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_compare_three_way.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Rational_2 { 5 | int num; 6 | int den; // > 0 7 | }; 8 | 9 | constexpr std::weak_ordering operator<=>(Rational_2 lhs, Rational_2 rhs) 10 | { 11 | return lhs.num * rhs.den <=> rhs.num * lhs.den; 12 | } 13 | 14 | void print(std::weak_ordering value) 15 | { 16 | if (value == 0) 17 | std::cout << "equal\n"; 18 | else if (value < 0) 19 | std::cout << "less\n"; 20 | else 21 | std::cout << "greater\n"; 22 | } 23 | 24 | int main() 25 | { 26 | Rational_2 c{6,5}; 27 | Rational_2 d{8,7}; 28 | print(c <=> d); 29 | print(std::compare_three_way{}(c,d)); 30 | } 31 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_compare_three_way_result.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | template 6 | void print_cmp_type() 7 | { 8 | if constexpr (std::is_same_v) 9 | std::cout << "strong ordering\n"; 10 | else if constexpr (std::is_same_v) 11 | std::cout << "weak ordering\n"; 12 | else if constexpr (std::is_same_v) 13 | std::cout << "partial ordering\n"; 14 | else 15 | std::cout << "illegal comparison result type\n"; 16 | } 17 | 18 | int main() 19 | { 20 | print_cmp_type>(); 21 | print_cmp_type>(); 22 | } 23 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_is_eq.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_is_gt.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_is_gteq.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_is_lt.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_is_lteq.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_is_neq.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_partial_order.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_partial_ordering.cpp: -------------------------------------------------------------------------------- 1 | void test_compare() 2 | { 3 | struct ts { 4 | int id; 5 | float f; 6 | std::partial_ordering operator<=>(const ts& that) const 7 | { 8 | if(id<0 || that.id<0) return std::partial_ordering::unordered; 9 | if(f>that.f) return std::partial_ordering::greater; 10 | if(fthat)==0; 16 | } 17 | }; 18 | 19 | ts ta = {2, 1.0f}; 20 | ts tb={3, 1.0f}; 21 | std::cout<< (ta==tb) <tb) < 2 | int main() 3 | { 4 | std::strong_ordering so = 55 <=> 10; 5 | 6 | so < 0; // Fine 7 | so < 1; // Fails 8 | } 9 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_three_way_comparable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Rational_2 { 5 | int num; 6 | int den; // > 0 7 | }; 8 | 9 | constexpr std::weak_ordering operator<=>(Rational_2 lhs, Rational_2 rhs) 10 | { 11 | return lhs.num * rhs.den <=> rhs.num * lhs.den; 12 | } 13 | 14 | void print(std::weak_ordering value) 15 | { 16 | if (value == 0) 17 | std::cout << "equal\n"; 18 | else if (value < 0) 19 | std::cout << "less\n"; 20 | else 21 | std::cout << "greater\n"; 22 | } 23 | 24 | int main() 25 | { 26 | Rational_2 c{6,5}; 27 | Rational_2 d{8,7}; 28 | print(c <=> d); 29 | print(std::compare_three_way{}(c,d)); 30 | } 31 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_three_way_comparable_with.cpp: -------------------------------------------------------------------------------- 1 | struct compare_three_way { 2 | template 3 | requires three_way_comparable_with<_Ty1, _Ty2> // TRANSITION, GH-489 4 | constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const 5 | noexcept(noexcept(_STD forward<_Ty1>(_Left) <=> _STD forward<_Ty2>(_Right))) /* strengthened */ { 6 | return _STD forward<_Ty1>(_Left) <=> _STD forward<_Ty2>(_Right); 7 | } 8 | 9 | using is_transparent = int; 10 | }; 11 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_weak_order.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/004_rtti_compare_weak_ordering.cpp: -------------------------------------------------------------------------------- 1 | // any type that is weakly ordered 2 | struct Weak { 3 | bool operator==(Weak const&) const; 4 | std::weak_ordering operator<=>(Weak const&) const; 5 | }; 6 | 7 | struct Foo { 8 | Weak w; 9 | int i; 10 | auto operator<=>(Foo const&) const = default; 11 | }; 12 | -------------------------------------------------------------------------------- /cpp_20/005_rtti_concepts_ranges_swap.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | vector v; 11 | 12 | srand(time(0)); 13 | for(int i=0;i<10;i++) 14 | { 15 | v.push_back(i+1); 16 | } 17 | 18 | //使用了lambda表达式 19 | for_each(v.begin(),v.end(),[](int n){cout< 7 | struct std::formatter { 8 | using _Pc = std::basic_format_parse_context<_CharT>; 9 | typename _Pc::iterator parse(_Pc& _ParseCtx) 10 | { // 此函数用来解析字符串 11 | // 相关代码逻辑可参考 https://zh.cppreference.com/w/cpp/utility/format/formatter 12 | // 以及查看源码 format 文件的 _Formatter_base 类 13 | // C++内置类型的特化formatter均继承自此基类 14 | // 本人也在啃此源码中,但最近没时间,打算留待假期 15 | } 16 | 17 | template 18 | typename _FormatContext::iterator format(const Box& v, _FormatContext& format_context) 19 | { 20 | // 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cpp_20/007_format_format.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 类型 T 的包装 5 | template 6 | struct Box { 7 | T value; 8 | }; 9 | 10 | // 能用被包装值的格式说明格式化包装 Box 11 | template 12 | struct std::formatter, CharT> : std::formatter { 13 | // 从基类继承 parse() 14 | 15 | // 通过以被包装值调用基类实现定义 format() 16 | template 17 | auto format(Box t, FormatContext& fc) { 18 | return std::formatter::format(t.value, fc); 19 | } 20 | }; 21 | 22 | int main() { 23 | Box v = { 42 }; 24 | std::cout << std::format("{:#x}", v); 25 | } 26 | -------------------------------------------------------------------------------- /cpp_20/007_format_format_args.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_format_context.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_format_error.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_format_parse_context.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_format_to_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | char buffer[64]; 8 | 9 | const auto result = 10 | std::format_to_n(buffer, std::size(buffer), 11 | "Hubble's H{0} {1} {2} km/sec/mpc.", 12 | "\u2080", "\u2245", 71); 13 | 14 | std::cout << "Buffer: \"" << std::string_view{buffer, result.size} << "\"\n" 15 | << "Buffer size = " << std::size(buffer) << '\n' 16 | << "Untruncated output size = " << result.size << '\n'; 17 | } 18 | -------------------------------------------------------------------------------- /cpp_20/007_format_formatter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 类型 T 的包装 5 | template 6 | struct Box { 7 | T value; 8 | }; 9 | 10 | // 能用被包装值的格式说明格式化包装 Box 11 | template 12 | struct std::formatter, CharT> : std::formatter { 13 | // 从基类继承 parse() 14 | 15 | // 通过以被包装值调用基类实现定义 format() 16 | template 17 | auto format(Box t, FormatContext& fc) { 18 | return std::formatter::format(t.value, fc); 19 | } 20 | }; 21 | 22 | int main() { 23 | Box v = { 42 }; 24 | std::cout << std::format("{:#x}", v); 25 | } 26 | -------------------------------------------------------------------------------- /cpp_20/007_format_make_format_args.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_make_wformat_args.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_vformat.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_vformat_to.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_visit_format_arg.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_wformat_args.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_wformat_context.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/007_format_wformat_parse_context.cpp: -------------------------------------------------------------------------------- 1 | 正在整理中... 2 | -------------------------------------------------------------------------------- /cpp_20/008_memory_destroy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Tracer { 6 | int value; 7 | ~Tracer() { std::cout << value << " destructed\n"; } 8 | }; 9 | 10 | int main() 11 | { 12 | alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8]; 13 | 14 | for (int i = 0; i < 8; ++i) 15 | new(buffer + sizeof(Tracer) * i) Tracer{i}; // 手工构造对象 16 | 17 | auto ptr = std::launder(reinterpret_cast(buffer)); 18 | 19 | std::destroy(ptr, ptr + 8); 20 | } 21 | -------------------------------------------------------------------------------- /cpp_20/008_memory_destroy_at.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Tracer { 6 | int value; 7 | ~Tracer() { std::cout << value << " destructed\n"; } 8 | }; 9 | 10 | int main() 11 | { 12 | alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8]; 13 | 14 | for (int i = 0; i < 8; ++i) 15 | new(buffer + sizeof(Tracer) * i) Tracer{i}; // 手工构造对象 16 | 17 | auto ptr = std::launder(reinterpret_cast(buffer)); 18 | 19 | std::destroy(ptr, ptr + 8); 20 | } 21 | -------------------------------------------------------------------------------- /cpp_20/008_memory_destroy_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Tracer { 6 | int value; 7 | ~Tracer() { std::cout << value << " destructed\n"; } 8 | }; 9 | 10 | int main() 11 | { 12 | alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8]; 13 | 14 | for (int i = 0; i < 8; ++i) 15 | new(buffer + sizeof(Tracer) * i) Tracer{i}; //manually construct objects 16 | 17 | auto ptr = std::launder(reinterpret_cast(buffer)); 18 | 19 | std::ranges::destroy_n(ptr, 8); 20 | } 21 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_get_default_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_memory_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_monotonic_buffer_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_new_delete_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_null_memory_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_polymorphic_allocator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_pool_options.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_set_default_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_synchronized_pool_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/009_memory_resource_unsynchronized_pool_resource.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_assignable_from.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | class A 5 | { 6 | public: 7 | const A& operator= (const A& other) { return *this; } 8 | }; 9 | 10 | int main () 11 | { 12 | A d1, d2; d1 = d2; //this works fine 13 | 14 | std::cout << std::boolalpha 15 | << "Is double assignable? " << std::assignable_from << '\n' //Says true, as it should 16 | << "Is A assignable? " << std::assignable_from&, A> << '\n'; //Says false 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_boolean_testable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_common_reference_with.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_common_with.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_convertible_to.cpp: -------------------------------------------------------------------------------- 1 | struct From; 2 | struct To { 3 | explicit To(From) = delete; 4 | }; 5 | struct From { 6 | operator To(); 7 | }; 8 | 9 | static_assert(std::is_convertible_v); 10 | static_assert(not std::convertible_to); 11 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_copy_constructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Ex1 { 5 | std::string str; // member has a non-trivial copy ctor 6 | }; 7 | struct Ex2 { 8 | int n; 9 | Ex2(const Ex2&) = default; // trivial and non-throwing 10 | }; 11 | 12 | int main() { 13 | std::cout << std::boolalpha << "Ex1 is copy-constructible? " 14 | << std::is_copy_constructible::value << '\n' 15 | << "Ex1 is trivially copy-constructible? " 16 | << std::is_trivially_copy_constructible::value << '\n' 17 | << "Ex2 is trivially copy-constructible? " 18 | << std::is_trivially_copy_constructible::value << '\n' 19 | << "Ex2 is nothrow copy-constructible? " 20 | << std::is_nothrow_copy_constructible::value << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_copyable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_destructible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Foo { 5 | std::string str; 6 | ~Foo() noexcept {}; 7 | }; 8 | struct Bar { 9 | ~Bar() = default; 10 | }; 11 | int main() { 12 | std::cout << std::boolalpha 13 | << "std::string is destructible? " 14 | << std::is_destructible::value << '\n' 15 | << "Foo is trivially destructible? " 16 | << std::is_trivially_destructible_v << '\n' 17 | << "Foo is nothrow destructible? " 18 | << std::is_nothrow_destructible() << '\n' 19 | << "Bar is trivially destructible? " 20 | << std::is_trivially_destructible{} << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_equality_comparable.cpp: -------------------------------------------------------------------------------- 1 | class strong_int { 2 | public: 3 | using integral_type = int; 4 | 5 | explicit strong_int( integral_type ) noexcept; 6 | 7 | explicit operator integral_type() const noexcept; 8 | 9 | constexpr auto operator<=>( const strong_int& ) const noexcept; 10 | 11 | private: 12 | integral_type m_value; 13 | }; 14 | 15 | constexpr auto operator<=>( const strong_int&, const int& ) noexcept; 16 | constexpr auto operator<=>( const int&, const strong_int& ) noexcept; 17 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_equality_comparable_with.cpp: -------------------------------------------------------------------------------- 1 | class strong_int { 2 | public: 3 | using integral_type = int; 4 | 5 | explicit strong_int( integral_type ) noexcept; 6 | 7 | explicit operator integral_type() const noexcept; 8 | 9 | constexpr auto operator<=>( const strong_int& ) const noexcept; 10 | 11 | private: 12 | integral_type m_value; 13 | }; 14 | 15 | constexpr auto operator<=>( const strong_int&, const int& ) noexcept; 16 | constexpr auto operator<=>( const int&, const strong_int& ) noexcept; 17 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_equivalence_relation.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_floating_point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | int main() 7 | { 8 | std::cout << std::boolalpha; 9 | std::cout << std::is_floating_point::value << '\n'; 10 | std::cout << std::is_floating_point::value << '\n'; 11 | std::cout << std::is_floating_point::value << '\n'; 12 | std::cout << std::is_floating_point::value << '\n'; 13 | std::cout << std::is_floating_point::value << '\n'; 14 | std::cout << std::is_floating_point::value << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_integral.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void print(std::integral auto i) { 5 | std::cout << "Integral: " << i << '\n'; 6 | } 7 | 8 | void print(auto x) { 9 | std::cout << "Non-integral: " << x << '\n'; 10 | } 11 | 12 | int main() 13 | { 14 | print( 'o' ); static_assert( std::integral ); 15 | print( 007 ); static_assert( std::integral ); 16 | print( 2e2 ); static_assert( !std::integral ); 17 | print("∫∫∫"); static_assert( !std::integral ); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_invocable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto func2(char) -> int (*)() 4 | { 5 | return nullptr; 6 | } 7 | 8 | int main() 9 | { 10 | static_assert( std::is_invocable_v ); 11 | static_assert( not std::is_invocable_v ); 12 | static_assert( std::is_invocable_r_v ); 13 | static_assert( not std::is_invocable_r_v ); 14 | static_assert( std::is_invocable_r_v ); 15 | static_assert( not std::is_invocable_r_v ); 16 | static_assert( std::is_invocable_r_v ); 17 | static_assert( not std::is_invocable_r_v ); 18 | } 19 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_movable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_predicate.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_regular.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_regular_invocable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_relation.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_same_as.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | concept IsAnyOf = (std::same_as || ...); 6 | 7 | template 8 | concept IsPrintable = std::integral || std::floating_point || 9 | IsAnyOf>>, char, wchar_t>; 10 | 11 | void println(IsPrintable auto const ... arguments) 12 | { 13 | (std::wcout << ... << arguments) << '\n'; 14 | } 15 | 16 | int main() { println("Example: ", 3.14, " : ", 42, " : [", 'a', L'-', L"Z]"); } 17 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_semiregular.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | struct Single { 6 | T value; 7 | }; 8 | 9 | int main() 10 | { 11 | Single myInt1{4}; 12 | Single myInt2; 13 | myInt2 = myInt1; 14 | 15 | std::cout << myInt1.value << ' ' << myInt2.value << '\n'; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_signed_integral.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void print(std::signed_integral auto i) { 5 | std::cout << "Signed integral: " << i << '\n'; 6 | } 7 | 8 | void print(std::unsigned_integral auto u) { 9 | std::cout << "Unsigned integral: " << u << '\n'; 10 | } 11 | 12 | void print(auto x) { 13 | std::cout << "Non-integral: " << x << '\n'; 14 | } 15 | 16 | int main() { 17 | print(42); // signed 18 | print(0xFull); // unsigned 19 | print(true); // unsigned 20 | print('A'); // platform-dependent 21 | print(4e-2); // non-integral (hex-float) 22 | print("∫∫∫"); // non-integral 23 | } 24 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_strict_weak_order.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_swappable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_swappable_with.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | requires std::swappable_with 5 | void mySwap(T& t, U& u) 6 | { 7 | T temp = t; t = u; u = temp; 8 | } 9 | 10 | int main() 11 | { 12 | int x, y; 13 | mySwap(x, y); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_totally_ordered.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_totally_ordered_with.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/010_concepts_unsigned_integral.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | template 4 | concept gcdint = std::unsigned_integral && !std::is_same_v, bool>; 5 | 6 | template 7 | constexpr std::common_type_t gcd(T1 a, T2 b) 8 | { 9 | return b ? gcd(b, a % b) : a; 10 | } 11 | int main() 12 | { 13 | std::cout << gcd(37u, 666u) << std::endl; 14 | std::cout << gcd(0u, false) << std::endl; 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /cpp_20/011_memory_assume_aligned.cpp: -------------------------------------------------------------------------------- 1 | void f(int* p) { 2 | int* p1 = std::assume_aligned<256>(p); 3 | // 用 p1 而非 p ,以确保从对齐假设受益。 4 | // 然而,若 p 未对齐则程序有未定义行为,无关乎是否使用 p1 。 5 | } 6 | -------------------------------------------------------------------------------- /cpp_20/011_memory_construct_at.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_make_obj_using_allocator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_no-throw-forward-iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_no-throw-forward-range.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_no-throw-input-iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_no-throw-input-range.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_no-throw-sentinel-for.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_ranges_construct_at.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct S { 5 | int x; 6 | float y; 7 | double z; 8 | 9 | S(int x, float y, double z) : x{x}, y{y}, z{z} { std::cout << "S::S();\n"; } 10 | 11 | ~S() { std::cout << "S::~S();\n"; } 12 | 13 | void print() const { 14 | std::cout << "S { x=" << x << "; y=" << y << "; z=" << z << "; };\n"; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | alignas(S) unsigned char buf[sizeof(S)]; 21 | 22 | S* ptr = std::ranges::construct_at(reinterpret_cast(buf), 42, 2.71828f, 3.1415); 23 | ptr->print(); 24 | 25 | std::ranges::destroy_at(ptr); 26 | } 27 | -------------------------------------------------------------------------------- /cpp_20/011_memory_ranges_destroy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Tracer { 6 | int value; 7 | ~Tracer() { std::cout << value << " destructed\n"; } 8 | }; 9 | 10 | int main() 11 | { 12 | alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8]; 13 | 14 | for (int i = 0; i < 8; ++i) 15 | new(buffer + sizeof(Tracer) * i) Tracer{i}; //manually construct objects 16 | 17 | auto ptr = std::launder(reinterpret_cast(buffer)); 18 | 19 | std::ranges::destroy(ptr, ptr + 8); 20 | } 21 | -------------------------------------------------------------------------------- /cpp_20/011_memory_ranges_destroy_at.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct S { 5 | int x; 6 | float y; 7 | double z; 8 | 9 | S(int x, float y, double z) : x{x}, y{y}, z{z} { std::cout << "S::S();\n"; } 10 | 11 | ~S() { std::cout << "S::~S();\n"; } 12 | 13 | void print() const { 14 | std::cout << "S { x=" << x << "; y=" << y << "; z=" << z << "; };\n"; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | alignas(S) unsigned char buf[sizeof(S)]; 21 | 22 | S* ptr = std::ranges::construct_at(reinterpret_cast(buf), 42, 2.71828f, 3.1415); 23 | ptr->print(); 24 | 25 | std::ranges::destroy_at(ptr); 26 | } 27 | -------------------------------------------------------------------------------- /cpp_20/011_memory_ranges_destroy_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Tracer { 6 | int value; 7 | ~Tracer() { std::cout << value << " destructed\n"; } 8 | }; 9 | 10 | int main() 11 | { 12 | alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8]; 13 | 14 | for (int i = 0; i < 8; ++i) 15 | new(buffer + sizeof(Tracer) * i) Tracer{i}; //manually construct objects 16 | 17 | auto ptr = std::launder(reinterpret_cast(buffer)); 18 | 19 | std::ranges::destroy_n(ptr, 8); 20 | } 21 | -------------------------------------------------------------------------------- /cpp_20/011_memory_ranges_uninitialized_fill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | constexpr int n {4}; 8 | alignas(alignof(std::string)) char out[n * sizeof(std::string)]; 9 | 10 | try 11 | { 12 | auto first {reinterpret_cast(out)}; 13 | auto last {first + n}; 14 | std::ranges::uninitialized_fill(first, last, "▄▀▄▀▄▀▄▀"); 15 | 16 | int count {1}; 17 | for (auto it {first}; it != last; ++it) { 18 | std::cout << count++ << ' ' << *it << '\n'; 19 | } 20 | 21 | std::ranges::destroy(first, last); 22 | } 23 | catch(...) 24 | { 25 | std::cout << "Exception!\n"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /cpp_20/011_memory_ranges_uninitialized_fill_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | constexpr int n {3}; 8 | alignas(alignof(std::string)) char out[n * sizeof(std::string)]; 9 | 10 | try 11 | { 12 | auto first {reinterpret_cast(out)}; 13 | auto last = std::ranges::uninitialized_fill_n(first, n, "cppreference"); 14 | 15 | for (auto it {first}; it != last; ++it) { 16 | std::cout << *it << '\n'; 17 | } 18 | 19 | std::ranges::destroy(first, last); 20 | } 21 | catch(...) 22 | { 23 | std::cout << "Exception!\n"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cpp_20/011_memory_uninitialized_construct_using_allocator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/011_memory_uses_allocator_construction_args.cpp: -------------------------------------------------------------------------------- 1 | return std::uses_allocator_construction_args(alloc, 2 | std::piecewise_construct, std::tuple<>{}, std::tuple<>{} 3 | ); 4 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_ambiguous_local_time.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_choose.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_clock_cast.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_clock_time_conversion.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_current_zone.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_day.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_file_clock.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_get_tzdb.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_get_tzdb_list.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_gps_clock.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_is_am.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_is_clock.cpp: -------------------------------------------------------------------------------- 1 | 2 | template 3 | struct is_clock : std::false_type {}; 4 | 5 | template 6 | requires 7 | requires { 8 | typename T::rep; 9 | typename T::period; 10 | typename T::duration; 11 | typename T::time_point; 12 | T::is_steady; 13 | T::now(); 14 | } 15 | struct is_clock : std::true_type {}; 16 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_is_clock_v.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_is_pm.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_last_spec.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_leap_second.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_local_info.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_local_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_locate_zone.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_make12.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_make24.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_month.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_month_day.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_month_day_last.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_month_weekday.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_month_weekday_last.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_nonexistent_local_time.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_operator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_parse.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_reload_tzdb.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_remote_version.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_sys_info.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_tai_clock.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_time_of_day.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_time_zone.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_time_zone_link.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_tzdb.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_tzdb_list.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_utc_clock.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_weekday.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_weekday_indexed.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_weekday_last.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_year.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_year_month.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_year_month_day.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_year_month_day_last.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_year_month_weekday.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_year_month_weekday_last.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_zoned_time.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/012_chrono_zoned_traits.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/013_string_ends_with.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int startsWith(string s, string sub){ 6 | return s.find(sub)==0?1:0; 7 | } 8 | 9 | int endsWith(string s,string sub){ 10 | return s.rfind(sub)==(s.length()-sub.length())?1:0; 11 | } 12 | 13 | int main(){ 14 | string str = "helloWorld"; 15 | string preStr = "he"; 16 | string sufStr = "rld"; 17 | if(startsWith(str,preStr)){ 18 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int startsWith(string s, string sub){ 6 | return s.find(sub)==0?1:0; 7 | } 8 | 9 | int endsWith(string s,string sub){ 10 | return s.rfind(sub)==(s.length()-sub.length())?1:0; 11 | } 12 | 13 | int main(){ 14 | string str = "helloWorld"; 15 | string preStr = "he"; 16 | string sufStr = "rld"; 17 | if(startsWith(str,preStr)){ 18 | cout< 2 | std::array to_array(const V& v) 3 | { 4 | assert(v.size() == N); 5 | std::array d; 6 | std::copy(v.begin(), v.end(), d.data()); 7 | return d; 8 | } 9 | -------------------------------------------------------------------------------- /cpp_20/017_vector_erase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Function to demo erase() 6 | void eraseDemo(string str) 7 | { 8 | // Deletes all characters 9 | str.erase(); 10 | 11 | cout << "After erase():"; 12 | cout << str; 13 | } 14 | 15 | // Driver code 16 | int main() 17 | { 18 | string str("Hello World!"); 19 | 20 | cout << "Before erase():"; 21 | cout << str << endl; 22 | eraseDemo(str); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /cpp_20/017_vector_erase_if.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | inline Os& operator<<(Os& os, Container const& cont) 6 | { 7 | os << "{"; 8 | for (const auto& item : cont) { 9 | os << "{" << item.first << ", " << item.second << "}"; 10 | } 11 | return os << "}"; 12 | } 13 | 14 | int main() 15 | { 16 | std::map data {{1, 'a'},{2, 'b'},{3, 'c'},{4, 'd'}, 17 | {5, 'e'},{4, 'f'},{5, 'g'},{5, 'g'}}; 18 | std::cout << "Original:\n" << data << '\n'; 19 | 20 | const auto count = std::erase_if(data, [](const auto& item) { 21 | auto const& [key, value] = item; 22 | return (key & 1) == 1; 23 | }); 24 | 25 | std::cout << "Erase items with odd keys:\n" << data << '\n' 26 | << count << " items removed.\n"; 27 | } 28 | -------------------------------------------------------------------------------- /cpp_20/018_map_contains.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::set example = {1, 2, 3, 4}; 7 | 8 | if (example.contains(2)) { 9 | std::cout << "Found\n"; 10 | } else { 11 | std::cout << "Not found\n"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /cpp_20/018_map_erase_if.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/019_unordered_map_contains.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/019_unordered_map_erase_if.cpp: -------------------------------------------------------------------------------- 1 | auto old_size = c.size(); 2 | for (auto i = c.begin(), last = c.end(); i != last; ) { 3 | if (pred(*i)) { 4 | i = c.erase(i); 5 | } else { 6 | ++i; 7 | } 8 | } 9 | return old_size - c.size(); 10 | -------------------------------------------------------------------------------- /cpp_20/020_span_back.cpp: -------------------------------------------------------------------------------- 1 | #include // std::cout 2 | #include // std::back_inserter 3 | #include // std::vector 4 | #include // std::copy 5 | 6 | int main () { 7 | std::vector foo,bar; 8 | for (int i=1; i<=5; i++) 9 | { foo.push_back(i); bar.push_back(i*10); } 10 | 11 | std::copy (bar.begin(),bar.end(),back_inserter(foo)); 12 | 13 | std::cout << "foo contains:"; 14 | for ( std::vector::iterator it = foo.begin(); it!= foo.end(); ++it ) 15 | std::cout << ' ' << *it; 16 | std::cout << '\n'; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /cpp_20/020_span_begin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v = { 3, 1, 4 }; 8 | auto vi = std::begin(v); 9 | std::cout << *vi << '\n'; 10 | 11 | int a[] = { -5, 10, 15 }; 12 | auto ai = std::begin(a); 13 | std::cout << *ai << '\n'; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_20/020_span_end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::vector v = { 3, 1, 4 }; 9 | if (std::find(std::begin(v), std::end(v), 5) != std::end(v)) { 10 | std::cout << "found a 5 in vector v!\n"; 11 | } 12 | 13 | int a[] = { 5, 10, 15 }; 14 | if (std::find(std::begin(a), std::end(a), 5) != std::end(a)) { 15 | std::cout << "found a 5 in array a!\n"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /cpp_20/020_span_front.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int main() 7 | { 8 | std::vector v{1,2,3,4,5}; 9 | std::deque d; 10 | std::copy(v.begin(), v.end(), 11 | std::front_insert_iterator>(d)); // or std::front_inserter(d) 12 | for(int n : d) 13 | std::cout << n << ' '; 14 | std::cout << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /cpp_20/020_span_rbegin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v = { 3, 1, 4 }; 8 | auto vi = std::rbegin(v); 9 | std::cout << *vi << '\n'; 10 | 11 | int a[] = { -5, 10, 15 }; 12 | auto ai = std::rbegin(a); 13 | std::cout << *ai << '\n'; 14 | } 15 | -------------------------------------------------------------------------------- /cpp_20/020_span_rend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int a[] = {4, 6, -3, 9, 10}; 9 | std::cout << "Array backwards: "; 10 | std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator(std::cout, " ")); 11 | 12 | std::cout << "\nVector backwards: "; 13 | std::vector v = {4, 6, -3, 9, 10}; 14 | std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator(std::cout, " ")); 15 | } 16 | -------------------------------------------------------------------------------- /cpp_20/021_std_bidirectional_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_bidirectional_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_contiguous_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_contiguous_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_forward_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_forward_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_incrementable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_indirectly_readable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_indirectly_readable_traits.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_indirectly_writable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_input_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_input_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_input_or_output_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_iter_common_reference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_iter_difference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_iter_rvalue_reference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_iter_value_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_iterator_traits.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_output_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_output_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_random_access_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_random_access_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_sentinel_for.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_sized_sentinel_for.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/021_std_weakly_incrementable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/022_ranges_iter_move.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/022_ranges_iter_swap.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_bidirectional_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_bidirectional_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_contiguous_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_contiguous_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_forward_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_forward_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_incrementable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_incrementable_traits.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_indirectly_readable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_indirectly_readable_traits.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_indirectly_writable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_input_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_input_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_input_or_output_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_iter_common_reference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_iter_difference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_iter_reference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_iter_rvalue_reference_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_iter_value_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_iterator_traits.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_output_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_output_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_random_access_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_random_access_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_sentinel_for.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_sized_sentinel_for.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/023_std_weakly_incrementable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_common_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_counted_iterator.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_default_sentinel_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirect_binary_predicate.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirect_equivalence_relation.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirect_result_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirect_strict_weak_order.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirect_unary_predicate.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_comparable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_copyable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_copyable_storable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_movable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_movable_storable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_regular_unary_invocable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_swappable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_indirectly_unary_invocable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_mergeable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_move_sentinel.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_permutable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_projected.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_sortable.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/024_iterator_unreachable_sentinel_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/025_iterator_ranges_advanc.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/025_iterator_ranges_distance.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/025_iterator_ranges_next.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/025_iterator_ranges_prev.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/026_std_ssize.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_begin.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_cbegin.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_cdata.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_cend.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_crbegin.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_crend.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_data.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_empty.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_end.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_rbegin.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_rend.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_size.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/027_ranges_ranges_ssize.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_20/README.md: -------------------------------------------------------------------------------- 1 | ##

C++20新特性

2 | 3 | C++20思维导图正在更新中... 4 |
5 | 敬请期待! 6 | 7 | 10 | ----------- 11 | -------------------------------------------------------------------------------- /cpp_23/001_rtti_is_scoped_enum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class A {}; 5 | 6 | enum E {}; 7 | 8 | enum struct Es { oz }; 9 | 10 | enum class Ec : int {}; 11 | 12 | int main() 13 | { 14 | std::cout << std::boolalpha; 15 | std::cout << std::is_scoped_enum_v
<< '\n'; 16 | std::cout << std::is_scoped_enum_v << '\n'; 17 | std::cout << std::is_scoped_enum_v << '\n'; 18 | std::cout << std::is_scoped_enum_v << '\n'; 19 | std::cout << std::is_scoped_enum_v << '\n'; 20 | } 21 | -------------------------------------------------------------------------------- /cpp_23/003_stacktrace_basic_stacktrace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << std::boolalpha; 7 | std::stacktrace bktr; 8 | std::cout << "Initially, bktr.empty(): " << bktr.empty() << '\n'; 9 | 10 | bktr = std::stacktrace::current(); 11 | std::cout << "After getting entries, bktr.empty(): " << bktr.empty() << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /cpp_23/004_memory_allocate_at_least.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/004_memory_allocation_result.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/004_memory_inout_ptr.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/004_memory_inout_ptr_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/004_memory_out_ptr.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/004_memory_out_ptr_t.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/005_string_contains.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/005_string_view_contains.cpp: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cpp_23/README.md: -------------------------------------------------------------------------------- 1 | ##

C++23新特性

2 | 3 | C++23思维导图正在更新中... 4 |
5 | 敬请期待! 6 | 7 | 10 | ----------- 11 | --------------------------------------------------------------------------------