serge-sans-paille / frozen

a header-only, constexpr alternative to gperf for C++14 users
Apache License 2.0
1.31k stars 104 forks source link

CTAD issue using frozen::map #168

Open pfee opened 9 months ago

pfee commented 9 months ago

I'm unable to use class template argument deduction with frozen::map and frozen::unordered_map. I'm able to use it with std::map and std::unordered_map.

Is this a bug with the frozen library or an issue with my code?

$ cat deduction.cpp 
#include <frozen/set.h>
#include <frozen/map.h>
#include <cstdint> // pmh.h missing include fixed after 1.1.1 was released.
#include <frozen/unordered_map.h>
#include <string_view>
#include <set>
#include <map>
#include <unordered_map>

using namespace std::literals;

int main()
{
    std::set a = {"one"sv, "two"sv};
    frozen::set b = {"one"sv, "two"sv};
    std::map c = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}};
    // frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
    std::unordered_map e = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}};
    // frozen::unordered_map f = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error

    return 0;
}

I've tried compiling with v1.1.1 and with the latest (7e79c375b0) from git. Both fail in the same way.

Environment:

My primary reason for wanting to use CTAD is to avoid having to specify the size of the container, which can be awkward with large datasets.

pfee commented 9 months ago

Example compilation error:

$ c++ deduction.cpp 
deduction.cpp: In function ‘int main()’:
deduction.cpp:17:66: error: class template argument deduction failed:
   17 |     frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
      |                                                                  ^
deduction.cpp:17:66: error: no matching function for call to ‘map(std::pair<std::basic_string_view<char>, int>, std::pair<std::basic_string_view<char>, int>)’
In file included from deduction.cpp:2:
/usr/include/frozen/map.h:109:13: note: candidate: ‘template<class Key, class Value, long unsigned int N, class Compare> map(std::initializer_list<typename frozen::bits::carray<std::pair<_T1, _T2>, N>::value_type>)-> frozen::map<Key, Value, N, Compare>’
  109 |   constexpr map(std::initializer_list<value_type> items)
      |             ^~~
/usr/include/frozen/map.h:109:13: note:   template argument deduction/substitution failed:
deduction.cpp:17:66: note:   candidate expects 1 argument, 2 provided
   17 |     frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
      |                                                                  ^
/usr/include/frozen/map.h:104:13: note: candidate: ‘template<class Key, class Value, long unsigned int N, class Compare> map(std::initializer_list<typename frozen::bits::carray<std::pair<_T1, _T2>, N>::value_type>, const Compare&)-> frozen::map<Key, Value, N, Compare>’
  104 |   constexpr map(std::initializer_list<value_type> items, Compare const &compare)
      |             ^~~
/usr/include/frozen/map.h:104:13: note:   template argument deduction/substitution failed:
deduction.cpp:17:66: note:   couldn’t deduce template parameter ‘Key’
   17 |     frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
      |                                                                  ^
/usr/include/frozen/map.h:101:22: note: candidate: ‘template<class Key, class Value, long unsigned int N, class Compare> map(frozen::bits::carray<std::pair<_T1, _T2>, N>)-> frozen::map<Key, Value, N, Compare>’
  101 |   explicit constexpr map(container_type items)
      |                      ^~~
/usr/include/frozen/map.h:101:22: note:   template argument deduction/substitution failed:
deduction.cpp:17:66: note:   ‘std::pair<std::basic_string_view<char>, int>’ is not derived from ‘frozen::bits::carray<std::pair<_T1, _T2>, N>’
   17 |     frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
      |                                                                  ^
/usr/include/frozen/map.h:97:13: note: candidate: ‘template<class Key, class Value, long unsigned int N, class Compare> map(frozen::bits::carray<std::pair<_T1, _T2>, N>, const Compare&)-> frozen::map<Key, Value, N, Compare>’
   97 |   constexpr map(container_type items, Compare const &compare)
      |             ^~~
/usr/include/frozen/map.h:97:13: note:   template argument deduction/substitution failed:
deduction.cpp:17:66: note:   ‘std::pair<std::basic_string_view<char>, int>’ is not derived from ‘frozen::bits::carray<std::pair<_T1, _T2>, N>’
   17 |     frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
      |                                                                  ^
/usr/include/frozen/map.h:73:7: note: candidate: ‘template<class Key, class Value, long unsigned int N, class Compare> map(frozen::map<Key, Value, N, Compare>)-> frozen::map<Key, Value, N, Compare>’
   73 | class map {
      |       ^~~
/usr/include/frozen/map.h:73:7: note:   template argument deduction/substitution failed:
deduction.cpp:17:66: note:   ‘std::pair<std::basic_string_view<char>, int>’ is not derived from ‘frozen::map<Key, Value, N, Compare>’
   17 |     frozen::map d = {std::pair{"one"sv, 1}, std::pair{"two"sv, 2}}; // Compilation error
      |