简体   繁体   English

C ++-部分专门化模板类成员函数

[英]C++ - Partially specializing a template class member function

I have a pair of template classes and I am attempting to create a specialization for one of my member functions for one of the classes. 我有一对模板类,并且正在尝试为其中一个类的成员函数创建一个特殊化。 The general form of what I have is: 我所拥有的一般形式是:

template <typename T, int K>
class URLListNode {
    // Some other stuff
    void split( class URLListNode<T,K>*, class URLListNode<T,K> );
}

For the general case I have: 对于一般情况,我有:

template <typename T, int K>
class URLListNode<T,K>::split( URLListNode<T,K>* node1, URLListNode<T,K>* node2 ){
    // Code here
}

And that works. 那行得通。 I am now trying to create a specialization for the case of K=1. 我现在正在尝试为K = 1的情况创建一个特殊化。 Here is what I have so far: 这是我到目前为止的内容:

template <typename T>
class URLListNode<T,1>::split( URLListNode<T,1>* n1, URLListNode<T,1>* n2 ) {
    // Code here
}

But when I try to compile, I get the following errors: 但是当我尝试编译时,出现以下错误:

In file included from ../src/driver.cpp:34:0:
../src/urllist.h:101:80: error: invalid use of incomplete type ‘class URLListNode<T, 1>’
void URLListNode<T,1>::split( URLListNode<T,1>* node1, URLListNode<T,1>* node2 ) {
                                                                            ^
../src/urllist.h:29:7: error: declaration of ‘class URLListNode<T, 1>’
class URLListNode {
   ^
make: *** [src/driver.o] Error 1

This is on g++-4.8.2 这是在g ++-4.8.2上

Main two syntax fixes: 主要的两个语法修复:

  • Semicolon at end of class definition. 在课程定义末尾使用分号。

  • Replacing the word class with void for the return type of the function definition. 将单词classvoid代替,以表示函数定义的返回类型。

Main problem, that you can't partially specialize a function. 主要问题是,您无法部分专门化功能。

Partially specializing the original class is not a good solution, because then the other original member declarations have to be inherited in or duplicated in the specialization. 对原始类进行部分专业化不是一个好的解决方案,因为其他原始成员声明必须在专门化中继承或重复。

General solution: use a static function member of a class, which can be partially specialized. 通用解决方案:使用类的静态函数成员,该成员可以是部分专用的。

#include <iostream>
using namespace std;

template <class T, int K>
class URLListNode {
    // Some other stuff
public:
    void split( URLListNode*, URLListNode* );
};

namespace detail {
    template <class T, int K>
    struct Impl
    {
        typedef URLListNode<T, K> Node;
        static void split( Node& self, Node*, Node* )
        { (void) self; cout << "Generic split" << endl; }
    };

    template <class T>
    struct Impl<T, 1>
    {
        typedef URLListNode<T, 1> Node;
        static void split( Node& self, Node*, Node* )
        { (void) self; cout << "Partially specialized split." << endl; }
    };

}  // namespace detail

template <class T, int K>
void URLListNode<T,K>::split( URLListNode* n1, URLListNode* n2 )
{
    detail::Impl<T, K>::split( *this, n1, n2 );
}

int main()
{
    URLListNode<int, 0>().split( 0, 0 );     // Generic.
    URLListNode<int, 1>().split( 0, 0 );     // Partially specialized.
}
#include <iostream>

using namespace std;

template<class T, int K>
class URLListNode
{
public:
    void split(class URLListNode<T,K>*, class URLListNode<T,K>*)
    {
        cout << "split generic" << endl;
    }
};

template<class T>
class URLListNode<T, 1>
{
public:
    void split(class URLListNode<T, 1>*, class URLListNode<T,1>*)
    {
        cout << "split specific" << endl;
    }
};

int main()
{
    URLListNode<int, 1> x;
    x.split(&x, &x);

    cin.get();
}

Is this what you want? 这是你想要的吗?

That does what I want it to do, but is there any way of doing this without creating an entire new template class? 那就是我想要它做的事,但是有没有办法创建一个新的模板类呢? I only have a couple of functions out of a fairly complex pair of classes that need to be specialized. 在一对相当复杂的需要专门化的类中,我只有几个函数。

You cannot partially specialize member functions. 您不能部分专门化成员函数。 You must specialize the whole class. 您必须全班学习。

14.5.5.3.1. 14.5.5.3.1。

The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization. 类模板部分专业化成员的模板参数列表应与类模板部分专业化的模板参数列表匹配。 The template argument list of a member of a class template partial specialization shall match the template argument list of the class template partial specialization. 类模板部分专业化成员的模板参数列表应与类模板部分专业化的模板参数列表匹配。

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

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