ရိုးရိုးလေး ရှင်းပြမယ်
Template နဲ့ concept ကို ရိုးရိုးလေး ပြောရရင်
int အတွက် max function တစ်ခု ရေး၊ double အတွက် ထပ်ရေး၊ ပြီးတော့ string အတွက် ထပ်ရေးတယ်လို့ စဉ်းစားကြည့်ပါ။ Body တွေက အတူတူပါပဲ၊ type ပဲ ပြောင်းပါတယ်။ Template က type placeholder တစ်ခုနဲ့ body ကို တစ်ခါတည်း ရေးခွင့်ပေးပြီး သင် သုံးလိုက်တဲ့အခါ compiler က int၊ double၊ string version တွေကို သင့်ကိုယ်စား ရေးပေးပါတယ်။
Concept ကတော့ အဲဒီ placeholder ပေါ်က label ပါ: 'integer type ဘယ်ဟာမဆို' ဒါမှမဟုတ် 'print ထုတ်လို့ရတဲ့ ဘယ်ဟာမဆို'။ Template ရဲ့ နှုတ်ဖြင့် မပြောထားတဲ့ ယူဆချက်တွေကို စစ်ဆေးတဲ့ စည်းမျဉ်းအဖြစ် ပြောင်းပေးတဲ့အတွက် type မှားရင် code အတွင်းပိုင်း နက်နက်မှာ fail မဖြစ်ဘဲ တံခါးဝမှာတင် message တိုတိုနဲ့ ငြင်းပယ်ခံရပါတယ်။
Problem → Why → Solution
ပြဿနာ: error က မှားတဲ့နေရာကို ညွှန်နေတယ်
Number တစ်ခုကို နှစ်ဆ လုပ်ပေးတဲ့ template တစ်ခု ရေးပြီး teammate တစ်ယောက်က std::string နဲ့ ခေါ်လိုက်ပါတယ်။ Compiler က call ကို လက်ခံပြီးမှ template body ကို string အတွက် compile လုပ်တဲ့အခါကျမှ ပြဿနာကို တွေ့ပါတယ်။ GCC က error ကို body ထဲက line 6 မှာ ပြပြီး ဘယ်က ခေါ်လဲဆိုတာကို နောက်က "required from here" note မှာမှ ပြောပါတယ်။
#include <iostream>
#include <string>
template <typename T>
T twice(T value) {
return value * 2; // error: no match for 'operator*' (operand types are
} // 'std::__cxx11::basic_string<char>' and 'int')
int main() {
std::string word = "hi";
std::cout << twice(word) << '\n'; // the real mistake is here
}ဘာကြောင့် ဒီလို error ထွက်လာတာလဲ
Constraint မရှိတဲ့ template က call မှာ type တိုင်းကို လက်ခံပြီး အဲဒီ type အတွက် body ကို compile လုပ်မှသာ ပြဿနာကို တွေ့ပါတယ်။ ဒါကြောင့် error က တကယ့် အမှား (string ပေးလိုက်တာ) အကြောင်း မပြောဘဲ template ရဲ့ အတွင်းပိုင်း (twice ထဲက operator*) အကြောင်း ပြောနေပါတယ်။ std::sort လို library အနက်ပိုင်းမှာဆိုရင် ဒီလို fail တစ်ခုက "required from" note ရှည်ကြီး တစ်တန်း ထွက်လာစေနိုင်ပါတယ်။ Concept ကတော့ စစ်ဆေးမှုကို ရှေ့တံခါးဝဆီ ရွှေ့ပေးပါတယ်။
Core concept: compile time မှာ type အလိုက် ထုတ်ပေးတဲ့ code
template <typename T> T largest(T a, T b) လို function template တစ်ခုကို ခေါ်တိုင်း compiler က argument တွေကနေ T ကို ဆုံးဖြတ်ပါတယ် (template argument deduction)။ ပြီးတော့ T မတူတိုင်း function သီးသန့် တစ်ခုစီကို instantiate လုပ်ပါတယ်။ ဒါက compile time မှာ ဖြစ်တာမို့ runtime ကုန်ကျစရိတ် မရှိပေမဲ့ type တစ်ခုစီအတွက် code တစ်စုံစီ ထွက်လာပါတယ်။ largest(3, 4.5) လို argument နှစ်ခုက T ကို မတူတဲ့ type နှစ်ခု ပေးနေရင် deduction fail ဖြစ်လို့ largest<double>(3, 4.5) လို type ကို ကိုယ်တိုင် ပေးရပါတယ်။
Class တွေလည်း template ဖြစ်နိုင်ပါတယ်: std::vector<T> က တစ်ခုပါ။ template <typename T> class Counter { ... }; လို ရေးပြီး Counter<double> လို သုံးပါတယ်။ C++17 ကစပြီး class template argument deduction (CTAD) က std::vector v{1, 2, 3} ကို vector<int> လို့ ကိုယ်တိုင် ဆုံးဖြတ်ပေးပါတယ်။ Template ကို သုံးတဲ့နေရာမှာ instantiate လုပ်ရတဲ့အတွက် compiler က အဲဒီနေရာမှာ definition အပြည့်အစုံကို မြင်ရပါမယ်၊ ဒါကြောင့် template တွေကို header ထဲမှာ ရေးပါတယ်။
C++20 concept တစ်ခုက type တွေအပေါ် compile time မှာ စစ်တဲ့ နာမည်ပေးထားတဲ့ predicate ပါ။ <concepts> ထဲမှာ std::integral၊ std::floating_point၊ std::same_as၊ std::convertible_to၊ std::totally_ordered စတာတွေ ပါပါတယ်။ ကိုယ်ပိုင် concept ကို requires expression နဲ့ ရေးပါတယ်: compile ဖြစ်ရမယ့် expression တွေကို စာရင်းပြုစုတာပါ၊ ဘာမှ run မလုပ်ပါဘူး။ Constraint ကို body မတိုင်ခင် ခေါ်တဲ့နေရာမှာ စစ်ပြီး မပြည့်မီရင် အဲဒီ overload ကို ဖယ်ထုတ်ကာ ဘယ် constraint fail ဖြစ်လဲ error က ပြောပါတယ်။
| ရေးနည်း | ဥပမာ |
|---|---|
| Constrained parameter | template <std::integral T> T twice(T v) |
| requires clause | template <typename T> requires std::integral<T> |
| Abbreviated template | auto twice(std::integral auto v) |
Constraint မတူတဲ့ overload တွေထဲက တစ်ခုထက်ပိုပြီး ကိုက်ညီရင် compiler က ပိုတင်းကျပ်တဲ့ constraint ရှိတဲ့ overload ကို ရွေးပါတယ်၊ ဒါကြောင့် generic version တစ်ခုနဲ့ အထူးပြု version တစ်ခုကို ဘေးချင်းယှဉ် ထားလို့ ရပါတယ်။ Concept တွေက syntax ကိုပဲ စစ်တာ မှတ်ထားပါ: a < b compile ဖြစ်တယ်ဆိုတာက အဲဒါ သင်မျှော်လင့်တဲ့ အဓိပ္ပာယ် ရှိတယ်လို့ မဆိုလိုပါဘူး။ Body ကိုလည်း type အစစ်နဲ့ compile လုပ်ဆဲမို့ concept က body သုံးတဲ့ operation တိုင်းကို ဖုံးလွှမ်းထားရပါမယ်။
- Body ကို placeholder T နဲ့ တစ်ခါ ရေး
- ခေါ်တဲ့နေရာ: compiler က T ကို deduce
- Body မတိုင်ခင် concept က T ကို စစ်
- Compiler က အဲဒီ T အတွက် code ထုတ်
T မတူတိုင်း compile လုပ်ထားတဲ့ copy တစ်ခုစီ ရတဲ့အတွက် template ရဲ့ ကုန်ကျစရိတ်က run time မဟုတ်ဘဲ compile time နဲ့ binary size ပါ။
အထွေထွေ ပုံစံ
template <typename T>
T name(T a, T b) { /* ... */ } // any T the body works for
template <std::integral T> // constrained by a standard concept
T name(T value) { /* ... */ }
template <typename T>
concept ConceptName = requires(T a) { // your own concept
a.method(); // these expressions must compile
};
template <typename T> class Name { T member; }; // class templatetemplate <...> က type parameter တွေကို ကြေညာပါတယ်။ typename ကို concept နာမည်နဲ့ အစားထိုးရင် constraint ဖြစ်သွားပြီး ကိုယ်ပိုင် concept ကိုတော့ requires expression နဲ့ ရေးပါတယ်။
Worked example: standard concept တစ်ခုနဲ့ ကိုယ်ပိုင် concept တစ်ခု
twice က integer type တွေကိုပဲ လက်ခံပြီး printAll ကတော့ << နဲ့ print ထုတ်လို့ရတဲ့ element type ရှိတဲ့ vector တိုင်းကို လက်ခံပါတယ်။ main ထဲက comment တွေက ခေါ်တိုင်း T ဘာဖြစ်လဲ ပြပါတယ်။ နောက်ဆုံး line ရဲ့ comment ကို ဖြုတ်ကြည့်ပါ၊ compiler က double ကို ခေါ်တဲ့နေရာမှာတင် ငြင်းပယ်ပါလိမ့်မယ်။
#include <concepts>
#include <iostream>
#include <string>
#include <vector>
template <std::integral T> // T must be an integer type
T twice(T value) {
return value * 2;
}
template <typename T>
concept Printable = requires(std::ostream& os, const T& value) {
os << value; // this expression must compile
};
template <Printable T>
void printAll(const std::vector<T>& items) {
for (const T& item : items) {
std::cout << '[' << item << ']';
}
std::cout << '\n';
}
int main() {
std::cout << twice(21) << '\n'; // T = int
std::cout << twice(3'000'000'000LL) << '\n'; // T = long long
printAll(std::vector<int>{1, 2, 3}); // T = int
printAll(std::vector<std::string>{"tea", "rice"}); // T = std::string
// twice(2.5); // error: double does not satisfy std::integral
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A constrained type parameter
template <std::integral T>typename အစား std::integral ကို ရေးထားလို့ T က int၊ long long၊ char လို integer type ဖြစ်မှ ဒီ template ကို သုံးခွင့်ရပါတယ်။
- Defining a concept
concept Printable = requires(std::ostream& os, const T& value) {requires expression က os နဲ့ value ဆိုတဲ့ စမ်းသပ်ဖို့ parameter တွေကို ကြေညာပြီး body ထဲက os << value; က compile ဖြစ်ရမယ်လို့ ပြောပါတယ်။ ဒီ code ကို ဘယ်တော့မှ run မလုပ်ပါဘူး၊ compiler က စစ်ရုံပါ။
- Using your own concept
template <Printable T>Standard concept လိုပဲ ကိုယ်ပိုင် concept ကို typename ရဲ့ နေရာမှာ ထည့်ပါတယ်။ vector<int> နဲ့ vector<std::string> နှစ်ခုလုံး ကိုက်ညီတဲ့အတွက် main က printAll ကို နှစ်ခါ ခေါ်နိုင်ပါတယ်။
- Deduction picks long long
twice(3'000'000'000LL)' က C++14 ကစပြီး digit separator ပါ၊ LL က literal ကို long long ဖြစ်စေတဲ့အတွက် T = long long ဖြစ်ပြီး int ထဲ မဆံ့တဲ့ 6000000000 ကို မှန်မှန်ကန်ကန် ပြန်ပေးပါတယ်။
- The call that would fail
// twice(2.5); // error: double does not satisfy std::integralComment ဖြုတ်ရင် GCC က "no matching function for call to 'twice(double)'"၊ ပြီးရင် "constraints not satisfied" နဲ့ "the expression 'is_integral_v<_Tp> [with _Tp = double]' evaluated to 'false'" လို့ ပြောပါတယ်။ Error က main ထဲက call line ကို တိုက်ရိုက် ညွှန်ပါတယ်။
42
6000000000
[1][2][3]
[tea][rice]ရေးနည်း သုံးမျိုး၊ overload နဲ့ class template
အောက်က program က constraint ရေးနည်း သုံးမျိုးလုံးကို ပြပါတယ်။ describe ရဲ့ overload နှစ်ခုက တူညီတဲ့ နာမည်ရှိပေမဲ့ constraint မတူလို့ compiler က argument ရဲ့ type နဲ့ ကိုက်ညီတဲ့ တစ်ခုတည်းကို ရွေးပါတယ်။ Counter ကတော့ type parameter ယူတဲ့ class ပါ။
#include <concepts>
#include <iostream>
template <typename T>
requires std::floating_point<T> // requires clause
T half(T value) {
return value / 2;
}
auto square(std::integral auto value) { // abbreviated template
return value * value;
}
void describe(std::integral auto) { std::cout << "integer\n"; }
void describe(std::floating_point auto) { std::cout << "floating point\n"; }
template <typename T>
class Counter { // class template
public:
void add(T amount) { total_ += amount; ++count_; }
T total() const { return total_; }
int count() const { return count_; }
private:
T total_{};
int count_ = 0;
};
int main() {
std::cout << half(5.0) << '\n';
std::cout << square(7) << '\n';
describe(3);
describe(2.5f);
Counter<double> distance; // T = double, chosen explicitly
distance.add(1.5);
distance.add(2.25);
std::cout << distance.count() << " runs, " << distance.total() << " km\n";
}Output က 2.5၊ 49၊ integer၊ floating point နဲ့ 2 runs, 3.75 km ပါ။ T total_{} မှာ {} က T ဘာပဲဖြစ်ဖြစ် value-initialize လုပ်ပေးလို့ number type တွေအတွက် 0 ဖြစ်ပါတယ်။ half(5) ကို ခေါ်ရင် int က floating_point မဟုတ်လို့ compile မဖြစ်ပါဘူး၊ half(5.0) ဒါမှမဟုတ် half<double>(5) လို့ ရေးရပါမယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ template နဲ့ concept
Template တစ်ခု မရေးရသေးခင်ကတည်းက နေ့တိုင်း သုံးနေပြီးသားပါ: std::vector<T>၊ std::map<K, V>၊ std::unique_ptr<T> နဲ့ algorithm အားလုံးက template တွေပါ။ C++20 std::ranges algorithm တွေကို std::sortable လို concept တွေနဲ့ ကန့်သတ်ထားလို့ std::list ကို std::ranges::sort နဲ့ sort လုပ်ဖို့ ကြိုးစားရင် error က ဘယ် requirement မပြည့်မီလဲ ပြောပါတယ်။ ကိုယ်ပိုင် code မှာတော့ အောက်ပါနေရာတွေမှာ အကျိုးရှိပါတယ်:
- Container နဲ့ utility function တွေ: ring buffer၊ cache၊ statistics helper တွေကို type အမျိုးမျိုးအတွက် တစ်ကြိမ်ပဲ ရေးပြီး concept က element type ဆီက လိုအပ်ချက်ကို မှတ်တမ်းတင်ပေးပါတယ်။
- Library API တွေ: template <Serializable T> void save(const T&) လို signature က ဘယ် type တွေ သုံးလို့ရလဲကို documentation ဖတ်စရာမလိုဘဲ code ကိုယ်တိုင်က ပြောပါတယ်။
- Strong type တွေ: Money<Currency> လို class template က USD နဲ့ EUR ကို မတော်တဆ ပေါင်းမိတာကို runtime မှာ မဟုတ်ဘဲ compile time မှာ တားဆီးပေးပါတယ်။
အမှားများ
Template နဲ့ concept မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Template definition ကို .cpp file ထဲ ထည့်ခြင်း
math.h မှာ declaration ပဲ ထားပြီး body ကို math.cpp ထဲ ရေးရင် main.cpp က compile ဖြစ်ပေမဲ့ linker က "undefined reference to `int twice<int>(int)'" လို့ ပြောပါတယ်။ math.cpp က twice ကို int နဲ့ ဘယ်တော့မှ မသုံးလို့ အဲဒီ version ကို ဘယ်သူမှ instantiate မလုပ်ခဲ့ပါဘူး။ Template တစ်ခုလုံးကို header ထဲ ရေးပါ။
std::integral က bool နဲ့ char ကိုပါ လက်ခံတာ မေ့ခြင်း
twice(true) က compile ဖြစ်ပြီး true ပြန်ပေးကာ -Wall က "'*' in boolean context, suggest '&&' instead" လို့ သတိပေးပါတယ်။ signed char 100 ကို နှစ်ဆလုပ်ရင် -56 ရပါတယ်။ bool ကို ပယ်ချင်ရင် std::integral<T> && !std::same_as<T, bool> လို concept ကို တင်းကျပ်ပါ။
Body သုံးတာကို concept က မဖုံးလွှမ်းခြင်း
Printable ကိုပဲ တောင်းပြီး body ထဲမှာ a < b ကို သုံးရင် << ရှိပေမဲ့ < မရှိတဲ့ type အတွက် "no match for 'operator<'" error က body ထဲကနေ ပြန်ထွက်လာပါတယ်။ Body သုံးတဲ့ operation တိုင်းကို concept ထဲ ထည့်ပါ၊ ဥပမာ Printable<T> && std::totally_ordered<T>။
Argument type မတူတာကို deduction က ဖြေရှင်းပေးမယ် ထင်ခြင်း
largest(3, 4.5) က "deduced conflicting types for parameter 'T' ('int' and 'double')" နဲ့ fail ဖြစ်ပါတယ်။ Deduction က conversion မလုပ်ပါဘူး။ largest<double>(3, 4.5) လို type ကို ပေးပါ၊ ဒါမှမဟုတ် argument ကို ကိုယ်တိုင် ပြောင်းပါ။
Best Practices
Best practices
- Template တစ်ခုက type တိုင်းနဲ့ အလုပ်မလုပ်ရင် constraint ကို အမြဲ ထည့်ပါ၊ typename တစ်လုံးတည်းက 'ဘာမဆို' လို့ ကတိပေးနေတာပါ။
- ကိုယ်ပိုင် concept မရေးခင် <concepts> ထဲက standard concept တွေကို အရင် ရှာပါ၊ နာမည်ကြီးတဲ့ စည်းမျဉ်းကို ဖတ်တဲ့သူတိုင်း သိပြီးသားပါ။
- Concept နာမည်ကို Sortable၊ Printable လို စွမ်းရည်တစ်ခုအဖြစ် ပေးပါ၊ HasLessThan လို syntax တစ်ခုချင်းစီ နာမည်တွေက ဖတ်ရခက်ပါတယ်။
- Type ပေါ် မူတည်ပြီး ပြောင်းလဲတဲ့ အစိတ်အပိုင်းကိုပဲ template လုပ်ပါ၊ ကျန်တဲ့ logic ကို ရိုးရိုး function ထဲ ထားရင် compile time နဲ့ binary size သက်သာပါတယ်။
Mini challenge
Mini challenge: Number concept နဲ့ generic helper နှစ်ခု
Integer နဲ့ floating-point type တွေကို လက်ခံပြီး bool ကို ငြင်းတဲ့ Number concept တစ်ခု ရေးပါ။ ပြီးရင် Number vector တစ်ခုကို ပေါင်းပေးတဲ့ sumAll နဲ့ < နဲ့ နှိုင်းယှဉ်လို့ရတဲ့ type တိုင်းအတွက် အကြီးဆုံးကို ရှာပေးတဲ့ largest ကို ရေးပါ။ Vector အလွတ်အတွက် largest က 'value မရှိ' လို့ ပြန်ပြောနိုင်ရပါမယ်။
- Number ကို std::integral၊ std::floating_point နဲ့ std::same_as တို့နဲ့ တည်ဆောက်ရမယ်
- sumAll ကို Number နဲ့ ကန့်သတ်ပြီး total ကို T{} ကနေ စရမယ်
- largest ကို std::totally_ordered နဲ့ ကန့်သတ်ပြီး std::optional<T> ပြန်ပေးရမယ်
- int၊ double နဲ့ std::string vector တွေနဲ့ ခေါ်ပြီး compile မဖြစ်သင့်တဲ့ call တစ်ခုကို comment အဖြစ် ထည့်ရမယ်
#include <concepts>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
// TODO: concept Number = integer or floating point, but not bool
// TODO: constrain T with Number
template <typename T>
T sumAll(const std::vector<T>& values) {
// TODO: start from T{} and add every value
}
// TODO: constrain T with std::totally_ordered
template <typename T>
std::optional<T> largest(const std::vector<T>& values) {
// TODO: return std::nullopt for an empty vector, otherwise the biggest value
}
int main() {
// TODO: test sumAll with ints and doubles, largest with ints and strings
// TODO: show that largest of an empty vector has no value
}ရလဒ်: steps: 18800၊ prices: 7.75၊ largest step: 8100၊ largest name: Su (string တွေကို alphabetical နှိုင်းယှဉ်လို့) နဲ့ empty has value: false ကို print ထုတ်ပါတယ်။ sumAll(names) ကို comment ဖြုတ်ရင် constraint error ရပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Concept ကို (std::integral<T> || std::floating_point<T>) && !std::same_as<T, bool> လို ရေးနိုင်ပါတယ်။ std::optional<T> က value တစ်ခု ရှိနိုင် မရှိနိုင်တဲ့ box ပါ: std::nullopt က 'မရှိ' ဖြစ်ပြီး has_value() နဲ့ value() က ခေါ်တဲ့သူဘက်က စစ်ပြီး ထုတ်ယူပါတယ်။
အဖြေ ကြည့်ရန်
#include <concepts>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
template <typename T>
concept Number = (std::integral<T> || std::floating_point<T>) && !std::same_as<T, bool>;
template <Number T>
T sumAll(const std::vector<T>& values) {
T total{}; // 0 for every number type
for (const T& v : values) total += v;
return total;
}
template <std::totally_ordered T>
std::optional<T> largest(const std::vector<T>& values) {
if (values.empty()) return std::nullopt; // no largest in an empty list
T best = values.front();
for (const T& v : values) {
if (best < v) best = v;
}
return best;
}
int main() {
std::vector<int> steps{4200, 8100, 6500};
std::vector<double> prices{2.5, 4.25, 1.0};
std::vector<std::string> names{"Su", "Aye", "Ko"};
std::cout << "steps: " << sumAll(steps) << '\n';
std::cout << "prices: " << sumAll(prices) << '\n';
std::cout << "largest step: " << largest(steps).value() << '\n';
std::cout << "largest name: " << largest(names).value() << '\n';
std::cout << "empty has value: " << std::boolalpha
<< largest(std::vector<int>{}).has_value() << '\n';
// sumAll(names); // error: std::string does not satisfy Number
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Template က body ကို တစ်ခါ ရေးခွင့်ပေးပြီး compiler က သုံးတဲ့ T တစ်ခုချင်းအတွက် code ကို compile time မှာ ထုတ်ပေးပါတယ်။
- T ကို argument တွေကနေ deduce လုပ်ပါတယ်၊ type မတူတာတွေကို conversion နဲ့ ဖြေရှင်းမပေးလို့ လိုရင် largest<double>(...) လို ကိုယ်တိုင် ပေးပါ။
- Concept က ခေါ်တဲ့နေရာမှာ type ကို စစ်ပြီး ဘယ် requirement fail ဖြစ်လဲ ပြောတဲ့ error တိုတို ပေးပါတယ်။
- requires expression က compile ဖြစ်ရမယ့် expression တွေကိုပဲ စစ်ပါတယ်၊ body သုံးတဲ့ operation တိုင်းကို ဖုံးလွှမ်းပါ။
- Template definition တွေကို header ထဲမှာ ရေးပါ၊ မဟုတ်ရင် linker က undefined reference လို့ ပြောပါလိမ့်မယ်။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation