In addition to the big additions to Reflection and Contracts that I’ve presented in a few posts, some smaller, useful additions are also planned for 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.
Last week I showed wildcards and extended character sets, and this time I’m talking about additional additions to the C++ standard.
static_assert
Expansion
First, here’s the syntax static_assert
In C++11:
static_assert(compile time predicate, unevaluated string)
In C++26, string can be a user-defined data type with the following properties:
- One
size()
Method that produces an integer - One
data()
Method that produces a pointer of character type - elements in
range (data(), data()+size())
are valid. ,P2741R3,
static_assert
Can now be used with formatting strings. Here’s a good example from the proposal P2741R3I turned it into a whole program.
// static_assert26.cpp
#include
#include
template
constexpr bool ensure_size() {
static_assert(sizeof(T) == Expected, "Unexpected sizeof");
return true;
}
struct S {
int _{};
};
int main() {
std::cout << std::boolalpha << "C++11\n";
static_assert(ensure_size());
std::cout << std::boolalpha << "C++26\n";
static_assert(sizeof(S) == 1,
std::format("Unexpected sizeof: expected 1, got {}", sizeof(S)));
std::cout << '\n';
}
template ensure_size
Three parameters are defined to be accepted: a data type T, an expected size Expected
and an optional parameter Size
which is the default size for T
matches. Does any checking within the function static_assert
-Instructions of any size T
even Expected
Is. If the sizes do not match, compilation fails with the message Unexpected sizeof
Memory. function returns true
Return upon completion of assurance.

The program then defines a simple structure S containing a single integer element _
prevent. This structure is used static_assert-
Display functionality.
In main
Function, program comes initially with C++11 std::boolalpha
To get boolean value in console true
Or false
to format. will do again static_assert
with, which checks whether the size S
Is 1 byte. since the size of S
is actually larger than 1 byte, this assertion will fail and result in a compilation error.
Next, the program outputs C++26 to the console and uses another static_assert
this time is coming std::format
For use. size of if S
There is 1 byte but not 4, compilation failed.
Looking at the gcc error messages three errors appear. std::format
not yet constexpr
,
pack indexing
Pack indexing is probably the favorite template improvement for template metaprogramming fans.
The following example is based on the proposal P2662R3,
// packIndexing.cpp
#include
#include
template
constexpr auto first_plus_last(T... values) -> T...(0) {
return T...(0)(values...(0) + values...(sizeof...(values)-1));
}
int main() {
std::cout << '\n';
using namespace std::string_literals;
std::string hello = first_plus_last("Hello"s, "world"s, "goodbye"s, "World"s);
std::cout << "hello: " << hello << '\n';
constexpr int sum = first_plus_last(1, 2, 10);
std::cout << "sum: " << sum << '\n';
std::cout << '\n';
}
The example provided is a function template that calculates the sum of the first and last elements of a parameter pack.
The function is defined as a template that accepts a variable number of parameters of any data type T. The return type of a function is specified using the following return type syntax.
Returns the sum of the first and last elements of the function body parameter pack. expression values...(0)
reaches the first element and values...(sizeof...(values)-1)
Reaches the last element.
Here is the output of the program:
delete
with reason
With C++26 you may have a reason for this delete
prompt. I believe this will become established as best practice. The following program is intended to clarify the process.
// deleteReason.cpp
#include
void func(double){}
template
void func(T) = delete("Only for double");
int main(){
std::cout << '\n';
func(3.14);
func(3.14f);
std::cout << '\n';
}
Celebration func
Is overloaded in two ways. The first overload is a regular function which is a double
Used as a parameter. This function can be used without any problems double
Can be called logic.
The second overload is a template that can take any data type as a parameter. However, this function is clearly used = delete
specifier with custom message”Only for double
“Removed. This means that each instantiation will have a data type other than double
This results in a compilation error and the provided message is displayed.
In main
Function calls program func
with logic 3.14
But, that one double
Is. This call is valid and invokes non-template overloading func
But.
Next program tries func
with logic 3.14f
to call him float
Is. Since there is no non-template overloading func
there is one float
Once accepted, the template functions will start working immediately. However, since the template function works for all data types except double
has been removed, this call resulted in a compilation error with the message “Only for double
,
What will happen next?
In my next article I will discuss C++26 libraries.
(rme)
