English 中文(简体)
即时后模板的专业化?
原标题:Specialization of template after instantiation?

我的全文太长了,但这里是一席之地,将反映我的问题的本质:

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 {

        }
};

}

在我编纂这一法典时,我有以下错误:

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>’

现在,我必须专门研究:这些班级的教益(因为标准:现金定义不包括用户界定的类型)。 当我把这些模板专业提升到类别(BPCFGParser)的定义之前,我就在所尝试的不同事情以及某些地方( 我读到:

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

因此,我 m。 在<条码>、CFGParser下定义之后,我不能在“条码”栏目“BPCFGParser<>条码”定义之前对模板进行专门化,我怎么做?


你们需要将专业提升到博帕尔多斯大学内部的一个内部班级。 这样做符合这两项要求。

非常感谢:

<代码>hash 类别在std上界定。 这使我无法在非名称空间范围内专门使用<代码>hash的模板。 甚至:

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

没有工作。 但是,当我附上<代码>的称呼空间({})时,它给我们留下了错误:

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

pacreviews的回答中,有人声称名称空间无法在班级内界定。 因此,我仍然 st。

问题回答

你们需要将专业提升到博帕尔多斯大学内部的一个内部班级。 满足这两项要求

  1. Specialization is after the complete definition of ActiveEquivClass
  2. Before the use of the specialization

例:

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;

};

我知道这个问题是很老的,但我刚才谈到同样的问题,我认为我已经报告我的调查结果。

错误信息:

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>’

根本不抱怨你的班级,它说,你可以专注以下几个方面:hash,要么是因为已经使用通用模板:hash,要么是现有的专业之一,即有人在定义点与你重新尝试专门化点之间使用。

http://en.cppvis.com/w/cpp/English/template_specialization”rel=“nofollow”

Specialization must be declared before the first use that would cause implicit instantiation

就我而言,问题与我的专业守则是吻合的。 固执:一味使用,你可以进一步专门使用。

我发现,如果我把专业守则移到列入“带”之后,它就立即执行。

Puppy关于将专业宣言与执行分开的建议,使你能够把声明移至非常接近于列入“设计”的范畴;执行可以到以后,只要其方便(在你的情况下全面界定BPCFGParser之后)。

转而使用斜体;和;在BPCFGParser级申报之前采用模版专业代码。 错误意味着根据以下因素扩大了散射:由用户/include/c++/4.3/tr1_impl/Function_hash.h确定。 因此,你的专业水平没有在即时使用。 理想的做法是,在扩大模板之前,应当向汇编者提供你的专业守则。

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

实际情况并非如此。 模板参数必须完整类型,其局限性一般取决于模板内容;但是,只要模板没有包含不完整的编码,就不必立即采用不完整的模板。

处理您问题的唯一途径是确定该类别之前的specialization,但界定实际成员职能operator( after。 这就是说,

template <>
class std::hash<BPCFGParser::ActiveEquivClass>
{
public:
    size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const;
};

// definition of BPCFGParser

template<> std::size_t std::hash<BPCFGParser::ActiveEquivClass>::operator()(const BPCFGParser::ActiveEquivClass & aec) const {
}

这还意味着,没有课堂,因为你不能转而宣布修课。

我提出了一个完全相同的问题,最后提出了由用户界定的散射器解决方案。

class Outer
{
    // TC++PL, 4e, 19.4.1 : A friend must be previously declared in an enclosing scope or
    // defined in the non-class scope immediately enclosing the class that is declaring it to be a friend. 
    struct Hash_inner;

    class Inner
    {
        int i;
        friend struct Hash_inner;
    };

    struct Hash_inner
    {
        size_t operator()(const Inner& in) const
        { return std::hash<int>()(in.i); }
    };

    std::unordered_map<Inner, int, Hash_inner> um;
};

而我不禁要问,是否采取了以下步骤:hash专业化办法。 谁能说明这一点?

如何在某个名称空间界定成员类别?

#include <unordered_map>
using std::unordered_map;
using std::hash;

namespace parser_detail
{
    class ActiveEquivClass { };
    class PassiveEquivClass { };
}

namespace std {
    template <>
    class hash<parser_detail::ActiveEquivClass>
    {
    public:
        size_t operator()(const parser_detail::ActiveEquivClass & aec) const { return 0; }
    };

    template <>
    class hash<parser_detail::PassiveEquivClass>
    {
    public:
        size_t operator()(const parser_detail::PassiveEquivClass & aec) const { return 0; }
    };
}

class BPCFGParser {
public:
    class Edge { };

    typedef parser_detail::ActiveEquivClass ActiveEquivClass;
    typedef parser_detail::PassiveEquivClass PassiveEquivClass;

    struct EqActiveEquivClass {
        bool operator()(const ActiveEquivClass&, const ActiveEquivClass&) const { return false; }
    };

    struct EqPassiveEquivClass {
        bool operator()(const PassiveEquivClass&, const PassiveEquivClass&) const { return false; }
    };

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

int main() { }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签