使用指向模板函数的指针时出现VC++错误

Pab*_*blo 7 c++ templates function-pointers visual-c++

我正在尝试为libcurl编写模板回调函数.但是,当使用指向模板函数实例的指针时,VC++ 2008和2010会一直给我这个错误:

template-callback.cpp(27):错误C2664:'curl_easy_setopt':无法将参数3从'size_t(__ cdecl*)(void*,size_t,size_t,void*)'转换为'...'上下文不允许消除过载功能的歧义

但GCC(4.5.1)编译代码没有问题.这是代码的修剪版本:

#include <string>

template<typename String>
size_t callback(
    void*       ptr
  , size_t  size
  , size_t  nmemb
  , void*       userdata
)
{
  //String* str = static_cast<String*>(userdata);
  size_t    len = size*nmemb;
  //str->append(static_cast<char const*>(ptr), len);
  return len;
}

typedef size_t (*write_callback)(void*, size_t, size_t, void*);
struct CURL;
enum CURLOption { none };
void curl_easy_setopt(CURL*, CURLOption, ...);

void f()
{
  CURL* curl = NULL;
  CURLOption option = none;

  // This gives an error
  curl_easy_setopt(curl, option, &callback<std::string>);

  // This doesn't
  write_callback cb = &callback<std::string>;
  curl_easy_setopt(curl, option, cb);
}
Run Code Online (Sandbox Code Playgroud)

这是VC++中的错误还是我做错了什么?

Mat*_* M. 1

我在ideone上重现了这个问题(C++03 with gcc-4.3.4):

#include <iostream>

typedef void (*FuncType)(int);

void foo(FuncType);
void bar(...);

template <typename T>
void callback(T t) { std::cout << t << "\n"; }

int main() {
  foo(&callback<int>); // OK
  bar(static_cast<FuncType>(&callback<int>)); // OK
  bar(&callback<int>); // error: address of overloaded function
                       // with no contextual type information
}
Run Code Online (Sandbox Code Playgroud)

该问题似乎来自可变参数和函数指针的交互。

注意:在使用 gcc-4.5.1 的 C++0x 模式下,它工作正常

我推测问题来自bar(或curl_easy_setopt在您的情况下)的重载解析。

问题是,为了使用省略号,编译器决定如何传递参数:int, double, pointer, ... 似乎它无法自行决定 的类型&callback<int>是什么。

当我们使用foo或执行强制转换时,它是明确的,因为没有选择。

我怀疑存在转换问题,但我没有可以深入研究的 C++03 标准版本。