c – 实例化后模板的专业化?

前端之家收集整理的这篇文章主要介绍了c – 实例化后模板的专业化?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的完整代码太长了,但这里的代码片段将反映我的问题的本质:
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,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 {

        }
};

}

当我编译此代码时,我收到以下错误

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)我读到:

Whenever you use a class as a template parameter,the declaration of that class must be complete and not simply forward declared.

所以我被卡住了.我不能在BPCFGParser定义之后专门化模板,我不能在BPCFGParser定义之前专门化它们,我怎么能得到这个呢?

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

非常感谢你的答案:)

哈希类在命名空间std中定义.它不允许我在非命名空间范围内专门化哈希模板.甚至如下:

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

不工作.但是当我用命名空间std {}括起特化时,它会给出奇怪的错误

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给出的答案中,有人声称命名空间不能在类中定义.所以我仍然被困住了.

解决方法

您需要将专门化移动到BPCFGParser内部的内部类中.这样做符合这两个要求

>专业化是在ActiveEquivClass的完整定义之后
>在使用专业化之前

例:

class BPCFGParser {

  class ActiveEquivClass {
    ...
  };

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

};
原文链接:https://www.f2er.com/c/119201.html

猜你在找的C&C++相关文章