34

My goal is to record the name of a templated type when defining other types with template template parameters. The problem I have encountered is that the resulting types are considered to be inequivalent.

To achieve this I have tried using a templated "using" alias to record the name given as the template template argument. I then refer to the alias to recover the name. An example of where this is useful is when I have a templated function that returns a type with template template parameter that needs to be deduced.

The following code shows my approach:

include <type_traits>

template<typename X>
struct TT {};

template<template<typename> class T>
struct UU {
  template<typename X>
  using name = T<X>;
};

template<template<typename> class T>
struct VV {};

int main() {
  static_assert(std::is_same<
    TT<int>,
    UU<TT>::template name<int>
  >::value, "Templated type instances are distinct");
  static_assert(std::is_same<
    VV<TT>,
    VV<UU<TT>::template name>
  >::value, "Template type instances using template names are distinct");

  return 0;
}

Compiling as follows:

c++ -std=c++14 Test.cpp -o Test

Yields:

Test.cpp:20:3: error: static_assert failed "Template type instances using template names are distinct"

Is there another way of recording and using template names?

Can someone direct me to the part of the standard where this behavior is specified?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
AngelGabriel
  • 694
  • 4
  • 12

0 Answers0