实例化后模板的专业化? [英] Specialization of template after instantiation?

查看:144
本文介绍了实例化后模板的专业化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的完整代码太长,但这里是一个反映我的问题的本质的片段:

My full code is too long, but here is a snippet that will reflect the essence of my problem:

class BPCFGParser {
  public:

  ...
  ...

  class Edge {
    ...
    ...
  };


  class ActiveEquivClass {
    ...
    ...
  };

  class PassiveEquivClass {
    ...
    ...
  };

  struct EqActiveEquivClass {
    ...
    ...
  };

  struct EqPassiveEquivClass {
    ...
    ...
  };



  unordered_map<ActiveEquivClass, Edge *, hash<ActiveEquivClass>, EqActiveEquivClass> discovered_active_edges;
  unordered_map<PassiveEquivClass, Edge *, hash<PassiveEquivClass>, EqPassiveEquivClass> discovered_passive_edges;

};

namespace std {


template <>
class hash<BPCFGParser::ActiveEquivClass>
{

    public:
        size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const {

        }
};

template <>
class hash<BPCFGParser::PassiveEquivClass>
{

    public:
        size_t operator()(const BPCFGParser::PassiveEquivClass & pec) const {

        }
};

}



当编译此代码时,会出现以下错误:

When I compile this code, I get the following errors:

In file included from BPCFGParser.cpp:3,
                 from experiments.cpp:2:
BPCFGParser.h:408: error: specialization of ‘std::hash<BPCFGParser::ActiveEquivClass>’     after instantiation
BPCFGParser.h:408: error: redefinition of ‘class                 std::hash<BPCFGParser::ActiveEquivClass>’
/usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of     ‘class std::hash<BPCFGParser::ActiveEquivClass>’
BPCFGParser.h:445: error: specialization of     ‘std::hash<BPCFGParser::PassiveEquivClass>’ after instantiation
BPCFGParser.h:445: error: redefinition of ‘class std::hash<BPCFGParser::PassiveEquivClass>’
/usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of     ‘class std::hash<BPCFGParser::PassiveEquivClass>’

现在我必须为这些类专门化std :: hash(因为标准的std :: hash定义不包括用户定义的类型)。当我移动这些模板专门化在类 BPCFGParser 的定义之前,我得到了各种不同的东西尝试,并在某处(http://www.parashift.com/c++-faq-lite/misc-technical-issues.html )我读到:

Now I have to specialize std::hash for these classes (because standard std::hash definition does not include user defined types). When I move these template specializations before the definition of class BPCFGParser, I get a variety of errors for a variety of different things tried, and somewhere (http://www.parashift.com/c++-faq-lite/misc-technical-issues.html) I read that:

每当你使用一个类作为模板参数,该类的声明必须是完整的,而不是简单地向前声明。
Whenever you use a class as a template parameter, the declaration of that class must be complete and not simply forward declared.

所以我被卡住了。我不能专门化模板 BPCFGParser 定义后,我不能专门化他们之前 BPCFGParser 定义,我怎么可能得到这个工作?

So I'm stuck. I cannot specialize the templates after BPCFGParser definition, I cannot specialize them before BPCFGParser definition, how may I get this working?


您需要将专业化移动到BPCFGParser中的内部类。

You need to move the specialization into an inner class inside of BPCFGParser. Doing so meets both requirements.

非常感谢您的回答:)

hash 类在命名空间 std 中定义。它不允许我专门化在非命名空间范围中的 hash 的模板。甚至以下:

hash class is defined within the namespace std. It does not allow me to specialize the templates for hash in a non-namespace scope. Even the following:

template <>
  class std::hash<ActiveEquivClass> {
...

无法使用。然而,当我用 namespace std {} 包含特殊化时,它给出了奇怪的错误:

did not work. When I enclose the specializations with namespace std {}, however, it gives the weird error of:

In file included from BPCFGParser.cpp:3,
                 from experiments.cpp:2:
BPCFGParser.h:225: error: expected unqualified-id before ‘namespace’
experiments.cpp:7: error: expected `}' at end of input
BPCFGParser.h:222: error: expected unqualified-id at end of input

中给出的答案中velocityreviews ,有人声称命名空间不能在类中定义。所以我仍然卡住。

In an answer given in velocityreviews, someone claims that namespaces cannot be defined within classes. So I'm still stuck.

推荐答案

您需要将专业化移动到BPCFGParser内部的类。

You need to move the specialization into an inner class inside of BPCFGParser. Doing so meets both requirements


  1. 专业化是在 ActiveEquivClass

  2. 使用专业化之前

示例:

class BPCFGParser {

  class ActiveEquivClass {
    ...
  };

  template <>
  class hash<ActiveEquivClass> {
     public:
        size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const {
        }
  };
  ...
  unordered_map<ActiveEquivClass, Edge *, hash<ActiveEquivClass>, EqActiveEquivClass> discovered_active_edges;

};

这篇关于实例化后模板的专业化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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