English 中文(简体)
部分专门化成员函数实现
原标题:Partially specializing member-function implementations
  • 时间:2011-02-07 10:14:12
  •  标签:
  • c++

我目前正在重构一些代码,用两个模板参数显式地专门化类模板的成员函数。

template <class S, class T>
class Foo
{
  void bar();
};

template <class S, class T>
void Foo<S, T>::bar()
{ /* Generic stuff */ }

template <>
void Foo<SomeType, SomeType>::bar()
{ /* Some special function */ }

现在我添加了更多的模板参数,所以类现在看起来是这样的:

template <class S, class EXTRA0, class T, class EXTRA1>
class Foo
{
  void bar();
};

这两个额外的参数只是将typedef添加到我的类中,所以运行时功能并没有真正改变。有什么办法可以保留bar的(现在部分)专业化实现吗?我似乎想不出它的语法,我有一种预感,这可能是不可能的。

编辑:我正在寻找以下内容:

template <class EXTRA0, class EXTRA1>
void foo<SomeType, EXTRA0, Sometype, EXTRA1>::bar()
{
   /* specialized implementation */
}

它似乎无法编译。。

最佳回答

你是对的,这是不可能的。

您可以做的是在新的Foo中创建一个helper成员类模板,并将专用函数作为非模板成员函数放置在其中。专门化helper类,而不是函数。

另一种选择是将专业化转变为非模板重载。

问题回答

我不认为你想要的是那么容易实现的。这样的东西怎么样:

template <class S, class EXTRA0, class T, class EXTRA1>
class FooBase
{
    void bar();
};

template <class S, class EXTRA0, class T, class EXTRA1>
void FooBase<S, EXTRA0, T, EXTRA1>::bar()
{ /* Generic stuff */ }

template <class S, class EXTRA0, class T, class EXTRA1>
class Foo
    : public FooBase <S, EXTRA0, T, EXTRA1>
{ };

template <class EXTRA0, class EXTRA1>
class Foo<int, EXTRA0, int, EXTRA1>
    : public FooBase <int, EXTRA0, int, EXTRA1>
{
    void bar ();
};

template <class EXTRA0, class EXTRA1>
void Foo<int, EXTRA0, int, EXTRA1>::bar()
{ /* Some special function */ }

您可以通过使用specialize函子而不是函数来实现这一点:

#include <iostream>

typedef int SomeType;

template <class A, class B>
class BarFunctor {
public:
    void operator()() {
        std::cout << "generic" << std::endl;
    }
};

template <>
class BarFunctor<SomeType, SomeType> {
public:
    void operator()() {
        std::cout << "special" << std::endl;
    }
};

template <class S, class T, class EXTRA0, class EXTRA1>
class Foo {
public:
    void helloWorld() {
        std::cout << "hello world !" << std::endl;
    }

    void bar() {
        return _bar();
    }

private:
    BarFunctor<S, T> _bar;
};

int main() {

    Foo<char, char, char, char> gen;
    Foo<SomeType, SomeType, char, char> spe;
    gen.helloWorld();
    spe.helloWorld();
    gen.bar();
    spe.bar();
    return 0;
}

您可以创建基类,在基类中您可以定义除bar()之外的所有成员,然后创建派生类(一个用于通用,一个用于SomeType):

template <class S, class T>
class FooBase
{
       // All other members 
};

template <class S, class EXTRA0, class T, class EXTRA1>
class Foo:public FooBase<S,T>
{
public:
      void bar()
      {

      }
};

struct SomeType {};

template <class EXTRA0, class EXTRA1>
class Foo<SomeType,EXTRA0,SomeType,EXTRA1>:public FooBase<SomeType,SomeType>
{
public:
    void bar()
    {

    }
};

int main()
{
    Foo<SomeType,int,SomeType,int> b;
    b.bar();
}




相关问题
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?

热门标签