4 ms·
You can sort of do it so long as the return type is a template parameter. template<typename T> T my_construct() { T result; return result; }
by slaymaker1907 2y ago
You can sort of do it so long as the return type is a template parameter.
template<typename T>
T my_construct() { T result; return result; }
- account42 2y agoThat's not polymorphism as that template parameter won't be deduced in any context and you will always have to explicitly instantiate the template.
- Maxatar 2y agoThat is still polymorphic, but as I mentioned C++ does not do the kind of type deduction that Haskell supports so you do have to explicitly instantiate the template. However, you can instantiate it based on context, for example using decltype.
- gpderetta 2y agochallenge accepted: https://gcc.godbolt.org/z/bGdP79aEj https://gcc.godbolt.org/z/bGdP79aEj edit: you get pseudo call-by-name as a bonus.