Yes, in C++20 it is absolutely possible to write a custom template helper function that takes a template non-type parameter and conditionally returns a type that can be used as a template parameter in another template. This is made much easier with C++20's enhanced support for concepts, constexpr if, and improved type deduction.
Here’s a practical example demonstrating this capability:
#include <type_traits>
#include <iostream>
// A simple concept to check if a type is integral
template<typename T>
concept Integral = std::is_integral_v<T>;
// A helper function that conditionally returns a type usable as a template parameter
template<typename T>
constexpr auto get_type_for_template(T value) {
if constexpr (std::is_same_v<T, int>) {
return std::type_identity<int>{}; // Returns a type identity wrapper for int
} else if constexpr (std::is_same_v<T, double>) {
return std::type_identity<double>{};
} else if constexpr (std::is_same_v<T, char>) {
return std::type_identity<char>{};
} else {
// Default case: use the type itself
return std::type_identity<T>{};
}
}
// Example of using the returned type as a template parameter
template<typename T>
struct MyContainer {
T value;
MyContainer(T v) : value(v) {}
};
// Helper function to create a container using the conditional type
template<typename T>
constexpr auto make_container(T value) {
auto type_selector = get_type_for_template(value);
// Extract the actual type from the type_identity wrapper
using selected_type = typename decltype(type_selector)::type;
return MyContainer<selected_type>(value);
}
int main() {
auto int_container = make_container(42); // Uses int
auto double_container = make_container(3.14); // Uses double
auto char_container = make_container('A'); // Uses char
std::cout << "int_container: " << int_container.value << '\n';
std::cout << "double_container: " << double_container.value << '\n';
std::cout << "char_container: " << char_container.value << '\n';
return 0;
}
Key Points:
-
std::type_identity (C++20): This is a utility that wraps a type and allows you to pass types around as values. It's perfect for returning types from functions.
-
if constexpr: Allows compile-time branching based on type or value conditions. This enables conditional logic at compile time.
-
Return Type Deduction: The function returns a std::type_identity<T> object whose .type member gives you the actual type.
-
Usage as Template Parameter: You can extract the type member from the returned std::type_identity and use it directly in another template like MyContainer<selected_type>.
Alternative Using Concepts and More Sophistication:
You could also write more complex logic using concepts:
template<typename T>
concept SmallInt = std::is_same_v<T, int8_t> || std::is_same_v<T, int16_t>;
template<typename T>
constexpr auto choose_storage_type(T value) {
if constexpr (SmallInt<T>) {
return std::type_identity<int16_t>{};
} else if constexpr (std::is_same_v<T, int32_t>) {
return std::type_identity<int32_t>{};
} else {
return std::type_identity<int64_t>{};
}
}
Conclusion:
Yes, C++20 provides all the necessary tools (constexpr if, std::type_identity, concepts, etc.) to create such helper functions that conditionally return types suitable for use as template parameters. This pattern is particularly useful for metaprogramming, type dispatching, and generic library design.