Alb*_*ert 8 c++ templates template-function
我有这个函数头:
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)
Run Code Online (Sandbox Code Playgroud)
这就是我使用它的方式:
OperateOnSurfaces<
true,
32, 32,
SDL_PixelFormat*, SDL_PixelFormat*,
GetPixel<true,32>, PutPixel<true,true,32> >(
bmpSrc->format, bmpDest->format,
bmpDest, bmpSrc, rDest, rSrc);
Run Code Online (Sandbox Code Playgroud)
这是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) { /* .. */ }
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
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> >();
}
Run Code Online (Sandbox Code Playgroud)
这个修改过的例子在C++ 98和C++ 11模式下在Clang 3.1和GCc 4.4.5上编译得很好,没有任何警告.如果我离开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()’
Run Code Online (Sandbox Code Playgroud)
† (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模式下不遵守这一点,它仍然只允许具有外部链接的功能.
| 归档时间: |
|
| 查看次数: |
8362 次 |
| 最近记录: |