Thuta Learning
C++
AdvancedProgrammingbeginner

Templates & Concepts

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Templates & Concepts ရဲ့type, lifetime နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Warnings နဲ့sanitizers သုံးပြီးfailure cases စမ်းနိုင်ရန်
  • Modern, safe C++ code ရေးနိုင်ရန်

Templates & Concepts မှာcontainer ownership, iterator/range validity, algorithm preconditions နဲ့complexity ကိုတစ်ပြိုင်နက်စဉ်းစားပါ။ Hand-written loops များအစားstandard algorithms/ranges သုံးပြီးmutation နဲ့allocation policy ကိုရှင်းလင်းပါ။

ပိုပြီးနားလည်ထားရမယ့် အချက်

Templates & Concepts မှာcontainer ownership, iterator/range validity, algorithm preconditions နဲ့complexity ကိုတစ်ပြိုင်နက်စဉ်းစားပါ။ Hand-written loops များအစားstandard algorithms/ranges သုံးပြီးmutation နဲ့allocation policy ကိုရှင်းလင်းပါ။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

Templates & Concepts example ကိုempty, boundary, invalid နဲ့failure cases ဖြင့်စမ်းပြီး `-std=c++23 -Wall -Wextra -Wpedantic` နဲ့build ပါ။ Lifetime-sensitive code ကိုAddressSanitizer/UndefinedBehaviorSanitizer ဖြင့်ထပ်စစ်ပါ။

ဒီသင်ခန်းစာပြီးရင်

cpp
#include <concepts>
#include <iostream>

template <std::integral T>
T twice(T value) { return value * 2; }

int main() { std::cout << twice(21) << '\n'; }
You should see
42

ကိုယ်တိုင်စမ်းကြည့်ရန်

Templates & Concepts example ကိုempty, boundary, invalid နဲ့failure cases ဖြင့်စမ်းပြီး `-std=c++23 -Wall -Wextra -Wpedantic` နဲ့build ပါ။ Lifetime-sensitive code ကိုAddressSanitizer/UndefinedBehaviorSanitizer ဖြင့်ထပ်စစ်ပါ။

Memory/Safety သတိပေးချက်

Output တစ်ကြိမ်မှန်ရုံဖြင့် lifetime, bounds, ownership နဲ့undefined behavior အားလုံးမှန်ပြီဟုယူဆခြင်း။

The Current ISO C++ Standard (C++23)Standard C++ Foundation

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Output တစ်ကြိမ်မှန်ရုံဖြင့် lifetime, bounds, ownership နဲ့undefined behavior အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Raw pointer သို့iterator တစ်ခုကိုowner container/resource ပြောင်းပြီးနောက်လည်းvalid ဖြစ်နေမယ်ဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Templates & Concepts example ကိုempty, boundary, invalid နဲ့failure cases ဖြင့်စမ်းပြီး `-std=c++23 -Wall -Wextra -Wpedantic` နဲ့build ပါ။ Lifetime-sensitive code ကိုAddressSanitizer/UndefinedBehaviorSanitizer ဖြင့်ထပ်စစ်ပါ။

You'll know it worked when: 42

Templates & Concepts | Thuta Learning