12

I try to create template alias which cannot be distinguished from original.

So, I create traits to check when 2 templates (not types) are equal:

template <template <class...> class C1,
          template <class...> class C2>
struct is_same_template : std::false_type {};

template <template <class...> class C1>
struct is_same_template<C1, C1> : std::true_type {};

Now test it:

// Expected alias
template <typename ... Ts> using V_Ts = std::vector<Ts...>;    // Variadic
// Fallback alias
template <typename T, typename A> using V = std::vector<T, A>; // Exact count

static_assert(!is_same_template<std::vector, V_Ts>::value); // Alias rejected by gcc/clang
static_assert( is_same_template<std::vector, V>::value);    // Alias accepted only for gcc

Demo

Is it possible to create "true" alias? which compiler is right?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • "which compiler is right?" What compilers are you using? –  May 06 '19 at 19:21
  • Your comments on the two static assertion lines do not match what I'm seeing on your gcc.godbolt demo. For one thing, the two lines are reversed in the demo, and the line ```static_assert(!is_same_template::value);``` is accepted by both gcc 8.3 and clang 8.0.0. Is there some other issue with that line that I'm not seeing? – David Dalcino May 06 '19 at 19:26
  • @DavidDalcino: assert pass, so template are different, so alias is "rejected". – Jarod42 May 06 '19 at 19:32
  • 3
    Related: https://stackoverflow.com/questions/43619075/matching-alias-template-as-template-argument – HolyBlackCat May 06 '19 at 19:33
  • There should be some more explanation, what you did, which tools you are using... – Alex Cio May 20 '19 at 12:15
  • @AlexCio: unsure what you want in addition. I want "template aliases". I provided code for my attempt with results for both clagg/gcc. – Jarod42 May 20 '19 at 14:28
  • Per HolyBlackCat: possible duplicate of [Matching alias template as template argument](https://stackoverflow.com/questions/43619075/matching-alias-template-as-template-argument) – Davis Herring Oct 10 '19 at 05:32

1 Answers1

7

I try to create template alias which cannot be distinguished from original.

I don't think this is currently possible. There are (unfortunately) no template aliases, there are only alias templates. And an alias template is always a template of its own [temp.alias]/1. A specialization of an alias template is equivalent to the type you get by substituting the template arguments into the alias template, but the alias template itself is not an alias for another template [temp.alias]/2. I would consider GCC letting your second static_assert pass a bug in GCC…

As pointed out by @HolyBlackCat in the comment above, there is a related question and answer which points to numerous related CWG issues. One issue in particular (CWG 1286) would seem to suggest that there is desire to allow an alias template to itself be equivalent to the template it refers to under certain circumstances. However, it does not seem that the proposed resolution has been adopted due to concerns raised later. The relevant wording in the current standard draft ([temp.alias] and [temp.type]) appears to be unchanged from C++11…

Michael Kenzel
  • 15,508
  • 2
  • 30
  • 39