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),
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)
我是这样使用的:
OperateOnSurfaces<
true,
32, 32,
SDL_PixelFormat*, SDL_PixelFormat*,
GetPixel<true,32>, PutPixel<true,true,32> >(
bmpSrc->format, bmpDest->format,
bmpDest, bmpSrc, rDest, rSrc);
这是GetPixel
和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) { /* .. */ }
我得到这个错误:
note: candidate template ignored: invalid explicitly-specified argument for template parameter 'getFunc' [3]
为什么?
我怀疑所有这些函数都是自由函数。当您声明一个自由函数static
时,它将获得内部链接。在c++ 03中,非类型模板形参必须有外部链接†。只需删除函数前面的static
。
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> >();
}
这个修改后的例子在Clang 3.1和GCc 4.4.5在c++ 98和c++ 11模式下可以很好地编译,没有警告。如果我把static
留在里面,我会得到与Clang类似的错误+注释,并且GCC会吐出重要信息(向右滚动,"没有外部链接"):
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
对于非类型、非模板的模板形参, 模板形参应该是:
[…]
具有外部链接的对象或函数的地址[…]
[…]
注意c++ 11改变了措辞,现在也允许函数有内部链接了:
(C++11) §14.3.2 [temp.arg.nontype] p1
对于非类型、非模板的模板形参, 模板形参应该是:
[…]
一个常量表达式(5.19),用于指定具有静态存储时间和外部或内部链接的对象的地址,或者具有外部或内部链接[…]
的函数的地址。[…]
Clang目前在c++ 11模式下不遵守这个,它仍然只允许带有外部链接的函数
相关文章:
- std::cin >>上的参数无效
- 结果失败或多个参数无效
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- 错误 带有模块的一元"*"(具有"int")的类型参数无效
- 由于调用 std::condition_variable 后参数无效而导致应用程序崩溃
- MPI_Iprobe:RMA 调用中的位移参数无效
- 推力::sort_by_key上的配置参数无效
- windres 致命错误:将输出写入时:参数无效
- 一元'*'的类型参数无效(有双精度)
- C++:传递给 C 运行时函数的参数无效
- 矢量模板参数无效
- 写入 -: 参数无效 } 时出错
- winsock2:recvfrom()函数以错误10022(参数无效)结束
- 模板参数的显式指定参数无效,该参数是 constexpr
- 模板参数无效
- 调用 getter 成员时的参数无效
- 模板模板参数中的参数无效
- 方法调用中的参数无效
- POSIX 计时器:参数无效
- C++类模板是一个模板:模板参数无效