2

This is directly linked/referenced by the question asked here Declaring a new struct on the fly. There are a few solutions presented with a very elegant solution using a lambda, exploiting the fact that every lambda has a unique type. The solution is as follows:

template <size_t>
constexpr auto make_new_type() { return [](){}; }

using A = decltype(make_new_type<__LINE__>());
using B = decltype(make_new_type<__LINE__>());
using C = decltype(make_new_type<__LINE__>());

What problem does the above solve which could be applied "generally"?

Samer Tufail
  • 1,835
  • 15
  • 25
  • 1
    Most often this is used to replace the unique integer IDs (like enums) by unique types. With templates this allows you to solve a number of problems, for example prohibit the function calling for individual IDs or use template argument deduction (which is not available for non type template arguments). Roughly speaking, this is the way to create "enum" each element of which has not just a unique value, but a unique type. It is a sad that the question on hold, I have a good example. – Dmytro Dadyka Mar 26 '19 at 12:03
  • @DmytroDadyka that is intresting, I am most certainly interested in your example and probably others would be too. You can vote to reopen. – Samer Tufail Mar 26 '19 at 12:11

0 Answers0