Reputation: 7931
Why does this fail to compile in G++ 5.3.1 (with --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; } );
}
This fails with the message:
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; } );
^
(Note line numbers are off by 8 due to some header comments which I've omitted).
But if I remove all the const
qualifiers then it compiles fine.
Upvotes: 0
Views: 447
Reputation: 4850
I think you want const typename ...
instead of typename const ...
:)
Upvotes: 2