1. Problem It Solves
C++23 fills two deduction and scope gaps. Inherited constructors can contribute to CTAD, and an alias declaration can live in an if or switch init-statement with tightly limited scope. The goal is to make the relevant rule visible in code instead of relying on an assumption about what “C++23 support” means.
2. Prerequisites
Day 4: templates and class template argument deduction.
Earlier knowledge of
ifandswitchinit-statements.
3. Core Idea
Inherited CTAD lets the derived template borrow constructor deduction clues. An alias init-statement is a temporary vocabulary word visible only to the following condition and body. Read the syntax from left to right, identify the value, object, or range being transformed, and then check its resulting type, lifetime, or ownership. Standardization and implementation are separate: a C++23 mode may still lack one library component, so a feature-test macro is part of responsible portable use.
4. Minimal Syntax
if (using score_t = int; score_t{7} > 5) { }5. How It Works
The sample builds the smallest expression or object needed for Inherited-Constructor CTAD and Alias Init-Statements.
It uses a scoped alias in an
ifand keeps inherited-CTAD syntax behind an explicit support guard. The compiler and library apply the relevant rule before the program observes the result.The program prints or checks the selected branch plus a truthful note about compiler support, making the important behavior easy to verify.
6. Common Mistakes
Assuming every inherited constructor yields a useful deduction guide can fail when template parameters cannot be inferred; leaking a broad alias outside the condition defeats the scope benefit.
Enabling C++23 mode without checking the relevant feature macro may select code that the installed compiler or standard library does not implement yet.
7. When to Use It
Use it when the task involves small derived wrappers with deducible constructor arguments and local type names used only by one branch.
Avoid it when the task involves CTAD when an explicit template argument communicates domain meaning more clearly.
8. Simple Example
A validation branch introduces using score_t = int only where the score expression is checked. The downloadable program keeps the data fixed so the output can be compared without entering input.
Complete sample code
Source file
cpp23/19_inherited_ctad_alias_init_statements/main.cpp
#include <iostream>
#if defined(ENABLE_INHERITED_CTOR_CTAD)
template <class T>
struct Box {
T value;
Box(T input) : value(input) {}
};
template <class T>
struct Tagged : Box<T> {
using Box<T>::Box;
};
#endif
int main() {
if (using score_t = int; score_t{7} > 5)
std::cout << "alias init-statement selected\n";
#if defined(ENABLE_INHERITED_CTOR_CTAD)
Tagged item{42};
std::cout << item.value << '\n';
#else
std::cout << "inherited CTAD demo disabled\n";
#endif
}
9. Key Takeaways
Separate the language rule from compiler and library availability.
Keep lifetime, ownership, and deduced types visible when they affect correctness.
Prefer the smallest syntax that communicates the intent.
Guard facilities that are not yet uniformly implemented.
10. Self-Check Questions
Easy — What is the smallest syntax in Section 4, and what main job does it perform?
Medium — Read the sample program: which value, type, or branch is observed, and why?
Hard — When an inherited constructor mentions only part of a derived class template's parameters, which parameters can CTAD infer and what remains impossible?