c++ - Why cannot clang++ deduce the type of a map of lambdas? -


i have following piece of code:

enum relationaloperator { lt, lte, eq, gte, gt }; std::map<relationaloperator, bool (*)(const point&, const point&)> ops = {     { gte, [](const point& a, const point& b) { return >= b; } },     { lte, [](const point& a, const point& b) { return <= b; } },     { eq, [](const point& a, const point& b) { return == b; } },     { gt, [](const point& a, const point& b) { return > b; } },     { lt, [](const point& a, const point& b) { return < b; } }, }; 

this code inside template , point template parameter.

i tried replace type of variable ops auto clang++ says:

src/utils.hpp:47:10: error: cannot deduce actual type variable 'ops' type 'auto' initializer list 

why that? thought keyword auto these kinds of situations, type long , obvious.

first of all, each lambda has own type, given set of different lambdas, cannot factor them single type without manual casting (typically embedding them in std::function<r(args...)> objects).

then, when write such initialization:

enum relationaloperator { lt, lte, eq, gte, gt }; std::map<relationaloperator, bool (*)(const point&, const point&)> ops = {     { gte, [](const point& a, const point& b) { return >= b; } },     { lte, [](const point& a, const point& b) { return <= b; } },     { eq, [](const point& a, const point& b) { return == b; } },     { gt, [](const point& a, const point& b) { return > b; } },     { lt, [](const point& a, const point& b) { return < b; } }, }; 

what happening? calls std::initializer_list constructor of std::map<relationaloperator, bool (*)(const point&, const point&)>. able deduce given braced expression initializer list such map:

std::initializer_list<std::pair<relationaloperator, bool (*)(point const&, point const&)>> 

then, implicit conversion occur lambdas.

now if write instead:

auto ops = {     { gte, [](const point& a, const point& b) { return >= b; } },     { lte, [](const point& a, const point& b) { return <= b; } },     { eq, [](const point& a, const point& b) { return == b; } },     { gt, [](const point& a, const point& b) { return > b; } },     { lt, [](const point& a, const point& b) { return < b; } }, }; 

it can't figure out kind of object represented braced expression (the t in std::initializer_list<t>). quite explicit in gcc's error message:

main.cpp:8:29: error: unable deduce 'std::initializer_list<auto>' '{{1, 2}, {3, 4}}'       auto x = {{1, 2}, {3, 4}};                               ^  main.cpp:8:29: note:   couldn't deduce template parameter 'auto' 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -