Loki-Astari / ThorsMongo

C++ MongoDB API and BSON/JSON Serialization library
GNU General Public License v3.0
316 stars 71 forks source link

MakeTrait: error: unable to deduce 'std::initializer_list<auto>' #72

Closed ripe909 closed 3 years ago

ripe909 commented 3 years ago

OK, I refreshed my code from the latest master to get the Filter implementation. I see quite a few changes since my last pull!

Regardless, I am running into the following error now with a simple test case.

Using the filter test code:

struct FilterTestType
{
    std::string     m1;
    std::string     m2;

    std::map<std::string, bool> filter;
};

ThorsAnvil_MakeFilter(FilterTestType, filter);
ThorsAnvil_MakeTrait(FilterTestType, m1, m2);

I am hitting the following error:

/ThorSerialize/Traits.h:580:18: error: unable to deduce 'std::initializer_list<auto>' from '{std::make_pair<long unsigned int, long unsigned int>(0, 0), ThorsAnvil::Serialize::Traits<FilterTestType>::addSizeEachMemberItem<std::pair<const char*, std::__cxx11::basic_string<char> FilterTestType::*> >((* & printer), (* & object), (* & std::get<0, std::pair<const char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > FilterTestType::*>, std::pair<const char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > FilterTestType::*> >((* & members)))), ThorsAnvil::Serialize::Traits<FilterTestType>::addSizeEachMemberItem<std::pair<const char*, std::__cxx11::basic_string<char> FilterTestType::*> >((* & printer), (* & object), (* & std::get<1, std::pair<const char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > FilterTestType::*>, std::pair<const char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > FilterTestType::*> >((* & members))))}'
  580 |             auto sizeData = {std::make_pair(0UL, 0UL), addSizeEachMemberItem(printer, object, std::get<Seq>(members))...};              \
      |                  ^~~~~~~~
/ThorSerialize/Traits.h:624:5: note: in expansion of macro 'ThorsAnvil_MakeTrait_Base'
  624 |     ThorsAnvil_MakeTrait_Base( , Map, 00, __VA_ARGS__, 1);              \
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
../source/test_thors.h:31:1: note: in expansion of macro 'ThorsAnvil_MakeTrait'
   31 | ThorsAnvil_MakeTrait(FilterTestType, m1, m2);
      | ^~~~~~~~~~~~~~~~~~~~
/ThorSerialize/Traits.h:580:18: note:   deduced conflicting types for parameter 'auto' ('std::pair<long unsigned int, long unsigned int>' and 'std::pair<unsigned int, unsigned int>')
  580 |             auto sizeData = {std::make_pair(0UL, 0UL), addSizeEachMemberItem(printer, object, std::get<Seq>(members))...};              \
      |                  ^~~~~~~~
/ThorSerialize/Traits.h:624:5: note: in expansion of macro 'ThorsAnvil_MakeTrait_Base'
  624 |     ThorsAnvil_MakeTrait_Base( , Map, 00, __VA_ARGS__, 1);              \
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
../source/test_thors.h:31:1: note: in expansion of macro 'ThorsAnvil_MakeTrait'
   31 | ThorsAnvil_MakeTrait(FilterTestType, m1, m2);
      | ^~~~~~~~~~~~~~~~~~~~

This is way over my head, but hopefully I can get it resolved.

Loki-Astari commented 3 years ago

I just added a test for this issue.

But it compiles and generates no errors for me: https://github.com/Loki-Astari/ThorsSerializer/blob/master/src/Serialize/test/Issue72Test.cpp

If you can modify this file to generate the same errors as you get I will accept the pull request and I can go from there.

Or if you want to reply here with more information so I can reproduce the error.

ripe909 commented 3 years ago

Thanks. Well, again, this is way above my cpp skills.

It looks like the typedef for unsigned int and signed int is the same size as long unsigned int and long signed int with Newlib for arm-gcc-eabi, so "auto" can't deduce for the correct type for std::pair I found this somewhat similar issue for another project: https://github.com/harfbuzz/harfbuzz/issues/451

This would explain why it doesn't compile for me, and it does for your compiler.

Fixing it is another matter. I tried to remove all of the templates for int and unsigned int but that didn't help

Loki-Astari commented 3 years ago

Try this change on line 580 of Traits.h let me know if it fixes the issue:

auto sizeData = ....

///change to

std::initializer_list<std::pair<std::size_t, std::size_t>> sizeData =
Loki-Astari commented 3 years ago

If that does not work try: 580 of Traits.h

std::make_pair(0UL, 0UL)

/// Change to

std::make_pair(std::size_t{0}, std::size_t{0})
Loki-Astari commented 3 years ago

One of those two or both should resolve the issue.

For initializer_list<> to work all the elements have to be the same type.
So the function call is returning: addSizeEachMemberItem() is returning std::pair<std:size_t, std::size_t> and make_pair() is returning std::pair<unsigned long, unsigned long> on most systems these would match looks like maybe your system is using unsigned int for std::size_t which causes the issue. So either we make the list a specific type so the compiler does not need to deduce the type or we are more explicit on the type of the initial pair in the list.

Loki-Astari commented 3 years ago

I see quite a few changes since my last pull!

Sine your last pull (which must have been more than 6 months ago) I have added support for BSON (this is a Binary Json format that is used by Mongo DB).

The advantages of BSON is that fields have a known size or are prefixed by their size (so easy to skip them). The disadvantages of BSON is that it is not very human-readable and arrays are implemented in a clunky fashion. Additionally, if you store a lot of small integers then BSON is bulkier as it always uses four bytes for an integer and is scattered with size objects that take up space (though JSON is scattered with lots of white space so win/loose).

ripe909 commented 3 years ago

Try this change on line 580 of Traits.h let me know if it fixes the issue:

auto sizeData = ....

///change to

std::initializer_list<std::pair<std::size_t, std::size_t>> sizeData =

I tested this change and it addresses the issue. Thank You!

Loki-Astari commented 3 years ago

I will update the code and push.

Loki-Astari commented 3 years ago

Pushed an update. Closing issue.