-1

I am experimenting with template partial specializations and I have come to the following code.

template <typename T>
struct X {
    using type = T;
};

template <typename T>
struct Y {};

template <typename T>
struct Y<typename X<T>::type> {};

Clang, GCC, and MSVC seem to complain that the template parameter is not deducible in this context. However, the partial specialization should never be matched. Thus every specialization would rely on the the primary template definition.


I can't find the section in the specification which prohibits this.

Mechap
  • 319
  • 3
  • 9
  • 4
    `typename X::type` can be *any* type, you cannot partial specialize *any* type. – 康桓瑋 Oct 25 '21 at 11:36
  • Are you just experimenting and testing, or is there an underlying problem that you need to solve with code like that? If you have an underlying problem then please ask about it directly instead (including your code as your attempt to solve it). If it's just plain curiosity then that's fine, but please state so explicitly. – Some programmer dude Oct 25 '21 at 11:39
  • 3
    https://stackoverflow.com/q/25245453/817643 – StoryTeller - Unslander Monica Oct 25 '21 at 11:40
  • 2
    Note that `X::type` is exactly the same as `T`... – Aconcagua Oct 25 '21 at 11:42
  • 1
    _However, I can't find the section in the specification which prohibits this._ 1. Open standard section about class template partial specializations. 2. Search for "deduc". ... https://timsong-cpp.github.io/cppwp/n4868/temp.class.spec.match#3 – Language Lawyer Oct 25 '21 at 11:43
  • @Someprogrammerdude It's just plain curiosity. – Mechap Oct 25 '21 at 11:44
  • Your question sounds like you're searching for where specification prohibits non-deducible partial specialization arguments. – Language Lawyer Oct 25 '21 at 11:47
  • @Aconcagua yes, the primary template would always be used – Mechap Oct 25 '21 at 11:48
  • The specification doesn't have to explicitly prohibit specializations that are not specializing anything. – Tharsalys Oct 25 '21 at 11:50
  • @Tharsalys then it should be well-formed even though the specialization would never be matched right ? – Mechap Oct 25 '21 at 11:52
  • 1
    @Tharsalys Well, at least GCC complains about (`error: partial specialization 'struct Y' does not specialize any template arguments; [...]`. Not a reference, sure, but a hint at least... – Aconcagua Oct 25 '21 at 11:55
  • I have updated my post since it appears to be non-clear. – Mechap Oct 25 '21 at 12:00
  • 2
    @Aconcagua I think it gets covered under: https://timsong-cpp.github.io/cppwp/n4861/temp.class.spec#9.2. – Tharsalys Oct 25 '21 at 12:24

1 Answers1

2
template <typename T>
struct Y<typename X<T>::type> {};

Is not more specialized than the primary class template.

template <typename T>
struct Y {};

In other words, it accepts the same set of types that the primary class template accepts, not a subset of it. The exact clause from the specification.

EDIT:

Thanks to @Jarod42 's clarification in the comments, the program is ill-formed because the argument in partial specialization is not deducible, not because it's not more specialized. The program fails at instantiating X, not at Y.

Tharsalys
  • 318
  • 1
  • 11
  • 2
    How do you consider it is not more specialized than the primary template? With its definition: `using type = T;`? if so `using type = T&;` would also fails... – Jarod42 Oct 25 '21 at 13:03
  • My code is ill-formed because it is not more specialized than the primary template, where as your code would fail because `typename X::type` isn't deducible in this context. Don't get me wrong, my partial specialization also uses a non-deducible type but since it can't be matched, the compiler can't use it and has to rely on the primary template. So with `using type = T`, the code is ill-formed because the partial specialization isn't more specialized than the primary template and with `using type = T&`, the code is ill-formed because `type` is a non-deducible type. – Mechap Oct 25 '21 at 14:46
  • @Mechap In both cases your code is ill-formed because specialization argument is not deducible. – Language Lawyer Oct 25 '21 at 14:48
  • Isn't it also ill-formed because it is not more specialized than the primary template ? – Mechap Oct 25 '21 at 14:52
  • @Mechap - "More specialized" is not some hand-wavy term. It's defined in terms of a process that involves template argument deduction between the primary and specializations. If the arguments cannot be deduced, the relationship doesn't hold. – StoryTeller - Unslander Monica Oct 25 '21 at 15:13
  • Ok I think I get it now. Thank you very much. – Mechap Oct 26 '21 at 07:27