简体   繁体   English

这个显式模板函数是否对类模板的成员模板的成员模板有效?

[英]Is this explicit template function specialization of a member template of a member template of a class template valid?

Does anyone know if this explicit specialization is or is not valid: 有谁知道这种明确的专业化是否有效:

template <class>
struct L {
  template <typename T>
  struct O {
    template <typename U>
    static void Fun(U);
  };
};

template<>
template<typename T>
template<typename U>
void L<int>::O<T>::Fun(U) {}

clang trunk (12/3/2013) gives the following error: clang trunk(12/3/2013)给出以下错误:
f:...\\test.cpp:36:20: error: out-of-line definition of 'Fun' from class 'O' without definition f:... \\ test.cpp:36:20:错误:没有定义的'O'类的'Fun'的外线定义

void L<int>::O<T>::Fun(U) {}
     ~~~~~~~~~~~~~~^

1 error generated. 生成1个错误。

Any supporting references from the standard to justify your answer will be greatly appreciated! 我们将非常感谢您提供标准的任何支持参考资料以证明您的答案。

Note: I am somewhat surprised that it is an error - I would expect the specialization to be picked for any family of template arguments with which 'Fun' is instantiated beginning with <int><?any><?any>. 注意:我有点惊讶它是一个错误 - 我希望能够为任何模板参数系列选择特化,其中'Fun'以<int><?any><?any>.开头实例化<int><?any><?any>.

Is this a clang bug or a bug in my expectations? 这是一个铿锵的bug还是我期望的错误?

Thank you! 谢谢!

====== EDIT (I think I have an answer) ======== ======编辑(我想我有答案)========

OK - i think i found the supporting wording - from N3797 (post-chicago 2013 working draft) - 14.7.3/16 => 好的 - 我想我找到了支持措辞 - 来自N3797(后芝加哥2013年工作草案) - 14.7.3 / 16 =>

"In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well." “在类模板成员或出现在命名空间作用域中的成员模板的显式特化声明中,成员模板及其某些封闭类模板可能保持非专业化,但声明不应明确专门化类成员模板。它的封闭类模板也没有明确专门化。“

If i interpret this correctly, we need an explicit specialization of O if we're going to declare an explicit specialization of its member? 如果我正确地解释了这一点,如果我们要声明其成员的显式特化,我们需要O的显式特化? Hence the error. 因此错误。

Correct? 正确?

Thanks! 谢谢!

I don't believe it is valid. 我不相信它是有效的。 I'm not deep enough in the language to tell you how/if you can do this without providing the top-level specialization as done below, or if there is a shortcut for skipping the replicate templates, but the error message is fairly clear: You're trying to provide an implementation of a static member of a dependent nested type without providing the specialization of the actual dependency. 我没有足够的语言来告诉你如何在不提供如下所述的顶级专业化的情况下完成此操作,或者如果有跳过复制模板的快捷方式,但错误消息相当清楚:您试图提供依赖嵌套类型的静态成员的实现,而不提供实际依赖项的特化。 Ie this works: 即这是有效的:

#include <iostream>

template <typename>
struct L
{
    template <typename T>
    struct O
    {
        template <typename U>
        static void Fun(U)
        {
            std::cout << "General: " << __PRETTY_FUNCTION__ << std::endl;
        };
    };
};

// provide specialized L
template<>
struct L<int>
{
    template <typename T>
    struct O
    {
        template <typename U>
        static void Fun(U);
    };
};

// L<int> is a specialized type, so provide the rest.
template<typename T>
template<typename U>
void L<int>::O<T>::Fun(U)
{
    std::cout << "Special: " << __PRETTY_FUNCTION__ << std::endl;
}


int main()
{
    L<int>::O<double> nobj;
    nobj.Fun(10);

    L<double>::O<int> nobj2;
    nobj2.Fun(20);

    return 0;
}

Output 产量

Special: static void L<int>::O<double>::Fun(U) [T = double, U = int]
General: static void L<double>::O<int>::Fun(U) [T = int, U = int]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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