C ++:候选模板已忽略:模板参数的显式指定参数无效 [英] C++: candidate template ignored: invalid explicitly-specified argument for template parameter
问题描述
我有这个函数头:
template<
bool src_alpha,
int sbpp,int dbpp,
typename T1,typename T2,
Color(* getFunc)(T1 data,Uint8 * addr),
(* putFunc)(T2数据,Uint8 * addr,Color c)
>
static void OperateOnSurfaces(T1 data1,T2 data2,SDL_Surface * bmpDest,SDL_Surface * bmpSrc,SDL_Rect& rDest,SDL_Rect& rSrc)
这是我如何使用它:
OperateOnSurfaces<
true,
32,32,
SDL_PixelFormat *,SDL_PixelFormat *,
GetPixel ,PutPixel bmpSrc-> format,bmpDest-> format,
bmpDest,bmpSrc,rDest,rSrc)
这是 GetPixel
和 PutPixel
:
模板< bool alpha,int bpp>
static Color GetPixel(SDL_PixelFormat * format,Uint8 * addr){/ * .. * /}
template< bool alpha,bool alphablend,int bpp>
static void PutPixel(SDL_PixelFormat * format,Uint8 * addr,Color col){/ * .. * /}
b $ b
我得到这个错误:
注意:候选模板被忽略:模板参数'getFunc' [3]
为什么?
p>我怀疑所有这些功能是自由功能。当你声明一个自由函数 static
时,它获得内部链接。非类型模板参数,在C ++ 03中,必须具有外部链接†。只需删除 static
前面的函数。
bool src_alpha,
int sbpp,int dbpp,
typename T1,typename T2,
char(* getFunc)(T1 data,unsigned * addr),
void (* putFunc)(T2 data,unsigned * addr,char c)
>
void OperateOnSurfaces(){}
template< bool alpha,int bpp>
char GetPixel(void * format,unsigned * addr);
template< bool alpha,bool alphablend,int bpp>
void PutPixel(void * format,unsigned * addr,char col);
int main(){
OperateOnSurfaces<
true,
32,32,
void *,void *,
GetPixel ,PutPixel }
此修改示例在C ++中编译Clang 3.1和GCc 4.4.5, +98和C ++ 11模式,没有警告。如果我离开 static
,我得到一个类似的错误+注释,你得到的Clang,和GCC吐出重要信息(滚动到右边,没有外部链接):
15:02:38 $ g ++ t.cpp
t.cpp: int main()':
t.cpp:21:error:'GetPixel< true,32>'不是类型'char(*) 'char GetPixel(void *,unsigned int *)[with bool alpha = true,int bpp = 32]'没有外部链接
t.cpp:21:error:'PutPixel< true,true,32&对于类型'void(*)(void *,unsigned int *,char)'不是有效的模板参数,因为函数'void PutPixel(void *,unsigned int *,char)[with bool alpha = true,bool alphablend = true ,int bpp = 32]'没有外部链接
t.cpp:21:错误:没有匹配的函数调用'OperateOnSurfaces()'
†(C ++ 03)§14.3.2[temp.arg.nontype] p1
一个非模板的模板参数 > template-parameter 应为以下之一:
[...]
>
具有外部连结的物件或函式的地址 [...]
$ b[...]
C ++ 11改变了写法,现在允许内部链接的函数:
(C ++ 11)§14.3.2[ arg.nontype] p1
一个模板参数 ,非模板模板参数应为以下之一:
[...] / p>
常量表达式(5.19),指定具有静态存储持续时间和外部或内部链接 [...]
- [...] $ b
Clang目前在C ++ 11模式下不服从,它仍然只允许具有外部链接的函数。
I have this function header:
template <
bool src_alpha,
int sbpp, int dbpp,
typename T1, typename T2,
Color (*getFunc)(T1 data, Uint8* addr),
void (*putFunc)(T2 data, Uint8* addr, Color c)
>
static void OperateOnSurfaces(T1 data1, T2 data2, SDL_Surface * bmpDest, SDL_Surface * bmpSrc, SDL_Rect& rDest, SDL_Rect& rSrc)
This is how I use it:
OperateOnSurfaces<
true,
32, 32,
SDL_PixelFormat*, SDL_PixelFormat*,
GetPixel<true,32>, PutPixel<true,true,32> >(
bmpSrc->format, bmpDest->format,
bmpDest, bmpSrc, rDest, rSrc);
This is GetPixel
and PutPixel
:
template<bool alpha, int bpp>
static Color GetPixel(SDL_PixelFormat* format, Uint8* addr) { /* .. */ }
template<bool alpha, bool alphablend, int bpp>
static void PutPixel(SDL_PixelFormat* format, Uint8* addr, Color col) { /* .. */ }
And I get this error:
note: candidate template ignored: invalid explicitly-specified argument for template parameter 'getFunc' [3]
Why?
I suspect that all those functions are free functions. When you declare a free function static
, it gains internal linkage. Non-type template parameters, in C++03, must have external linkage†. Just remove static
in front of the functions.
template <
bool src_alpha,
int sbpp, int dbpp,
typename T1, typename T2,
char (*getFunc)(T1 data, unsigned* addr),
void (*putFunc)(T2 data, unsigned* addr, char c)
>
void OperateOnSurfaces(){}
template<bool alpha, int bpp>
char GetPixel(void* format, unsigned* addr);
template<bool alpha, bool alphablend, int bpp>
void PutPixel(void* format, unsigned* addr, char col);
int main(){
OperateOnSurfaces<
true,
32, 32,
void*, void*,
GetPixel<true,32>, PutPixel<true,true,32> >();
}
This modified example compiles fine on Clang 3.1 and GCc 4.4.5 in C++98 and C++11 mode, no warnings. If I leave the static
in, I get a similar error + note to what you got with Clang, and GCC spits out the vital information (scroll to the right, "has not external linkage"):
15:02:38 $ g++ t.cpp
t.cpp: In function ‘int main()’:
t.cpp:21: error: ‘GetPixel<true, 32>’ is not a valid template argument for type ‘char (*)(void*, unsigned int*)’ because function ‘char GetPixel(void*, unsigned int*) [with bool alpha = true, int bpp = 32]’ has not external linkage
t.cpp:21: error: ‘PutPixel<true, true, 32>’ is not a valid template argument for type ‘void (*)(void*, unsigned int*, char)’ because function ‘void PutPixel(void*, unsigned int*, char) [with bool alpha = true, bool alphablend = true, int bpp = 32]’ has not external linkage
t.cpp:21: error: no matching function for call to ‘OperateOnSurfaces()’
† (C++03) §14.3.2 [temp.arg.nontype] p1
A template-argument for a non-type, non-template template-parameter shall be one of:
[...]
the address of an object or function with external linkage [...]
[...]
Note that C++11 changed the wording and allows functions with internal linkage too now:
(C++11) §14.3.2 [temp.arg.nontype] p1
A template-argument for a non-type, non-template template-parameter shall be one of:
[...]
a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage [...]
- [...]
Clang does currently not obey to this in C++11 mode, it still only allows functions with external linkage.
这篇关于C ++:候选模板已忽略:模板参数的显式指定参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!