#include <iostream>
template <typename T>
inline
T accum (T const* beg, T const* end)
{
T total = T(); // assume T() actually creates a zero value
while (beg != end) {
total += *beg;
++beg;
}
return total;
}
int main()
{
// create array of 5 integer values
int num[]={1,2,3,4,5};
// print average value
std::cout << "the average value of the integer values is "
<< accum(&num[0], &num[5]) / 5
<< '\n';
// create array of character values
char name[] = "templates";
int length = sizeof(name)-1;
// (try to) print average character value
std::cout << "the average value of the characters in \""
<< name << "\" is "
<< accum(&name[0], &name[length]) / length
//<< accum<int>(&name[0], &name[length]) / length //but this give me error
<< '\n';
}
私はC ++テンプレートを読んでいました:完全なガイドブックで、著者はテンプレートの特殊化を使用できると述べ
accum<int>(&name[0], &name[length]) / length
ましたVisual Studio 2012でこれを試してエラー
main.cpp(34)を取得しました:エラーC2664: 'accum' : cannot convert parameter 1 from ' char *' to 'const int *'
私の C++ は少しさびています。
この「動作」が以前は許可されていたが、「最新の」C++ 標準に変更があり、これが違法になっているのか、それとも私が読んでいる本のエラーなのか、興味があります。