Hacker News new | ask | show | jobs
by slaymaker1907 722 days 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; }
1 comments

That's not polymorphism as that template parameter won't be deduced in any context and you will always have to explicitly instantiate the template.
That 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.
challenge accepted: https://gcc.godbolt.org/z/bGdP79aEj

edit: you get pseudo call-by-name as a bonus.