[英]auto is not allowed here in C++ VS2017
bool isEven(int val) {
return val % 2 == 0;
}
bool isOdd(int val) {
return val % 2 != 0;
}
template<class Iterator>
int count_function(Iterator start, Iterator end, auto criteria) {
int count = 0;
for (; start != end; ++start) {
if (criteria(*start)) {
count++;
}
}
return count;
}
以上是我的代碼,在標准之前自動給出錯誤“現在允許此處自動”。 我想為這個函數提供 isEven /isOdd 標准。
這是為什么?
我試過 int, bool - 返回一些更多的問題。
函數參數中不允許使用關鍵字auto
。 如果要使用不同的數據類型,則需要使用模板。
template<class Iterator, class T>
int count_function(Iterator start, Iterator end, T criteria) {
int count = 0;
for (; start != end; ++start) {
if (criteria(*start)) {
count++;
}
}
return count;
}
普通函數參數中不允許使用 Auto 。 它只允許在 lambda 參數中使用。 C++20 將添加此功能:)
另請參閱“縮寫功能模板”,此處:
https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template
現在,您可能會逃避使用 lambda 聲明您的函數:
auto count_function = [](auto start, auto end, auto criteria)
{
int count = 0;
for (; start != end; ++start) {
if (criteria(*start)) {
count++;
}
}
return count;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.