specialize a template class using a nested name specifier of another template class

浪子不回头ぞ 提交于 2019-12-23 22:53:55

问题


I want to specialize a template class using a nested name specifier of another template class. But the compiler complains that it can't deduce this code. What should I do?

template <typename T>
struct convert{ // this is a class in an extern library
                // the guide of this library tells me to specialize 
                // convert to my own class for some features.
    void foo(T&t) {/* do something */}
};

template <typename T>
struct A{
    struct A_sub{ // my class

    };
};

template <typename T>
struct convert<A<T>::A_sub> { // error, compiler can't deduce

};

error: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]

struct convert::A_sub> {

main.cpp:65:19: note: non-deducible template parameter 'T'

template

1 error generated.


回答1:


You need typename keyword:

struct convert<typename A<T>::A_sub>

But it won't help you, because language prevents what you want to do (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf temp.deduct.type):

The non-deduced contexts are:

—(5.1) The nested-name-specifier of a type that was specified using a qualified-id

Imagine situation like this:

template <typename T> struct A {
    using C = int;
};
template <typename W> struct Q;
template <typename W> struct Q<typename A<T>::C> { ... };
...
Q<int> q; // which argument T for A should be deduced and how compiler is supposed to guess?


来源:https://stackoverflow.com/questions/56626007/specialize-a-template-class-using-a-nested-name-specifier-of-another-template-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!