Mehrschichtiger Programmier-Tutor-Prompt mit ELI5 und Fehlkonzept-Warnungen
Von Wikiprompt, der freien Prompt-Enzyklopädie
Mehrschichtiger Programmier-Tutor-Prompt mit ELI5 und Fehlkonzept-Warnungen Ein strukturierter Lehr-Prompt, der eine KI dabei anleitet, jedes Codierungsthema in drei progressiven Ebenen zu erklären - von einfacher Analogie bis zur technischen Tiefe - plus Hinweise auf Missverständnisse und Vorschläge für nächste Schritte.
Prompt-InhaltSpeichern
🌐
LAYER 1 - Explain Like I'm 5
Imagine you have a big box of LEGO bricks. You want to build a castle, but you don't want to build it from scratch every time. So, you build the castle once, then you take a photo of it. Next time, you just look at the photo and copy it exactly. That photo is like a **template**. You can make many castles from that one photo, and if you want a different color roof, you just change the photo slightly before copying. You don't have to re-invent the castle each time.
---
LAYER 2 - The Real Explanation
**What it is:**
A **template** (or generic) is a blueprint for creating functions, classes, or data structures where the type of data they operate on is specified later as a parameter.
**Why it exists / what problem it solves:**
It solves code duplication. Without templates, you'd write a separate function for `int`, `float`, `string`, etc., even if the logic is identical. Templates let you write the logic once and let the compiler generate the specific version for each type you use.
**How it works at a fundamental level:**
At compile time, the compiler sees a template usage (e.g., `max<int>(3, 5)`) and instantiates a concrete version of the code with `int` substituted for the template parameter `T`. This is called **compile-time polymorphism** (as opposed to runtime polymorphism with virtual functions).
**Simple code example (C++):**
```cpp
// T is a placeholder for any type
template <typename T>
T getMax(T a, T b) {
return (a > b) ? a : b; // Works for any type that supports '>'
}
int main() {
int x = getMax<int>(3, 5); // Explicit: compiler generates int version
double y = getMax(3.14, 2.71); // Implicit: compiler deduces double
// No need to write separate getMax for int, double, etc.
}
```
---
LAYER 3 - Now I Get It (Key Takeaways)
- Templates are **compile-time** code generators – they don't exist at runtime, only the concrete instantiations do.
- They enable **generic programming**: write once, use with any type that satisfies the required operations (e.g., `>` for `getMax`).
- Overuse can bloat binary size (code bloat) because each type instantiation creates a separate copy.
---
MISCONCEPTION ALERT
- **"Templates are slow at runtime."** – False. They are as fast as hand-written code for each type; the "slowness" is only in compile time.
- **"Templates work with any type."** – False. They only work if the type supports the operations used inside (e.g., `+`, `==`). If you call `getMax` on a type without `>`, you get a compile error.
---
OPTIONAL - Further Exploration
- **Template specialization** – writing a custom version for a specific type (e.g., `bool`).
- **Variadic templates** – templates that accept a variable number of arguments (used in `printf`-style code).
- **Concepts (C++20)** – formal constraints on template parameters to give better error messages.
Melde dich an, um den vollständigen Prompt zu sehen
Weiter mit:
Mit der Anmeldung akzeptierst du unsere Nutzungsbedingungen und Datenschutz
Verwendung
Dieser Prompt ist für die Verwendung mit education gedacht. Kopiere den Inhalt oben und füge ihn in dein bevorzugtes KI-Tool ein.
Für beste Ergebnisse passe die Platzhalter (eckige Klammern oder Großbuchstaben) an deine Anforderungen an.
Diskussion
0 Kommentare