Modern C++ Features
"Modern C++" का मतलब क्या है?
C++ 1998 में standardise हुई। तेरह साल तक इसमें बहुत कम बदलाव हुआ। फिर C++11 इतने सुधार लेकर आया कि लोग इसे "modern C++" कहने लगे। अब हर तीन साल में नया standard आता है:
| Standard | वर्ष | मुख्य features |
|---|---|---|
| C++11 | 2011 | auto, lambdas, smart pointers, move semantics, nullptr, range-based for, constexpr, threads |
| C++14 | 2014 | Generic lambdas, make_unique, return type deduction, binary literals |
| C++17 | 2017 | Structured bindings, std::optional, initialiser वाला if, string_view, inline variables |
| C++20 | 2020 | Concepts, ranges, std::format, modules, coroutines, <=> operator |
| C++23 | 2023 | std::print, std::expected, और ranges |
यह lesson रोज़ काम आने वाले features दिखाता है, हर एक *पुराना तरीका* vs *modern तरीका* के रूप में। उदाहरण g++ -std=c++17 (या नए) से compile करें।
auto — type compiler को निकालने दें
std::map<std::string, std::vector<int>> marks; // पुराना std::map<std::string, std::vector<int>>::iterator it = marks.begin(); // modern auto it2 = marks.begin();
Type फिर भी compile time पर तय होता है। Type साफ़ या बहुत लंबा हो तो auto लें; पढ़ने वाले की मदद हो तो type लिखें।
Range-based for loop
std::vector<int> v = {78, 91, 66};
// पुराना
for (size_t i = 0; i < v.size(); i++) std::cout << v[i] << " ";
// modern
for (int m : v) std::cout << m << " "; // copy
for (int& m : v) m += 5; // वहीं बदलो
for (const auto& m : v) std::cout << m << " "; // बड़े objects बिना copy पढ़ोNULL की जगह nullptr
int* p = nullptr; // modern: अपने type वाली असली null-pointer value // int* q = NULL; // पुराना: असल में सिर्फ 0, overload चुनने में उलझन
Uniform (brace) initialisation
int count{}; // 0
std::vector<int> v{1, 2, 3};
struct Point { int x, y; };
Point p{3, 4};
int narrow{3.7}; // error: braces narrowing conversion रोकते हैंSmart pointers
#include <memory>
// पुराना
Student* s = new Student("Aman");
delete s; // भूलना आसान
// modern
auto s2 = std::make_unique<Student>("Aman"); // अपने-आप delete
auto s3 = std::make_shared<Student>("Priya"); // साझा ownership, reference countedदेखें smart pointers।
Lambdas
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });देखें lambda expressions।
Move semantics
दस लाख elements वाले vector की copy महँगी है। Move बस उसका अंदरूनी buffer सौंप देता है।
#include <iostream>
#include <vector>
#include <utility>
int main() {
std::vector<int> a(1000000, 7);
std::vector<int> b = std::move(a); // कोई element copy नहीं; a खाली रह जाता है
std::cout << "a: " << a.size() << ", b: " << b.size() << "\n";
return 0;
}Function से local vector लौटाने पर वह अपने-आप move होता है (या copy पूरी तरह हट जाती है), इसलिए return result; सस्ता है।
constexpr — compile time पर गणना
constexpr int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); }
constexpr int f5 = factorial(5); // 120, compiler ने निकाला
int table[factorial(4)]; // 24 ints का arrayC++17 के पसंदीदा features
#include <iostream>
#include <map>
#include <optional>
#include <string>
std::optional<int> findMarks(const std::map<std::string, int>& m, const std::string& name) {
auto it = m.find(name);
if (it == m.end()) return std::nullopt; // "कोई value नहीं"
return it->second;
}
int main() {
std::map<std::string, int> marks{{"Aman", 78}, {"Priya", 91}};
for (const auto& [name, score] : marks) // structured bindings
std::cout << name << " = " << score << "\n";
if (auto r = findMarks(marks, "Ravi"); r.has_value()) // initialiser वाला if
std::cout << "Ravi = " << *r << "\n";
else
std::cout << "Ravi not found\n";
std::cout << "Priya = " << findMarks(marks, "Priya").value_or(0) << "\n";
return 0;
}- Structured bindings pairs, tuples और structs को खोलते हैं:
auto [x, y] = point; - **
std::optional<T>** में या तोTहोता है या कुछ नहीं — "नहीं मिला" के लिए-1लौटाने से ज़्यादा साफ़। - **
if (init; condition)** helper variable कोifके अंदर ही रखता है। - **
std::string_view** string का सस्ता, read-only view है (copy नहीं)।
C++20 की झलक
- **
std::format** — Python जैसी formatting:std::format("{} scored {}", name, marks)। - Ranges —
begin()औरend()देने की जगहstd::ranges::sort(v);। - Concepts — templates पर शर्त:
template<std::integral T> T gcd(T a, T b); - Three-way comparison
<=>— एक operator से<,<=,>,>=बन जाते हैं। - **
std::erase_if(v, pred)** — मिलते elements एक call में हटाएँ।
GCC 11+, Clang 14+ और नए MSVC में C++20 का अच्छा support है; -std=c++20 से compile करें।
पुराना style vs modern style
| काम | पुराना C++ | Modern C++ |
|---|---|---|
| Dynamic array | int* a = new int[n]; ... delete[] a; | std::vector<int> a(n); |
| Owning pointer | T* p = new T; ... delete p; | auto p = std::make_unique<T>(); |
| Null pointer | NULL या 0 | nullptr |
| Container पर loop | index या iterator loop | range-based for |
| Sort के लिए comparator | अलग नाम वाला function | lambda |
| Constants | #define MAX 100 | constexpr int MAX = 100; |
| "शायद value है" | -1 जैसी खास value | std::optional<int> |
आम गलतियाँ
- Turbo C++ या
-std=c++98इस्तेमाल करना और सोचना किauto/lambdas क्यों नहीं चलते — IDE में-std=c++17set करें। - Braces के साथ
auto:auto x{5};intहै, परauto y = {5};std::initializer_list<int>है। std::moveके बाद variable को ऐसे इस्तेमाल करना जैसे उसमें पुरानी value हो।const auto&चाहते हुएfor (auto m : bigObjects)(copies) लिखना।
अपना कोई पुराना program modern style में दोबारा लिखें: new[]/delete[] की जगह vector, NULL की जगह nullptr, index loops की जगह range-based for, #define की जगह constexpr, और "न मिले तो -1 लौटाओ" वाले function की जगह std::optional।
सारांश
- Modern C++ = C++11 और उसके बाद; कम से कम
-std=c++17लें। - रोज़ के features:
auto, range-for,nullptr, brace init, smart pointers, lambdas। - Move semantics से बड़े objects भेजना और लौटाना सस्ता होता है।
- C++17 structured bindings,
optional,if-init जोड़ता है; C++20format, ranges, concepts। - Modern style का मतलब कम raw pointers और कम manual
delete।
संबंधित: smart pointers, lambda, templates, C++ best practices।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Modern C++ क्या है?
auto, lambdas, smart pointers, move semantics, nullptr, range-based for और बहुत कुछ जोड़ा, और रोज़ के C++ code लिखने का तरीका बदल दिया।Student को C++ का कौन-सा version इस्तेमाल करना चाहिए?
-std=c++17 (या -std=c++20) से compile करें। Turbo C++ modern C++ बिल्कुल support नहीं करता।C++ में auto क्या करता है?
auto compiler से कहता है कि variable का type उसकी initial value से निकाल ले, जैसे auto x = 3.5; से x double बनता है, और auto it = v.begin(); से लंबा iterator type नहीं लिखना पड़ता। Type फिर भी compile time पर तय होता है — C++ dynamically typed नहीं बनती।C++ में move semantics क्या है?
std::move(x) x को movable mark करता है। इससे std::vector और std::string जैसे बड़े objects को लौटाना और भेजना बहुत सस्ता हो जाता है।