1cfd3bbb56
Topic_CSS/Topic_HTML/Topic_JavaScript/Topic_Prompt/Topic_Comfyui 등 기존 카테고리 폴더를 정리하고, Topic_Graphic/Dev 등 신규 산출물과 Topics 내부 세션/메모리 기록을 동기화.
79 lines
5.1 KiB
Markdown
79 lines
5.1 KiB
Markdown
---
|
|
id: cpp-auto
|
|
title: "C++ auto"
|
|
category: "Programming_Language"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["auto keyword C++", "type inference C++", "C++ 자동 타입 추론"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.86
|
|
created_at: 2026-07-04
|
|
updated_at: 2026-07-04
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["cpp", "programming-language", "w3schools", "auto", "type-inference"]
|
|
raw_sources: ["https://www.w3schools.com/cpp/cpp_auto.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[CPP Auto]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
C++11's `auto` hijacks the EXACT SAME keyword C already had (`auto` is a legal C storage-class specifier meaning "automatic/local storage duration," the implicit default for every local variable) and repurposes it to mean something completely unrelated — compile-time type inference — meaning any C programmer who ever learned "auto is the default, mostly-unused storage class" must unlearn that and relearn `auto` as "let the compiler deduce my type," the same token now doing a categorically different job depending on which language's rules apply. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **`auto` = compile-time type inference** — the compiler determines the variable's type from the value assigned to it, e.g. `auto x = 5;` makes `x` an `int`. [S1]
|
|
- **Must initialize at declaration** — `auto x;` with no assigned value is illegal; the compiler needs the right-hand value to infer a type from. [S1]
|
|
- **Type is locked in after inference** — once `auto x = 5;` fixes `x` as `int`, later reassigning a different type (`x = 9.99;`) is a compile error, exactly as if `int` had been written explicitly. [S1]
|
|
- **Works for any type** — `int`, `float` (`5.99f`), `double` (`9.98`), `char` (`'D'`), `bool` (`true`), and `std::string` (`string("Hello")`) are all shown inferred correctly. [S1]
|
|
- **Primary real-world use: complex types** — the tutorial notes `auto` is used sparingly for simple types (`int`, `double`) but becomes genuinely valuable for verbose/complex types like iterators and lambdas, where writing the full type out by hand is impractical. [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
- Basic substitution: `auto x = 5;` behaves identically to `int x = 5;`. [S1]
|
|
- Multi-type example: `auto myNum = 5; auto myFloatNum = 5.99f; auto myDoubleNum = 9.98; auto myLetter = 'D'; auto myBoolean = true; auto myString = string("Hello");` — each variable's true type is silently inferred (int, float, double, char, bool, std::string respectively). [S1]
|
|
- Type-locking demonstrated: `auto x = 5; x = 10; // OK; x = 9.99; // Error`. [S1]
|
|
- Cross-reference to prior chapters: this page explicitly calls back to iterators (`vector<string>::iterator it` shortened to `auto it`) and forward to lambdas as the chapters where `auto`'s real value shows up. [S1]
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
- **동일 키워드, 완전히 다른 의미**: C에서 `auto`는 지역 변수의 기본 저장 기간(automatic storage duration)을 명시하는 저장 클래스 지정자로, 사실상 거의 사용되지 않는 키워드였다. C++11은 이 키워드를 재사용하되 "타입 추론"이라는 전혀 다른 의미를 부여했다는 점이 이번 챕터에서 확인됨 — 문법 토큰은 같지만 역할은 완전히 별개. [S1]
|
|
- **한 번 추론된 타입은 고정됨**: 동적 타입 언어의 변수 재할당과 혼동하기 쉽지만, `auto`로 추론된 타입도 일반 `int`/`double` 변수와 똑같이 정적 타입 검사를 받는다는 점이 예제(`x = 9.99;` 에러)로 명확히 확인됨. [S1]
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
Iterators 챕터에서 이미 `vector<string>::iterator it` 대신 `auto it = cars.begin();`으로 축약해 사용한 것이 `auto`의 실전 활용 사례로 원문에서 직접 역참조됨. [S1]
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Type inference across multiple types, and the type-locking rule (C++):
|
|
```cpp
|
|
auto myNum = 5; // int
|
|
auto myFloatNum = 5.99f; // float
|
|
auto myDoubleNum = 9.98; // double
|
|
auto myLetter = 'D'; // char
|
|
auto myBoolean = true; // bool
|
|
auto myString = string("Hello"); // std::string
|
|
|
|
auto x = 5; // x is now an int
|
|
x = 10; // OK - still an int
|
|
x = 9.99; // Error - can't assign a double to an int
|
|
```
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.86
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[C++ Tutorial]]
|
|
- **관련 개념:** [[CPP Iterators]], [[CPP Functions Lambda]], [[C Variables]]
|
|
- **참조 맥락:** C++ 튜토리얼의 마지막 챕터 — Topic_CPP 전체 완료.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — C++ auto — https://www.w3schools.com/cpp/cpp_auto.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-07-04: Initial draft synthesized from the W3Schools "C++ auto" page (Astra wiki-curation, P-Reinforce v3.1 format).
|