Software Development: Wildcards and Extended Character Sets in C++26

0
21
Software Development: Wildcards and Extended Character Sets in C++26


In addition to the major additions to Reflections and Contracts that I’ve introduced over the past few weeks, there are also some smaller, useful additions to C++26.

Advertisement





Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages ​​C++, Python, and Haskell, but also frequently speaks at expert conferences. On his blog Modern C++ he discusses his passion C++ in depth.



Structured binding is a feature of C++17 that allows you to bind multiple variables to elements of a structured object.

The following program demonstrates the use of tuples and structured binding to open and return multiple values ​​from a function.

Software Development: Wildcards and Extended Character Sets in C++26Software Development: Wildcards and Extended Character Sets in C++26

// placeholder1.cpp

#include 
#include 
#include 

// Function that returns three values
std::tuple getThreeValues() {
    int intValue = 42;
    std::string strValue = "example";
    double doubleValue = 3.14;
    return std::make_tuple(intValue, strValue, doubleValue);
}

int main() {

    // Retrieve the three values using structured binding
    auto (intValue, strValue, doubleValue) = getThreeValues();

    // Print the values
    std::cout << "Integer: " << intValue << '\n';
    std::cout << "String: " << strValue << '\n';
    std::cout << "Double: " << doubleValue << '\n';

}

Celebration getThreeValues Defined to return a tuple containing three different data types: a intA std::string and one doubleThen these values ​​are included std::make_tuple Packed into a tuple and returned by the function.

In mainfunction, program calls all three getThreeValues Values ​​returned using structured binding. Structured binding allows programs to pack tuples directly into three different variables: intValue, strValue And doubleValueThis makes the code more readable and simpler than manually unpacking the tuple.

Sometimes you don’t need all three values ​​from a function getThreeValues,

// placeholder2.cpp

#include 
#include 
#include 

// Function that returns three values
std::tuple getThreeValues() {
    int intValue = 42;
    std::string strValue = "example";
    double doubleValue = 3.14;
    return std::make_tuple(intValue, strValue, doubleValue);
}

int main() {

    // Retrieve the three values using structured binding
    auto (_, strValue, doubleValue) = getThreeValues();

    // Print the values
    std::cout << "String: " << strValue << '\n';
    std::cout << "Double: " << doubleValue << '\n';

}

this time it will happen intValue from the ceremony getThreeValues This is not needed in the following code. By convention I bind it with an underscore.

This also means that the compiler does not issue any warnings due to the variable. _ is not used:

Unfortunately, this may be counter-intuitive _ Can be used only once as an identifier. This restriction no longer applies to C++26:

// placeholder3.cpp

#include 
#include 
#include 

// Function that returns three values
std::tuple getThreeValues() {
    int intValue = 42;
    std::string strValue = "example";
    double doubleValue = 3.14;
    return std::make_tuple(intValue, strValue, doubleValue);
}

int main() {

    // Retrieve the three values using structured binding
    auto (_, strValue, _) = getThreeValues();

    // Print the values
    std::cout << "String: " << strValue << '\n';
    
}

In this version neither intValue nor that doubleValue from the ceremony getThreeValues Needed. I use two consecutive underscores.

three new characters have arrived basic character set Available:



The following program uses all three for raw string literals.


#include 

int main() {

    std::cout << '\n';

    auto raw1 = R"@(Hello\n)@";    
    auto raw2 = R"$(Hello\t)$";    
    auto raw3 = R"`(Hello\b)`";
    
    std::cout << "raw1: " << raw1 << '\n';    
    std::cout << "raw2: " << raw2 << '\n';    
    std::cout << "raw3: " << raw3 << '\n';

    std::cout << '\n';

}

The program then defines three raw string literals: raw1, raw2 And raw3Raw string literals are used in C++ delimiter(...)delimiter included, where delimiter The characters can be in any order. So the string can contain special characters like \n, \t Or \b Included without explanation.

  • raw1 is defined as R„@(Hello\n)@“, that’s the text Hello\n without him included \n This is interpreted as a line break
  • raw2 is defined as R„$(Hello\t)$“that lesson Hello\t including, without \t will be interpreted as a tab character
  • raw3 is defined as R„`(Hello\b)`“that lesson Hello\b without him included \b Interpreted as backspace.

Finally we see the output of the program:



The core C++26 language pack provides further improvements such as indexing. I will write about it in the next blog article.


(rme)

EnterJS: Learn to develop accessible websites on Accessibility DayEnterJS: Learn to develop accessible websites on Accessibility Day

LEAVE A REPLY

Please enter your comment!
Please enter your name here