OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How does the ODR interact with "unique" types that use a non-type template defaulted to a lambda expression?

  • Thread starter Thread starter Joshua Maiche
  • Start date Start date
J

Joshua Maiche

Guest
If a class template uses a lambda a default template argument, every default usage of that class type will be a new type.

Code:
template <auto unique = [] {}>
struct UniqueStruct {};
static_assert(!std::is_same_v<UniqueStruct<>, UniqueStruct<>>);

However, in a single translation unit, both deriving and aliasing the class keeps that derivation / alias to a single type in all the modern compilers I tested.

Code:
using UniqueAlias = UniqueStruct<>;
static_assert(std::is_same_v<UniqueAlias, UniqueAlias>);

struct UniqueDerived : UniqueStruct<> {};
static_assert(std::is_same_v<UniqueDerived, UniqueDerived>);

Will this behavior safely hold across TUs, or is there a possibility that different TUs will come up with a different lambda type for UniqueAlias / UniqueDerived? If so, would that make those types unsafe to use across multiple TUs, leaving UniqueStruct as the only safe type to use across TUs?
Continue reading...
 

Latest posts

Top