Yes, absolutely!
In C++20, you can write template helper functions that take non-type template parameters and return types. Here are several approaches:
1. Using auto as template parameter (C++17+)
#include <type_traits>
#include <iostream>
// Helper that returns a type based on a value
template<auto Value>
constexpr auto get_type() {
if constexpr (Value > 10) {
return int{};
} else {
return double{};
}
}
// Use decltype to extract the type
template<auto Value>
using ConditionalType = decltype(get_type<Value>());
// Use it in another template
template<auto N>
class MyClass {
ConditionalType<N> value;
public:
MyClass(ConditionalType<N> v) : value(v) {}
void print() {
std::cout << "Value: " << value << " (type size: "
<< sizeof(value) << ")\n";
}
};
int main() {
MyClass<5> obj1(3.14); // Uses double
MyClass<15> obj2(42); // Uses int
obj1.print();
obj2.print();
}
2. Using Type Traits Pattern (Traditional)
#include <cstdint>
#include <type_traits>
// Helper struct instead of function
template<auto Value>
struct TypeSelector {
using type = std::conditional_t<
(Value < 256),
uint8_t,
std::conditional_t<
(Value < 65536),
uint16_t,
uint32_t
>
>;
};
// Convenience alias
template<auto Value>
using TypeSelector_t = typename TypeSelector<Value>::type;
// Use in another template
template<auto MaxValue>
class Counter {
TypeSelector_t<MaxValue> count = 0;
public:
void increment() { ++count; }
auto get() const { return count; }
void print_info() {
std::cout << "Storage size: " << sizeof(count) << " bytes\n";
}
};
int main() {
Counter<100> small; // Uses uint8_t
Counter<1000> medium; // Uses uint16_t
Counter<100000> large; // Uses uint32_t
small.print_info();
medium.print_info();
large.print_info();
}
3. More Complex Example with Multiple Parameters
#include <array>
#include <vector>
#include <iostream>
// Helper that decides between static and dynamic storage
template<auto Size>
constexpr auto get_container_type() {
if constexpr (Size > 0 && Size <= 100) {
return std::type_identity<std::array<int, Size>>{};
} else {
return std::type_identity<std::vector<int>>{};
}
}
template<auto Size>
using Container = typename decltype(get_container_type<Size>())::type;
template<auto Size>
class DataHolder {
Container<Size> data;
public:
DataHolder() {
if constexpr (Size <= 0 || Size > 100) {
data.resize(Size > 0 ? Size : 10);
}
}
auto& get_data() { return data; }
};
int main() {
DataHolder<10> small; // std::array<int, 10>
DataHolder<200> large; // std::vector<int>
std::cout << "Small size: " << small.get_data().size() << "\n";
std::cout << "Large size: " << large.get_data().size() << "\n";
}
Key Points:
if constexpr allows compile-time branching based on non-type parameters
decltype extracts the type from the helper function's return value
std::type_identity (C++20) is useful when you need to return type information
auto template parameters (C++17) make non-type parameters more flexible
All of these patterns work great for conditionally selecting types based on compile-time values!