Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C++20 STL Cookbook

You're reading from   C++20 STL Cookbook Leverage the latest features of the STL to solve real-world problems

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781803248714
Length 450 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Bill Weinman Bill Weinman
Author Profile Icon Bill Weinman
Bill Weinman
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Chaper 1: New C++20 Features 2. Chapter 2: General STL Features FREE CHAPTER 3. Chapter 3: STL Containers 4. Chapter 4: Compatible Iterators 5. Chapter 5: Lambda Expressions 6. Chapter 6: STL Algorithms 7. Chapter 7: Strings, Streams, and Formatting 8. Chapter 8: Utility Classes 9. Chapter 9: Concurrency and Parallelism 10. Chapter 10: Using the File System 11. Chapter 11: A Few More Ideas 12. Other Books You May Enjoy

Initialize variables within if and switch statements

Beginning with C++17, if and switch now have initialization syntax, much like the for loop has had since C99. This allows you to limit the scope of variables used within the condition.

How to do it…

You may be accustomed to code like this:

const string artist{ "Jimi Hendrix" };
size_t pos{ artist.find("Jimi") };
if(pos != string::npos) {
    cout << "found\n";
} else {
    cout << "not found\n";
}

This leaves the variable pos exposed outside the scope of the conditional statement, where it needs to be managed, or it can collide with other attempts to use the same symbol.

Now you can put the initialization expression inside the if condition:

if(size_t pos{ artist.find("Jimi") }; pos != string::npos) {
    cout << "found\n";
} else {
    cout << "not found\n";
}

Now the scope of the pos variable is confined to the scope of the conditional. This keeps your namespace clean and manageable.

How it works…

The initializer expression can be used in either if or switch statements. Here are some examples of each.

  • Use an initializer expression with an if statement:
    if(auto var{ init_value }; condition) {
        // var is visible 
    } else {
        // var is visible 
    } 
    // var is NOT visible 

The variable defined in the initializer expression is visible within the scope of the entire if statement, including the else clause. Once control flows out of the if statement scope, the variable will no longer be visible, and any relevant destructors will be called.

  • Use an initializer expression with a switch statement:
    switch(auto var{ init_value }; var) {
    case 1: ...
    case 2: ...
    case 3: ...
    ...
    Default: ...
    }
    // var is NOT visible 

The variable defined in the initializer expression is visible within the scope of the entire switch statement, including all the case clauses and the default clause, if included. Once control flows out of the switch statement scope, the variable will no longer be visible, and any relevant destructors will be called.

There's more…

One interesting use case is to limit the scope of a lock_guard that's locking a mutex. This becomes simple with an initializer expression:

if (lock_guard<mutex> lg{ my_mutex }; condition) { 
    // interesting things happen here 
}

The lock_guard locks the mutex in its constructor and unlocks it in its destructor. Now the lock_guard will be automatically destroyed when it runs out of the scope of the if statement. In the past you would have had to delete it or enclose the whole if statement in an extra block of braces.

Another use case could be using a legacy interface that uses output parameters, like this one from SQLite:

if(
    sqlite3_stmt** stmt, 
    auto rc = sqlite3_prepare_v2(db, sql, -1, &_stmt,
        nullptr);
    !rc) {
          // do SQL things
} else {  // handle the error 
    // use the error code 
    return 0;
}

Here I can keep the statement handle and the error code localized to the scope of the if statement. Otherwise, I would need to manage those objects globally.

Using initializer expressions will help keep your code tight and uncluttered, more compact, and easier to read. Refactoring and managing your code will also become easier.

You have been reading a chapter from
C++20 STL Cookbook
Published in: May 2022
Publisher: Packt
ISBN-13: 9781803248714
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime