5

The following code works fine:

#include <type_traits>

template <typename> class AddLayer;

template <template <typename> class TLayer>
struct LayerInputPortSet
{
    using type = int;
};

template <>
struct LayerInputPortSet<AddLayer>
{
    using type = float;
};

using type = typename LayerInputPortSet<AddLayer>::type;
static_assert(std::is_same_v<type, float>);

But with the following modification:

#include <type_traits>

template <typename> class AddLayer;

template <template <typename> class TLayer>
struct LayerInputPortSet
{
    using type = int;
};

template <>
struct LayerInputPortSet<AddLayer>
{
    using type = float;
};

template <template<typename> class TLayer>
struct Sublayer
{
    template <typename TInputs>
    using LayerType = TLayer<TInputs>;
};

using type = typename LayerInputPortSet<Sublayer<AddLayer>::template LayerType>::type;
static_assert(std::is_same_v<type, float>);

As I understand, since "A type alias declaration introduces a name which can be used as a synonym for the type denoted by type-id", therefore Sublayer<AddLayer>::template LayerType is an alias of AddLayer, so type should be float based on the template specialization.

This code works on GCC, but it cannot be compiled on clang 6, clang 7, clang 8, and VS 2019. In fact, if I change the last line as:

static_assert(std::is_same_v<type, int>);

The code could be compiled on clang 6, clang 7, clang 8, and VS 2019.

I don't know which compiler's behavior is correct: GCC or Clang/VS ?

Thanks a lot!

Wei Li
  • 1,847
  • 2
  • 10
  • 10
  • [It also compiles ins gbd](https://onlinegdb.com/B1yrsmhuS), but I couldn't tell you which is correct. –  Oct 10 '19 at 03:55
  • I prefer [compiler explorer](https://godbolt.org/z/lpZ5Am) @Chipster – Isura Manchanayake Oct 10 '19 at 04:00
  • In fact, there is a similar question (https://stackoverflow.com/questions/17392621/using-a-template-alias-instead-of-a-template-within-a-template). One answer about that question is "An alias template name is never deduced." But I don't think this answer is correct. Since there is no argument deduction involved inside the code. – Wei Li Oct 10 '19 at 04:09

0 Answers0