类型嵌套在模板化类中的部分特化 [英] Partial specialization with type nested in a templated class

查看:39
本文介绍了类型嵌套在模板化类中的部分特化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用模板和部分专业化,但有一个专业化我不知道如何编写...我将简化代码以使其更易于阅读.

I'm playing with templates and partial specialization, but there is one specialization I don't know how to write... I'll simplify code to make it easier to read.

让我们考虑一下

template <typename T>
    class x
{
    ...
};

通常,我可以这样专攻:

Usually, I can specialize like this :

class x<a_type>
{
    ...
};

也适用于模板类型:

template <typename T>
    class x<std::vector<T>>
{
    ...
}

现在我想对嵌套在模板类中的类型进行特化:

Now I would like to make the specialization for a type nested in a templated class:

template <typename T>
    class y
{
    struct nested_type
    {
        y a_member;
    };

    ...
};

// Here comes the specialization

template <typename T>
    class x<y<T>::nested_type>
{
    ...
};

这失败了.我也尝试将 'typename' 放在 y::nested_type 之前,但它没有解决问题.编译错误是:

This fails. I also tried to put 'typename' before y::nested_type but it did not solved the problem. Compiler error is:

type/value mismatch at argument 1 in template parameter list for ‘template <class T> struct x’

我想做的事情似乎合乎逻辑,但我不确定是否可行.我正在使用带有 g++-4.5 的 C++0x.有人知道编写这种专业化的正确语法吗?

What I want to do seems logical, but I'm not sure if it is possible. I'm using C++0x with g++-4.5. Does anybody know the correct syntax to write such specialization ?

推荐答案

答案是你不能做这个专业化.这不是语法错误,而是无法实现的东西.你必须看到模板特化有点像函数重载.编译器必须在使用站点获取类型参数,查看可用的专业化,找到匹配项,然后选择最好的(最专业的).您的示例的问题在于,这种专业化无法实现查找匹配"步骤.编译器可以期望nested_type"是任何东西,不一定是唯一的类型(如您的示例中所示),例如,它也可以是嵌套的 typedef.此外,编译器无法预测它已经看到了模板y"的所有特化,因此即使nested_type 是嵌套在y(通用模板)中的唯一类型,它也可能是即将到来的模板特化声明中的嵌套typedef 模板是".

The answer is that you cannot do this specialization. It is not a syntax error, but just something that cannot be realized. You have to see template specializations a little bit like function overloading. The compiler has to take the type argument at the use-site, look at the specializations available, find matches, and select the best one (most specialized one). The problem with your example is that the "find match" step cannot be realized with such a specialization. The compiler can expect "nested_type" to be anything, not necessarily a unique type (as it is in your example), it could also be a nested typedef, for instance. Moreover, the compiler cannot predict that it is already seeing all the specializations of template "y", so even if nested_type is a unique type nested in y (general template), it could be a nested typedef in an upcoming template specialization declaration for template "y".

就像函数重载和在那里使用的匹配算法一样,编译器推断类型的能力是有限的,它的限制是它可以做出多少假设.如果您对 x 进行了专门化,然后使用 x,则匹配是微不足道的,不需要推论,不需要假设.如果你有一个像 x 这样的专业化,然后使用 x,匹配很容易,T 可以推导出为 整数.如果你有像 x< 这样的专业.y<T>::type > 然后使用任何版本的 x,编译器应该如何从 y::type 推导出 T?它必须用整个世界中存在的所有可能类型替换 y 中的 T,以查看是否存在导致匹配嵌套类型的类型.这是一种不合理的期望,这也是 C++ 模板的类型推导能力到此为止的原因.很多时候,要知道您是否应该期望编译器能够解决某些问题,只需站在它的角度,看看它是否有可能实现(答案通常很明确).

Just like with function overloading and the matching algorithm used there, the compiler is limited in its capabilities to deduce the type, and what limits it is how much assumptions it can make. If you have a specialization for x<int> and later use x<int>, the match is trivial, no deduction needed, no assumptions needed. If you have a specialization like x<T*> and later use x<int*>, the match is easy, T can be deduced to be int. If you have a specialization like x< y<T>::type > and then use any version of x, how is the compiler supposed to deduce T from y::type? It would have to substitute for T in y all the possible types that exist in the entire world to see if there is one that results in a matching nested type. That's an unreasonable expectation, and that is why the type deduction capabilities of C++ templates stop here. Very often, to know if you should expect the compiler to be able to resolve something, just put yourself in its shoes and see if it is even remotely possible (the answer is usually clear).

这篇关于类型嵌套在模板化类中的部分特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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