Starfish
Starfish

Reputation: 707

Template partial specialization issues "template parameters not deducible in partial specialization"

I am trying to understand why there is a "template parameters not deducible in partial specialization". I could not find an answer to this with current answers.

Pre:

template<std::size_t, class T, class F>
struct IF {
    using type = T;
};

template<class T, class F>
struct IF<0, T, F>
{
    using type = F;
};

Actual Issue:

template <std::size_t x, std::size_t y, std::size_t z = x*y >
struct Selector
{
    using A = int; //simplified for questioning- uses x,y 
    using B = float;

    using type = typename IF<z, A, B>::type;
};


template <std::size_t x, std::size_t y>
using MyType = typename Selector<x, y>::type;


//Everything is good until here. 

template <typename T>
struct XTyp;
// struct XTyp : std::integral_constant<std::size_t, 0> {};

template <std::size_t x, std::size_t y>
struct XTyp<MyType<x, y> > : std::integral_constant<std::size_t, x>
{
};

error: template parameters not deducible in partial specialization: struct XTyp<MyType<x, y> > : std::integral_constant<std::size_t, x> note: ‘x’ note: ‘y’

See: https://godbolt.org/z/673f9WM3G

Upvotes: 2

Views: 623

Answers (1)

user17732522
user17732522

Reputation: 76628

Inserting the type alias, your partial specialization reads

template <std::size_t x, std::size_t y>
struct XTyp< typename Selector<x, y>::type > : std::integral_constant<std::size_t, x>
{
};

When the compiler needs to check whether this partial specialization should be chosen for a given specialization XTyp<Arg>, it has to check whether there are values x and y, such that Arg is the same as Selector<x, y>::type. But that is generally impossible, since each specialization of Selector<x, y> could have any type, including Arg, as type.

In terms of the language specification, everything left of a :: is a non-deduced context in which template argument deduction cannot deduce template arguments. But the x and y template arguments of the partial specialization must be deducible from Arg as if XTyp<Arg> was given as argument in a call to a function template with function parameter XTyp< typename Selector<x, y>::type >.

Since the template arguments of the partial specialization are never deducible, the compiler will reject the specialization outright.

Upvotes: 1

Related Questions