5.2.18 · HinglishC++ Programming

Concepts (C++20) — constraining templates

1,868 words8 min readRead in English

5.2.18 · Coding › C++ Programming


Concepts kyun exist karte hain?


Concept KYA hota hai?


Inhe kaise likho aur apply karo? (4 syntaxes, same meaning)


Concepts scratch se banana: requires expression

Do alag requires hote hain. Inhe confuse mat karo.

{ } ke andar chaar requirement kinds:

Kind Example Matlab
simple a + b; expression valid hai
type typename T::value_type; nested type exist karta hai
compound { *p } -> std::convertible_to<int>; valid aur type constraint
nested requires Numeric<T>; doosra constraint hold karta hai
Figure — Concepts (C++20) — constraining templates

Concepts par overloading (killer feature)


Common mistakes ko steel-man karna


Worked example: ek generic print_all


Flashcards

C++20 concept kya hota hai, ek phrase mein?
Template parameters par ek named compile-time boolean predicate (ek constexpr bool template) jo templates ko constrain karne ke liye use hota hai.
Requires-clause aur requires-expression mein kya farq hai?
Clause ek constraint introduce karta hai aur ek bool evaluate karta hai (requires Numeric<T>); expression expressions list karke ek bool banata hai jo compile hone chahiye (requires(T a){ a+a; }).
{ a + b } -> std::same_as<T> kya check karta hai?
Ek compound requirement: a+b ek valid expression hona chahiye AUR uska type exactly T hona chahiye.
Jab do constrained overloads dono apply hote hain, kaun choose hota hai?
Zyada constrained wala (subsumption) — strictly stronger requirements jeet jaati hain.
Jab template ki constraint satisfy nahi hoti tab kya hota hai?
Template silently candidate set se remove ho jaata hai (error nahi hoti) jab tak koi candidate na bache.
Ek requires-expression ke andar chaar requirement kinds ke naam batao.
simple, type (typename T::x), compound ({e}->C), nested (requires C<T>).
Kya requires-expression runtime par code run karta hai?
Nahi — yeh sirf compile time par check karta hai ki har listed expression well-formed hai ya nahi.
Parameter x ko Numeric constrain karne ka shortest syntax batao.
auto f(Numeric auto x) — abbreviated function template.

Recall Feynman: ek 12-saal ke bachche ko samjhao

Ek khilona machine imagine karo jo sirf batteries se chalta hai. Purana C++ aapko kuch bhi daalne deta tha — kela, patthar — aur sirf phat'ta tha jab andar battery use karne ki koshish karta tha, ek confusing mess ke saath. Ek concept ek sticker hai slot par jo kehta hai "sirf batteries." Ab agar aap kela laao, machine politely kehti hai "nahi, is slot ko battery chahiye" bilkul darwaze par. Same idea: ek concept check karta hai "kya yeh type woh kar sakti hai jo mujhe chahiye?" kuch bhi tootne se pehle.

Connections

  • Templates — function & class templates
  • SFINAE and std::enable_if (purana, painful tarika jo concepts replace karta hai)
  • type_traits — std::integral, std::floating_point
  • Overload Resolution & Subsumption
  • constexpr — compile-time evaluation
  • Ranges library (C++20) (heavily built on concepts)
  • auto and decltype

Concept Map

cause

motivates

is a

used as

if unsatisfied

enables

written via

introduced by

built from

checks

moves errors to

Pre-C++20 duck-typed templates

Deep cryptic template errors

Concept: named compile-time predicate

constexpr bool on types

Constraint restricts template

Removed from candidate set

Overload on requirements

Four constraint syntaxes

requires-clause returns bool

requires-expression lists valid exprs

Every listed expression compiles

Clear call-site error