我正在尝试编写一个函数模板,该函数模板可以为简单的查找表推断出大部分参数。
然而,我的努力始终会导致编译器错误。 到目前为止,这是我最好的尝试:
// A simple templated struct that maps one key to one value
template<typename KT, typename VT>
struct LookupTable
{
KT key;
VT value;
};
// A lookup function that searches an array for a match.
// returns NULL if key was not found.
// returns pointer to matching value when key is found.
//
// The first parameter is a Reference to an Array of T, with a specific size N
// In this example, the size is 12.
template<typename T, size_t N, typename KT, typename VT>
VT* Find(T<KT,VT> (&A)[N], KT key) // <== Multiple Errors on this line
{
VT* pFoundValue = NULL;
for (size_t i = 0; i < N; ++i)
{
if (key == A[i].key)
{
pFoundValue = &(A[i].value);
break;
}
}
return pFoundValue;
}
// Test the function with a simple example
int main(void)
{
LookupTable<std::string, int> calendar[] = {
{ "January", 31 },
{ "February", 28 },
{ "March", 31 },
{ "April", 30 },
{ "May", 31 },
{ "June", 30 },
{ "July", 31 },
{ "August", 31 },
{ "September", 30 },
{ "October", 31 },
{ "November", 30 },
{ "December", 31 }
};
const int* pDays = Find(calendar, std::string("May"));
if (pDays == NULL)
{
cout << "Invalid Month" << endl;
}
else
{
cout << "The month of May has " << *pDays << " Days" << endl;
}
_getch();
return 0;
}
我得到的错误都在Find
函数的声明上
(标有评论):
1>error C2143: syntax error : missing ')' before '<'
1>error C2143: syntax error : missing ';' before '<'
1>error C2988: unrecognizable template declaration/definition
1>error C2059: syntax error : '<'
1>error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我看起来似乎很容易通过函数调用推断出所有参数。
我是否有语法错误,我不知道?
答案 0 :(得分:3)
在此:
template<typename T, size_t N, typename KT, typename VT>
VT* Find(T<KT,VT> (&A)[N], KT key)
您正在尝试使用T
作为模板模板参数。语法是:
template<template <class, class> class T, size_t N, typename KT, typename VT>
VT* Find(T<KT,VT> (&A)[N], KT key)
答案 1 :(得分:1)
而不是
template<typename T, size_t N, typename KT, typename VT>
VT* Find(T<KT,VT> (&A)[N], KT key)
使用
template<template <typename, typename> class T, size_t N, typename KT, typename VT>
//^^^^^^^^^^^^^^^^^^^ Need to indicate that T is a class template
VT* Find(T<KT,VT> (&A)[N], KT key)
答案 2 :(得分:1)
这是一个更紧凑,SFINAE友好的版本,可以编译。请注意,我已声明了数组参数const
,因为它无需修改。
#include <type_traits>
template<typename PairT,
typename KeyT = decltype(std::declval<PairT>().key),
typename ValueT = decltype(std::declval<PairT>().value),
std::size_t N>
const ValueT *
Find(const PairT (&array)[N], KeyT key)
{
for (auto& iter : array)
if (key == iter.key)
return &(iter.value);
return nullptr;
}
当然,这可以使用STL算法进一步简化,这也将避免为查找键创建std::string
临时值。
auto pred = [](const LookupTable<std::string, int>& cal){
return cal.key == "May";
};
auto pDays = std::find_if(std::begin(calendar), std::end(calendar), pred);
if (pDays == std::end(calendar))
std::cout << "Invalid Month" << std::endl;
else
std::cout << "May has " << (*pDays).value << " days" << std::endl;
答案 3 :(得分:0)
请尝试以下格式:
template<size_t N, typename KT, typename VT, template<typename, typename> class T>