派生类中的类模板的部分专门化会影响基类 [英] Partial specialization of a class template in derived class affects base class

查看:141
本文介绍了派生类中的类模板的部分专门化会影响基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元功能:

struct METAFUNCION
{
  template<class T>
  struct apply
  {
    typedef T type;
  };
};

然后我定义一个助手:

template<class T1, class T2>
struct HELPER
{
};

然后我有了第二个元函数,它源于上面的METAFUNCTION并定义了apply struct的部分特化: / p>

And then I have second metafunction which derives from the METAFUNCTION above and defines partial specialization of apply struct:

struct METAFUNCION2 : METAFUNCION
{
  template<class T1, class T2>
  struct apply<HELPER<T1, T2> > : METAFUNCION::apply<T2>
  {
  };
};

到目前为止,很好 - 代码在g ++ 4.3.2下编译。所以我使用它如下:

So far, so good - the code compiles under g++ 4.3.2. So I used it like below:

#include <typeinfo>
#include <string>
#include <cstdlib>
#include <cxxabi.h>

template<typename T>
struct type_info2
{
  static std::string name()
  {
    char *p = abi::__cxa_demangle(typeid(T).name(), 0, 0, 0);
    std::string r(p);
    free(p);
    return(r);
  }
};

#include <boost/mpl/apply.hpp>
#include <iostream>

int main()
{
  std::cout <<
    type_info2<boost::mpl::apply<METAFUNCION, int>::type>::name() <<
    std::endl;
  std::cout <<
    type_info2<boost::mpl::apply<METAFUNCION, HELPER<float, double> >::type>::name() <<
    std::endl;
  std::cout <<
    type_info2<boost::mpl::apply<METAFUNCION2, HELPER<float, double> >::type>::name() <<
    std::endl;
  return(0);
}

输出:

int
double
double

这让我感到惊讶,因为我预期:

That surprised me a bit as I expected:

int
HELPER<float, double>
double


$ b $ p现在,我知道上面的代码不能在Microsoft Visual C ++ 2008 (我不记住消息,但它是沿着线,我不能专门应用的结构内部METAFUNCTION2结构)。

Now, I know that code like above does not compile under Microsoft Visual C++ 2008 (I don't remeber the message but it was something along the lines that I cannot specialize apply struct inside METAFUNCTION2 struct).

所以我的问题是 - 这是g ++行为符合标准?我有一个强烈的感觉,这里有一些问题,但我不是100%肯定。

So my question is - is this g++ behaviour conformant with the standard? I have a strong feeling that there is something wrong here but I am not 100% sure.


对于好奇 - 我有我想象的行为,当我重新定义METAFUNCTION2这样:

For the curious - I have the behaviuor as I expected when I redefine METAFUNCTION2 this way:

struct METAFUNCION2 : METAFUNCION
{
  template<class T>
  struct apply : METAFUNCION::apply<T>
  {
  };
  template<class T1, class T2>
  struct apply<HELPER<T1, T2> > : METAFUNCION::apply<T2>
  {
  };
};


推荐答案

以下代码是非法的:

struct METAFUNCION2 : METAFUNCION
{
  template<class T1, class T2>
  struct apply<HELPER<T1, T2> > : METAFUNCION::apply<T2>
  {
  };
};

根据C ++标准14.7.3 / 3:

According to C++ Standard 14.7.3/3:


明确专门化的函数模板或类模板的声明应在
明确专门化声明的范围内。

A declaration of a function template or class template being explicitly specialized shall be in scope at the point of declaration of an explicit specialization.

EDIT:根据 Core Issue 727 此限制不适用于成员模板的部分特殊化。

According to Core Issue 727 this restriction does not apply to partial specializations of member templates.

这篇关于派生类中的类模板的部分专门化会影响基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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