The following code is failing to compile with
error C3497: you cannot construct an instance of a lambda`:
auto hasher = [&](const cv::Vec3b& color) -> size_t {
std::hash<int> int_hasher;
return int_hasher(color[0]) + int_hasher(color[1]) + int_hasher(color[2]);
// the above code is probably a wrong way of constructing a hash,
// but I'd like to get it to compile first
};
std::unordered_map<cv::Vec3b, int, decltype(hasher)> color_counts(10, hasher);
I noticed sometimes this happens when headers are not included. These are the included headers:
#include <unordered_map>
#include <functional>
#include "opencv2/core/core.hpp"
Note: I use the same technique for a comparator for priority queues in VS 2013, and it works. I see there's an alternative using std::function but I'd like to make this method work.
EDIT: Complete error log
error C3497: you cannot construct an instance of a lambda
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(164) : while compiling class template member function 'OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824> std::_Hash_oper1<false,_Hasher>::_Gethash(void) const'
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(242) : see reference to function template instantiation 'OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824> std::_Hash_oper1<false,_Hasher>::_Gethash(void) const' being compiled
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(198) : see reference to class template instantiation 'std::_Hash_oper1<false,_Hasher>' being compiled
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(220) : see reference to class template instantiation 'std::_Hash_oper2<false,_Hasher,_Keyeq>' being compiled
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\unordered_map(23) : see reference to class template instantiation 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(255) : see reference to class template instantiation 'std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Ty=int
2> , _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> , _Alloc=std::allocator<std::pair<const cv::Vec3b,int>>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\unordered_map(81) : see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Ty=int
2> , _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> , _Alloc=std::allocator<std::pair<const cv::Vec3b,int>>
2> ]
2> oslsegmentation.cpp(42) : see reference to class template instantiation 'std::unordered_map<cv::Vec3b,int,OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Ty=int
2> ]
What am I doing wrong here?
Figured it out after Kerrek's comment:
change this:
to (note the reference):