C++11 standard have std::conditional<>
template for the type selection by the some boolean condition at compiler time.
How to do the same operation but for select the init value for variable initialization? Similar to type a = (exp) ? first_value : second_value;
.
I use my template:
template<bool B, typename T>
inline constexpr T&& conditional_initialize(T&& i1, T&& i2) {
return B ? std::move(i1) : std::move(i2);
}
But it can be used only for POD types: int a = conditional_initialize<true>(1, 2);
.
For array initialization this template is compiled with error. Wrong compile example: int a[] = conditional_initialize<true>({1, 2}, {3,4,5});
Error message: no matching function for call to 'conditional_initialize(<brace-enclosed initializer list>, <brace-enclosed initializer list>)';
Who can help me with template?