c++ - std::function 模板参数中的 const

标签 c++ templates c++11

为什么无法在 G++ 5.3.1 中编译(使用 --std=c++1y):

#include <iostream>
#include <vector>
#include <functional>

using std::cout;
using std::endl;

template<typename InputIterator>
void foo(InputIterator i, std::function<bool(typename const std::iterator_traits<InputIterator>::value_type&)> f)
{
    cout << f(*i) << endl;
}

int main() {
    std::vector<int> v { 1,2,3 };
    foo(v.begin(), [] (const int& a) -> bool { return false; } );
}

失败并显示消息:

g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"source.d" -MT"source.o" -o "source.o" "../source.cc"
../source.cc:16:110: error: template argument 1 is invalid
 void foo(InputIterator i, std::function<bool(typename const std::iterator_traits<InputIterator>::value_type&)> f)
                                                                                                              ^
../source.cc:16:110: error: template argument 1 is invalid
../source.cc:16:110: error: template argument 1 is invalid
../source.cc:16:110: error: template argument 1 is invalid
../source.cc:16:110: error: template argument 1 is invalid
../source.cc:16:32: error: ‘std::function’ is not a type
 void foo(InputIterator i, std::function<bool(typename const std::iterator_traits<InputIterator>::value_type&)> f)
                                ^
../source.cc:16:40: error: expected ‘,’ or ‘...’ before ‘<’ token
 void foo(InputIterator i, std::function<bool(typename const std::iterator_traits<InputIterator>::value_type&)> f)
                                        ^
../source.cc: In function ‘int main()’:
../source.cc:23:61: error: no matching function for call to ‘foo(std::vector<int>::iterator, main()::<lambda(const int&)>)’
  foo(v.begin(), [] (const int& a) -> bool { return false; } );
                                                             ^
../source.cc:16:6: note: candidate: template<class InputIterator> void foo(InputIterator, int)
 void foo(InputIterator i, std::function<bool(typename const std::iterator_traits<InputIterator>::value_type&)> f)
      ^
../source.cc:16:6: note:   template argument deduction/substitution failed:
../source.cc:23:61: note:   cannot convert ‘<lambda closure object>main()::<lambda(const int&)>{}’ (type ‘main()::<lambda(const int&)>’) to type ‘int’
  foo(v.begin(), [] (const int& a) -> bool { return false; } );
                                                             ^

(由于我省略了一些标题注释,请注意行号减少了 8)。

但如果我删除所有 const 限定符,那么它可以正常编译。

最佳答案

我想你想要 const typename ... 而不是 typename const ... :)

关于c++ - std::function 模板参数中的 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38853761/

相关文章:

c++ getchar 在 vs 2010 中有效,在 2012 中无效

c++ - C++03 中的模板函数返回类型推导

templates - SFINAE:检查两个可变参数包的串联是否与一个包相同

c++ - 可以通过模板间接访问基类中的私有(private)类型

c++ - 在特定时间更改 QLabel 的背景颜色

c++ - C++ 中是否有等效的 readline().split() python 函数?

c++ - 警告 : deprecated conversion from string constant to 'char*' '

c++ - 如何将具有不同参数的 std::function 传递给同一函数

C++:使用用户定义的哈希/相等性帮助创建 unordered_map

c++ - 使用 vector 的 vector 在 opengl 中绘制地面