I'm the beginner with C++. Could some one explain the return of bellow function. Thanks a lot.
template <typename T>
auto process(T arg)
{
// 4. With constexpr if, to enable the compiler to generate different code based on the type the template is instantiated with:
if constexpr (std::is_same<T, bool>::value)
return !arg;
else if constexpr (std::is_integral<T>::value)
return -arg;
else if constexpr (std::is_floating_point<T>::value)
return std::abs(arg);
else
return arg;
}
int main(){
...
{
auto v1 = process(false); // true
auto v2 = process(42); // -42
auto v3 = process(-42.0); // 42.0
auto v4 = process("42"s); // "42"
}
...
return 0;
}
what's the real code compiler for process() is generated after we call above code in main function.
Aucun commentaire:
Enregistrer un commentaire