简体   繁体   English

C ++:候选模板被忽略:模板参数的显式指定参数无效

[英]C++: candidate template ignored: invalid explicitly-specified argument for template parameter

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 : 这是GetPixelPutPixel

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. 当你声明一个自由函数static ,它会获得内部链接。 Non-type template parameters, in C++03, must have external linkage . C ++ 03中的非类型模板参数必须具有外部链接 Just remove static in front of the functions. 只需删除函数前面的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> >();
}

This modified example compiles fine on Clang 3.1 and GCc 4.4.5 in C++98 and C++11 mode, no warnings. 这个修改过的例子在C ++ 98和C ++ 11模式下在Clang 3.1和GCc 4.4.5上编译得很好,没有任何警告。 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"): 如果我保留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++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现在改变了措辞并允许具有内部链接的功能:

(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 [...] 一个常量表达式(5.19),用于指定具有静态存储持续时间和外部或内部链接的对象的地址, 或具有外部或内部链接的函数 [...]

  • [...] [...]

Clang does currently not obey to this in C++11 mode, it still only allows functions with external linkage. Clang目前在C ++ 11模式下不遵守这一点,它仍然只允许具有外部链接的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 模板错误:“模板参数的显式指定参数无效” - C++ templating error: "invalid explicitly-specified argument for template parameter" 模板参数constexpr的显式指定参数无效 - Invalid explicitly-specified argument for template parameter which is constexpr 显式指定的模板参数包 - Explicitly-specified template parameter packs C ++-为什么忽略带有函数指针参数的候选模板? - C++ - Why is this candidate template with a function pointer parameter ignored? C ++:模板函数,显式指定引用类型作为类型参数 - C++: template function with explicitly specified reference type as type parameter C ++候选模板传递lambda作为std :: function的参数时忽略错误 - C++ Candidate Template Ignored error when passing lambda as argument for std::function C ++使用模板进行整数类型,候选模板被忽略 - C++ working with templates for integral types, candidate template is ignored “候选模板被忽略:无法匹配......”用于模板函数的模板函数参数 - "candidate template ignored: could not match ..." for template function argument to template function 在Clang中无效的显式指定参数,但在gcc中成功编译-谁错了? - Invalid explicitly-specified argument in clang but successful compilation in gcc — who's wrong? 没有完全指定的模板作为C ++中的模板参数 - Not fully specified template as template argument in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM