【问题标题】:Passing function template specializations to a variadic template function将函数模板特化传递给可变参数模板函数
【发布时间】:2011-11-15 16:38:54
【问题描述】:

将函数模板特化的地址传递给常规模板函数没有问题:

template <typename T>
void f(T) {}

template <typename A, typename B>
void foo(A, B) {}

int main()
{
    foo(&f<int>, &f<float>);
}

但是,当我尝试将相同的特化传递给可变参数模板时:

template <typename T>
void f(T) {}

template <typename... A>
void bar(A...) {}

int main()
{
    bar(&f<int>, &f<float>);
}

使用 GCC 时出现以下编译器错误(我尝试了 4.6.1 和 4.7.0):

test.cpp: In function 'int main()':
test.cpp:9:27: error: no matching function for call to 'bar(<unresolved overloaded function type>, <unresolved overloaded function type>)'
test.cpp:9:27: note: candidate is:
test.cpp:5:6: note: template<class ... A> void bar(A ...)
test.cpp:5:6: note:   template argument deduction/substitution failed:

为什么会出现这些错误?

【问题讨论】:

  • auto a = &amp;f&lt;int&gt; 也不起作用:error: 'a' has incomplete type
  • 这可行:bar((void(*)(int))f&lt;int&gt;, (void(*)(double))f&lt;double&gt;); 但显然这不是解决方案。它只是意味着(就像错误所说的那样)由于某种原因它无法分辨&amp;f&lt;int&gt; 是什么类型。

标签: c++ templates c++11 variadic-templates template-function


【解决方案1】:

看起来可能是bug in GCC that is possibly fixed in GCC 4.6.2(我说可能是因为它不完全相同,但确实涉及获取可变参数模板函数的地址)。

【讨论】:

  • 不错的发现!这看起来几乎可以肯定是同一个问题。 (我会在构建更新的 4.7.0 后验证)。
  • 确认,同样的问题。
猜你喜欢
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 2021-11-10
  • 1970-01-01
  • 2015-09-10
相关资源
最近更新 更多