多参数函数模板的别名
alias for multi parameter function template
我正在尝试为多参数函数创建模板,然后为特定实例化创建别名。从此真不错post:
C++11: How to alias a function?
我找到了适用于单个函数参数和单个模板参数的示例代码:
#include <iostream>
namespace Bar
{
void test()
{
std::cout << "Test\n";
}
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 中遇到这些错误:
error C2440: 'initializing' : cannot convert from 'void (__cdecl
*)(const T &,const T &)' to 'void (__cdecl &)(const int &,const int &)'
IntelliSense: a reference of type "void (&)(const int &, const int &)"
(not const-qualified) cannot be initialized with a value of type
""
我认为我完全遵循了扩展到 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>
中指定一次。
我正在尝试为多参数函数创建模板,然后为特定实例化创建别名。从此真不错post:
C++11: How to alias a function?
我找到了适用于单个函数参数和单个模板参数的示例代码:
#include <iostream>
namespace Bar
{
void test()
{
std::cout << "Test\n";
}
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 中遇到这些错误:
error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(const T &,const T &)' to 'void (__cdecl &)(const int &,const int &)'
IntelliSense: a reference of type "void (&)(const int &, const int &)" (not const-qualified) cannot be initialized with a value of type ""
我认为我完全遵循了扩展到 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>
中指定一次。