多参数函数模板的别名



我正在尝试为多参数函数创建一个模板,然后为特定实例创建一个别名。 从这篇非常好的帖子:

C++11: 如何为函数添加别名?

我找到了适用于单个函数参数和单个模板参数的示例代码:

#include <iostream>
namespace Bar
{
   void test()
   {
      std::cout << "Testn";
   }
   template<typename T>
   void test2(T const& a)
   {
      std::cout << "Test: " << a << std::endl;
   }
}
void (&alias)()        = Bar::test;
void (&a2)(int const&) = Bar::test2<int>;
int main()
{
    Bar::test();
    alias();
    a2(3);
}

当我尝试扩展为两个函数参数时:

void noBarTest(T const& a, T const& b)
{
    std::cout << "noBarTest: " << a << std::endl;
}
void(&hh)(int const&, int const&) = noBarTest<int, int>;

我在Visual Studio中收到以下错误:

错误 C2440:"正在初始化":无法从"void (__cdecl *)(const T &,const T &)' to 'void (__cdecl &)(const int &,const int &)'

智能感知:类型为"void (&)(const int &, const int &)"的引用 (非常量限定)不能使用类型值初始化 ""

我以为我完全遵循了扩展到 2 个参数的模式。
正确的语法是什么?

template <typename T>
void noBarTest(T const& a, T const& b)
{
}
void(&hh)(int const&, int const&) = noBarTest<int>; // Only once
int main() {
  return 0;
}

类型参数int只需要在noBarTest<int>中指定一次。

最新更新