Search code examples
c++c++14autocereal

"auto" deducing incorrect type in hashtable_policy.h


I'm building a pair of tools, gridlab-d and HELICS, the prior of which uses the latter's shared libraries. When compiling gridlab-d after successfully building/installing HELICS, I get the following errors:

In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
             from /usr/include/c++/7/unordered_map:47,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/external/cereal/archives/../cereal.hpp:36,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/external/cereal/archives/portable_binary.hpp:32,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/ValueConverter_impl.hpp:20,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/ValueConverter.hpp:65,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/ValueFederate.hpp:11,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/CombinationFederate.hpp:10,
             from connection/helics_msg.h:21,
             from connection/helics_msg.cpp:16:
/usr/include/c++/7/bits/hashtable_policy.h: In member function ‘std::size_t std::__detail::_Power2_rehash_policy::_M_next_bkt(std::size_t)’:
/usr/include/c++/7/bits/hashtable_policy.h:563:40: error: invalid operands of types ‘std::size_t {aka long unsigned int}’ and ‘double’ to binary ‘operator<<’
   const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
                          ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
             from /usr/include/c++/7/unordered_map:47,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/external/cereal/archives/../cereal.hpp:36,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/external/cereal/archives/portable_binary.hpp:32,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/ValueConverter_impl.hpp:20,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/ValueConverter.hpp:65,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/ValueFederate.hpp:11,
             from /home/ericsilk/.local/helics-2.3.0/include/helics/application_api/CombinationFederate.hpp:10,
             from connection/helics_msg.h:21,
             from connection/init.cpp:16:
/usr/include/c++/7/bits/hashtable_policy.h: In member function ‘std::size_t std::__detail::_Power2_rehash_policy::_M_next_bkt(std::size_t)’:
/usr/include/c++/7/bits/hashtable_policy.h:563:40: error: invalid operands of types ‘std::size_t {aka long unsigned int}’ and ‘double’ to binary ‘operator<<’
   const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
                          ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Investigating the header in question, this is the code it is complaining about:

// Return a bucket size no smaller than n (as long as n is not above the
// highest power of 2).
std::size_t
_M_next_bkt(std::size_t __n) noexcept
{
  const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
  const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
  std::size_t __res = __clp2(__n);

  if (__res == __n)
__res <<= 1;

  if (__res == 0)
__res = __max_bkt;

  if (__res == __max_bkt)
// Set next resize to the max value so that we never try to rehash again
// as we already reach the biggest possible bucket number.
// Note that it might result in max_load_factor not being respected.
_M_next_resize = std::size_t(-1);
  else
_M_next_resize
  = __builtin_ceil(__res * (long double)_M_max_load_factor);

  return __res;
}

So, it appears that the auto type of __max_width is improperly deduced to be of type double rather than size_t (which should be trivially deducible from the type of std::min<size_t>). If I make a copy of the header, and then edit it to change auto to size_t, the error disappears, confirming this.

Interestingly, the following program compiles without complaint:

#include <iostream>
#include <algorithm>

int main()
{
    const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
    const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);

    std::cout << "__max_width: " << __max_width << std::endl;
    std::cout << "__max_bkt: " << __max_bkt << std::endl;

    return 0;
}

I'm on Ubuntu 18.04, using gcc 7.4.0, although I get the same error with clang 6.0.0. I've also confirmed this occurs in both a chroot 18.04 environment, and on another machine running WSL Ubuntu 18.04. HELICS was compiled with the Python interface and with HELICS_BUILD_CXX_SHARED_LIB, and gridlab-d was built per the instructions.

Happy to provide additional information, any help resolving this without requiring users to modify their standard headers would be greatly appreciated.

Update: Here's the pre-processed output of the root file, helics_msg.cpp, with the specific function it is complaining about. I don't see any macro funny business going on that would cause the error. The full file is located here as a gist, and the snippet starts on line 72,722. Its a large file (>95k lines).

    std::size_t
    _M_next_bkt(std::size_t __n) noexcept
    {
      const auto __max_width = std::
# 562 "/usr/include/c++/7/bits/hashtable_policy.h"
                                   fmin
# 562 "/usr/include/c++/7/bits/hashtable_policy.h" 3
                                      <size_t>(sizeof(size_t), 8);
      const auto __max_bkt = size_t(1) << (__max_width * 8 - 1);
      std::size_t __res = __clp2(__n);

      if (__res == __n)
 __res <<= 1;

      if (__res == 0)
 __res = __max_bkt;

      if (__res == __max_bkt)



 _M_next_resize = std::size_t(-1);
      else
 _M_next_resize
   = __builtin_ceil(__res * (long double)_M_max_load_factor);

      return __res;
    }

Solution

  • gridlab-d redefines min to fmin:

    #define min fmin /**< min macro */
    

    So whenever platform.h gets included, min will be replaced by fmin. This is only an educated guess, as I don't have you exact include-graph at hand.

    Unfortunately, std::fmin will always return a double on integral values. This is a defect in gridlab-d and should be reported—as you already did.