静态成员函数模板专业化 [英] Static Member Function Template Specialization

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

问题描述

各位大家好,请提前感谢您的帮助。我是C ++

模板的新手,并且使用成员函数

模板遇到了一些问题。我有一个包含模板的共享库,我试图从可执行文件中使用
,使用gcc 4.1.2进行编译。一切

工作正常,直到我尝试在非模板类中专门化一个静态成员函数

模板。我有一种感觉我搞砸了

显而易见的事情,所以在我发布一堆代码之前做了以下

看起来是否正确?谢谢。


---共享库(Arrays.h)---


名称空间示例{

class Arrays {

public:

template< class Tstatic int compareItems(T * a1,T * a2,int length);

protected:

数组(){};

};


模板< class Tint Arrays :: compareItems(T * a1,T * a2,int length){

printf(Calling generic function.\ n);

返回1;

}


模板< int Arrays :: compareItems< char>(char * a1,char * a2,int

长度){

printf(Calling char specialization.\ n);

返回0;

}

}

解决方案

在2007-10-14 18:38,mike b写道:


大家好, 在此先感谢您的帮助。我是C ++

模板的新手,并且使用成员函数

模板遇到了一些问题。我有一个包含模板的共享库,我试图从可执行文件中使用
,使用gcc 4.1.2进行编译。一切

工作正常,直到我尝试在非模板类中专门化一个静态成员函数

模板。我有一种感觉我搞砸了

显而易见的事情,所以在我发布一堆代码之前做了以下

看起来是否正确?谢谢。


---共享库(Arrays.h)---


名称空间示例{

class Arrays {

public:

template< class Tstatic int compareItems(T * a1,T * a2,int length);

protected:

数组(){};

};


模板< class Tint Arrays :: compareItems(T * a1,T * a2,int length){

printf(Calling generic function.\ n);

返回1;

}


模板< int Arrays :: compareItems< char>(char * a1,char * a2,int

长度){

printf(Calling char specialization.\ n);

返回0;

}

}


我可以看到它没有任何问题(除了使用printf而不包括

< stdio.h>)。您收到了什么错误消息以及您如何尝试使用该功能?


-

Erik Wikstr ?? m


10月14日下午12:38,mike b< michaeljber ... @ gmail.comwrote:


大家好,请提前感谢您的帮助。我是C ++

模板的新手,并且使用成员函数

模板遇到了一些问题。我有一个包含模板的共享库,我试图从可执行文件中使用
,使用gcc 4.1.2进行编译。一切

工作正常,直到我尝试在非模板类中专门化一个静态成员函数

模板。我有一种感觉我搞砸了

显而易见的事情,所以在我发布一堆代码之前做了以下

看起来是否正确?谢谢。


---共享库(Arrays.h)---


名称空间示例{

class Arrays {

public:

template< class Tstatic int compareItems(T * a1,T * a2,int length);

protected:

数组(){};


};


模板< class Tint Arrays :: compareItems(T * a1,T * a2,int length){

printf(Calling generic function.\ n);

返回1;


}


模板< int Arrays :: compareItems< char>(char * a1,char * a2,int

length){

printf(Calling char specialization.\ n);

返回0;


}

} - 隐藏引用的文字 -


- 显示引用的文字 -



您的专业化应该在示例中:数组类,而不是

在它之外。

名称空间例子e {

class Arrays {

public:

template< class Tstatic int compareItems(T * a1,T * a2,int

length);

static int compareItems(char * a1,char * a2,int

length);

};

一旦班级声明

关闭,你就无法添加新的专业化。如果compareItems是命名空间级别声明,那么你可以重新打开命名空间并稍后添加一个新的特化。

Lance


mike b写道:


大家好,请提前感谢您的帮助。我是C ++

模板的新手,并且使用成员函数

模板遇到了一些问题。我有一个包含模板的共享库,我试图从可执行文件中使用
,使用gcc 4.1.2进行编译。一切

工作正常,直到我尝试在非模板类中专门化一个静态成员函数

模板。我有一种感觉我搞砸了

显而易见的事情,所以在我发布一堆代码之前做了以下

看起来是否正确?谢谢。


---共享库(Arrays.h)---


名称空间示例{

class Arrays {

public:

template< class Tstatic int compareItems(T * a1,T * a2,int length);

protected:

数组(){};

};


模板< class Tint Arrays :: compareItems(T * a1,T * a2,int length){

printf(Calling generic function.\ n);

返回1;

}


模板< int Arrays :: compareItems< char>(char * a1,char * a2,int

长度){

printf(Calling char specialization.\ n);

返回0;

}

}


以下编辑可以很好地完成我的预期。有什么问题

你有吗?


#include< cstdio>

#include< iostream>


名称空间示例{

class Arrays {

public:

template< class Tstatic int compareItems(T * a1 ,T * a2,int length);

Arrays(){};

};


template< class Tint Arrays :: compareItems(T * a1,T * a2,int length){

std :: printf(" Calling generic function.\ n);

return 1;

}


模板< int Arrays :: compareItems< char>(char * a1,char * a2,int

长度){

std :: printf(Calling char specialization.\ n);

返回0;

} < br $>
}


int main()

{

std :: cout<< example :: Arrays :: compareItems((short *)0,(short *)0,0

)<< " \ n";

std :: cout<< example :: Arrays :: compareItems((char *)0,(char *)0,0)

<< " \ n";

}


Hello everyone, thanks in advance for your help. I''m new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I''m
trying to use from an executable, compile using gcc 4.1.2. Everything
works fine until I try specializing one of the static member function
templates in a non-template class. I have a feeling I''m messing up
something obvious so before I post a bunch of code does the following
look correct? Thanks.

--- Shared Library (Arrays.h) ---

namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};

template<class Tint Arrays::compareItems(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}

template<int Arrays::compareItems<char>(char *a1, char *a2, int
length) {
printf("Calling char specialization.\n");
return 0;
}
}

解决方案

On 2007-10-14 18:38, mike b wrote:

Hello everyone, thanks in advance for your help. I''m new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I''m
trying to use from an executable, compile using gcc 4.1.2. Everything
works fine until I try specializing one of the static member function
templates in a non-template class. I have a feeling I''m messing up
something obvious so before I post a bunch of code does the following
look correct? Thanks.

--- Shared Library (Arrays.h) ---

namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};

template<class Tint Arrays::compareItems(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}

template<int Arrays::compareItems<char>(char *a1, char *a2, int
length) {
printf("Calling char specialization.\n");
return 0;
}
}

I can see nothing wrong with it (except using printf and not including
<stdio.h>). What error messages are you getting and how are you trying
to use the function?

--
Erik Wikstr??m


On Oct 14, 12:38 pm, mike b <michaeljber...@gmail.comwrote:

Hello everyone, thanks in advance for your help. I''m new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I''m
trying to use from an executable, compile using gcc 4.1.2. Everything
works fine until I try specializing one of the static member function
templates in a non-template class. I have a feeling I''m messing up
something obvious so before I post a bunch of code does the following
look correct? Thanks.

--- Shared Library (Arrays.h) ---

namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};

};

template<class Tint Arrays::compareItems(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;

}

template<int Arrays::compareItems<char>(char *a1, char *a2, int
length) {
printf("Calling char specialization.\n");
return 0;

}
}- Hide quoted text -

- Show quoted text -

Your specialization should be inside the example:Arrays class, not
outside it.
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int
length);
static int compareItems(char*a1, char *a2, int
length);
};
You cant add a new specialization once the class declaration is
closed. If compareItems were a namespace level declaration, then you
can reopen the namespace and add a new specialization later.
Lance


mike b wrote:

Hello everyone, thanks in advance for your help. I''m new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I''m
trying to use from an executable, compile using gcc 4.1.2. Everything
works fine until I try specializing one of the static member function
templates in a non-template class. I have a feeling I''m messing up
something obvious so before I post a bunch of code does the following
look correct? Thanks.

--- Shared Library (Arrays.h) ---

namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};

template<class Tint Arrays::compareItems(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}

template<int Arrays::compareItems<char>(char *a1, char *a2, int
length) {
printf("Calling char specialization.\n");
return 0;
}
}

This below compiles fine and does what I think you expect. What problem
are you having ?

#include <cstdio>
#include <iostream>

namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
Arrays() {};
};

template<class Tint Arrays::compareItems(T *a1, T *a2, int length) {
std::printf("Calling generic function.\n");
return 1;
}

template<int Arrays::compareItems<char>(char *a1, char *a2, int
length) {
std::printf("Calling char specialization.\n");
return 0;
}
}

int main()
{
std::cout << example::Arrays::compareItems( (short*)0, (short*)0, 0
) << "\n";
std::cout << example::Arrays::compareItems( (char*)0, (char*)0, 0 )
<< "\n";
}


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

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