使用另一个模板类的嵌套名称说明符专门化一个模板类 [英] specialize a template class using a nested name specifier of another template class

查看:32
本文介绍了使用另一个模板类的嵌套名称说明符专门化一个模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用另一个模板类的嵌套名称说明符专门化一个模板类.但是编译器抱怨它无法推导出这段代码.我该怎么办?

template struct convert{//这是外部库中的一个类//这个库的指南告诉我要专攻//将某些功能转换为我自己的类.void foo(T&t) {/* 做某事 */}};模板 结构体{struct A_sub{//我的类};};模板 struct convert::A_sub>{//错误,编译器无法推断};

错误:类模板部分特化包含无法推导的模板参数;这个部分特化永远不会被使用[-Wunusable-partial-specialization]

结构转换::A_sub> {

main.cpp:65:19: 注意:不可推导的模板参数 'T'

模板

产生了 1 个错误.

解决方案

您需要 typename 关键字:

struct convert::A_sub>

但它不会帮助你,因为语言阻止了你想做的事情(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf temp.deduct.type):<块引用>

非推导的上下文是:

—(5.1) 使用限定 ID 指定的类型的嵌套名称说明符

想象一下这样的情况:

template 结构体{使用 C = int;};模板 <typename W>结构 Q;模板struct Q::C>{ ... };...Qq;//应该推导出 A 的哪个参数 T 以及编译器应该如何猜测?

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.

解决方案

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?

这篇关于使用另一个模板类的嵌套名称说明符专门化一个模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆