简体   繁体   中英

Why are dependent template types not deducible in partial specialization?

With clang 7.0.0, compiling this code produces the errors below:

template <typename A> class Outer
{
public:
    template <typename B> class Inner
    {
    };
};

template<typename C> struct Foo
{
};

template <typename D, typename E> struct Foo<typename Outer<D>::template Inner<E>>
{
};
 clang++-7 -pthread -std=c++11 -o main main.cpp
main.cpp:15:42: error: class template partial specialization contains template parameters that cannot be
      deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
template <typename D, typename E> struct Foo<typename Outer<D>::template Inner<E>>
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:15:20: note: non-deducible template parameter 'D'
template <typename D, typename E> struct Foo<typename Outer<D>::template Inner<E>>
                   ^
main.cpp:15:32: note: non-deducible template parameter 'E'
template <typename D, typename E> struct Foo<typename Outer<D>::template Inner<E>>
                               ^

MSVC offers a similar error.

Why aren't D and E deducible?

During template argument deduction everything on the left-hand side of the scope resolution operator :: in a template argument of the partial specialization is a non-deduced context, meaning that a template parameter appearing there will not be deduced from the corresponding argument of the specialization.

Further, if part of a qualified type name is non-deduced context, then all parameters used to specify the type are non-deduced.

So in your example neither D nor E are deduced from typename Outer<D>::template Inner<E> and since there is no other way of deducing them, deduction fails, meaning that the partial specialization is never viable.

Foo<Outer<int>::template Inner<int> > ...

C++ 编译器不会在第一次通过时进行推断,您必须告诉他您正在手动访问依赖于上下文的查找符号;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM