std::map 具有不同函数类型的值

std::map with values of different function-types

我想使用 std::map 将某些类型的函数(某些特定的参数类型)作为可能的值。但是,以下(最小)示例无法编译。

这是为什么?我如何允许 FunctionType<bool>FunctionType<int> 作为地图的可能值?

#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <variant>


template <typename T>
using FunctionType = std::function<T(T)>;


int main()
{
  auto myMap = std::map<std::string, std::variant<FunctionType<bool>, FunctionType<int>>>
    {
     {"foo", [](bool x){return x;}},
     {"bar", [](int x){return x;}},
    };

  return 0;
}

错误信息:


$ g++ --std=c++2a -o /tmp/variant-test /tmp/variant-test.cpp && /tmp/variant-test

variant-test.cpp: In function ‘int main()’:
variant-test.cpp:18:5: error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > >::map(<brace-enclosed initializer list>)’
   18 |     };
      |     ^
In file included from /usr/include/c++/10/map:61,
                 from variant-test.cpp:3:
/usr/include/c++/10/bits/stl_map.h:290:2: note: candidate: ‘template<class _InputIterator> std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator, const _Compare&, const allocator_type&) [with _InputIterator = _InputIterator; _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  290 |  map(_InputIterator __first, _InputIterator __last,
      |  ^~~
/usr/include/c++/10/bits/stl_map.h:290:2: note:   template argument deduction/substitution failed:
variant-test.cpp:18:5: note:   candidate expects 4 arguments, 2 provided
   18 |     };
      |     ^
In file included from /usr/include/c++/10/map:61,
                 from variant-test.cpp:3:
/usr/include/c++/10/bits/stl_map.h:273:2: note: candidate: ‘template<class _InputIterator> std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  273 |  map(_InputIterator __first, _InputIterator __last)
      |  ^~~
/usr/include/c++/10/bits/stl_map.h:273:2: note:   template argument deduction/substitution failed:
variant-test.cpp:18:5: note:   couldn’t deduce template parameter ‘_InputIterator’
   18 |     };
      |     ^
In file included from /usr/include/c++/10/map:61,
                 from variant-test.cpp:3:
/usr/include/c++/10/bits/stl_map.h:256:2: note: candidate: ‘template<class _InputIterator> std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = _InputIterator; _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  256 |  map(_InputIterator __first, _InputIterator __last,
      |  ^~~
/usr/include/c++/10/bits/stl_map.h:256:2: note:   template argument deduction/substitution failed:
variant-test.cpp:18:5: note:   candidate expects 3 arguments, 2 provided
   18 |     };
      |     ^
In file included from /usr/include/c++/10/map:61,
                 from variant-test.cpp:3:
/usr/include/c++/10/bits/stl_map.h:250:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(std::initializer_list<std::pair<const _Key, _Tp> >, const allocator_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  250 |       map(initializer_list<value_type> __l, const allocator_type& __a)
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:250:40: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::initializer_list<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >’
  250 |       map(initializer_list<value_type> __l, const allocator_type& __a)
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_map.h:244:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(std::map<_Key, _Tp, _Compare, _Alloc>&&, const allocator_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  244 |       map(map&& __m, const allocator_type& __a)
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:244:17: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::map<std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > >&&’
  244 |       map(map&& __m, const allocator_type& __a)
      |           ~~~~~~^~~
/usr/include/c++/10/bits/stl_map.h:240:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&, const allocator_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  240 |       map(const map& __m, const allocator_type& __a)
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:240:22: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const std::map<std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > >&’
  240 |       map(const map& __m, const allocator_type& __a)
      |           ~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_map.h:236:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(const allocator_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  236 |       map(const allocator_type& __a)
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:236:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/10/bits/stl_map.h:228:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(std::initializer_list<std::pair<const _Key, _Tp> >, const _Compare&, const allocator_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  228 |       map(initializer_list<value_type> __l,
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:228:40: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::initializer_list<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >’
  228 |       map(initializer_list<value_type> __l,
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_map.h:215:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  215 |       map(map&&) = default;
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:215:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/10/bits/stl_map.h:207:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  207 |       map(const map&) = default;
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:207:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/10/bits/stl_map.h:194:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(const _Compare&, const allocator_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  194 |       map(const _Compare& __comp,
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:194:27: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const std::less<std::__cxx11::basic_string<char> >&’
  194 |       map(const _Compare& __comp,
      |           ~~~~~~~~~~~~~~~~^~~~~~
/usr/include/c++/10/bits/stl_map.h:185:7: note: candidate: ‘std::map<_Key, _Tp, _Compare, _Alloc>::map() [with _Key = std::__cxx11::basic_string<char>; _Tp = std::variant<std::function<bool(bool)>, std::function<int(int)> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::variant<std::function<bool(bool)>, std::function<int(int)> > > >]’
  185 |       map() = default;
      |       ^~~
/usr/include/c++/10/bits/stl_map.h:185:7: note:   candidate expects 0 arguments, 2 provided

这与单用户自定义转换规则无关。通过以您的示例并将 FunctionType<int> 替换为 FunctionType<std::string> 很容易看出这一点 - 一切都编译。

variant<X, Y> 可从所有转换为 XY 的内容隐式转换, 前提是 它可以明确确定要转换的类型到。附带条件很重要,因为 std::function 不需要完全匹配:std::function<bool(bool)> 接受可以用 bool 和 returns 调用的所有东西,可以转换为 bool。显然 [](bool x) { return x; } 满足这个要求,但是 [](int x) { return x; } 也满足。同样,这两个 lambda 都可以转换为 std::function<int(int)>,因为它们都可以用 int 调用以生成可转换为 int.

的内容

由于 lambda 可以转换为两种选择,并且两者之间没有顺序,variant 将拒绝从它转换。这就是为什么将 FunctionType<int> 替换为 FunctionType<std::string> 使其可以编译 - 在这种情况下只有 bool 替代方案可行。