Assuming the template function
template<typename T>
T foo(){
// ...
// Error occured
if(error)
return 0;
// ...
}
which should return 0
, 0.0f
, nullptr
, ... depending on the type T
, when an error occured.
How to get the 0
of a unknown template type?
in C# you can write default(T)
to do this.
How to perform this in C++?
You can use value initialization like return T();
or return T{};
(since C++11), or just return {};
(see list initialization (since C++11)) to return the default value of T
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With