C-callback to function template: explicitly instantiate template

后端 未结 3 381
温柔的废话
温柔的废话 2020-12-10 03:18

Premise

I’m using a C library (from C++) which provides the following interface:

void register_callback(void* f, void* data);
void invoke_callback(         


        
3条回答
  •  盖世英雄少女心
    2020-12-10 03:53

    Apparently, the real problem was the missing static_cast in my original code:

    register_callback(reinterpret_cast(&my_callback), &ft);
    

    This compiles fine, but triggers the liker error when using GCC 4.5. It doesn’t even compile when using GCC 4.2, instead giving the following compile error:

    insufficient contextual information to determine type

    Once this “contextual information” is provided, the code compiles and links:

    register_callback(reinterpret_cast(
        static_cast(my_callback)), &value);
    

    I’ve got no idea whether the cast is actually required and (if so) why GCC 4.5 allows me to leave it off, and then fails to instantiate the template. But at least I got the code to compile without resorting to hacks.

提交回复
热议问题