Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - compiler error: pack expansion #25068

Closed
doraeric opened this issue Oct 31, 2024 · 1 comment
Closed

[BUG] - compiler error: pack expansion #25068

doraeric opened this issue Oct 31, 2024 · 1 comment

Comments

@doraeric
Copy link

LeetCode Username

doraeric

Problem Number, Title, and Link

  1. Minimum Total Distance Traveled https://leetcode.com/problems/minimum-total-distance-traveled/

Bug Category

Editorial

Bug Description

with the code

using u8 = uint8_t;
using u8u8u8 = tuple<u8, u8, u8>;
struct MyHash {
    size_t operator()(const u8u8u8 &keys) const noexcept {
        size_t k0 = get<0>(keys);
        size_t k1 = get<1>(keys);
        size_t k2 = get<2>(keys);
        return (k0 << 16) | (k1 << 8) | k2;
    }
};

class Solution {
public:
    long long minimumTotalDistance(vector<int>& robot, vector<vector<int>>& factory) {
        sort(robot.begin(), robot.end());
        sort(factory.begin(), factory.end());
        unordered_map<tuple<u8, u8, u8>, long long, MyHash> cache;
        // k: how many items have been processed in this factory
        function<long long(u8, u8, u8)> dp = [&](u8 rid, u8 fid, u8 k) {
            if (cache.count({rid, fid, k})) return cache[{rid, fid, k}];
            if (rid == robot.size()) return cache[{rid, fid, k}] = 0;
            if (fid == factory.size()) return cache[{rid, fid, k}] = -1;
            auto skip = dp(rid, fid+1, 0);
            int &fpos = factory[fid][0], &fcap = factory[fid][1];
            long long take = -1;
            if (fcap > k) {
                auto next = dp(rid+1, fid, k+1);
                if (next != -1) {
                    take = next + abs(robot[rid] - fpos);
                }
            }
            if (skip == -1 && take == -1) return cache[{rid, fid, k}] = -1;
            if (skip == -1) skip = LLONG_MAX;
            if (take == -1) take = LLONG_MAX;
            return cache[{rid, fid, k}] = min(skip, take);
        };
        return dp(0, 0, 0);
    }
};

There is the compiler error message

In file included from prog_joined.cpp:1:
In file included from /leetcode/precompiled/headers.h:25:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/ccomplex:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/complex:45:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/sstream:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/istream:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/ios:44:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/ios_base.h:41:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/locale_classes.h:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/string:58:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/memory_resource.h:41:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/uses_allocator_args.h:38:
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/tuple:691:2: error: pack expansion contains parameter pack '_UTypes' that has a different length (1 vs. 3) from outer parameter packs
  682 |         using __convertible = __and_<is_convertible<_UTypes, _Types>...>;
      |         ^~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/tuple:853:27: note: in instantiation of template type alias '__convertible' requested here
  844 |           = _TCC<true>::template __convertible<_Args...>::value;
      |                                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/tuple:948:12: note: in instantiation of static data member 'std::tuple<std::tuple<unsigned char, unsigned char, unsigned char> &&>::__convertible<unsigned char &, unsigned char &, unsigned char &>' requested here
  939 |         explicit(!__convertible<_UElements&...>)
      |                   ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/tuple:2000:36: note: while substituting deduced template arguments into function template 'tuple' [with _UElements = <unsigned char, unsigned char, unsigned char>]
 1991 |     { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
      |                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/hashtable_policy.h:844:7: note: in instantiation of function template specialization 'std::forward_as_tuple<std::tuple<unsigned char, unsigned char, unsigned char>>' requested here
  835 |         std::forward_as_tuple(std::move(__k)),
      |              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/unordered_map.h:991:16: note: in instantiation of member function 'std::__detail::_Map_base<std::tuple<unsigned char, unsigned char, unsigned char>, std::pair<const std::tuple<unsigned char, unsigned char, unsigned char>, long long>, std::allocator<std::pair<const std::tuple<unsigned char, unsigned char, unsigned char>, long long>>, std::__detail::_Select1st, std::equal_to<std::tuple<unsigned char, unsigned char, unsigned char>>, MyHash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true>>::operator[]' requested here
  982 |       { return _M_h[std::move(__k)]; }
      |                ^
Line 20: Char 57: note: in instantiation of member function 'std::unordered_map<std::tuple<unsigned char, unsigned char, unsigned char>, long long, MyHash>::operator[]' requested here
   20 |             if (cache.count({rid, fid, k})) return cache[{rid, fid, k}];
      |                                                         ^

If compile the code with g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, there is no compiling error.

Don't know whether it's related to llvm/llvm-project#61415

Language Used for Code

C++

Code used for Submit/Run operation

using u8 = uint8_t;
using u8u8u8 = tuple<u8, u8, u8>;
struct MyHash {
    size_t operator()(const u8u8u8 &keys) const noexcept {
        size_t k0 = get<0>(keys);
        size_t k1 = get<1>(keys);
        size_t k2 = get<2>(keys);
        return (k0 << 16) | (k1 << 8) | k2;
    }
};

class Solution {
public:
    long long minimumTotalDistance(vector<int>& robot, vector<vector<int>>& factory) {
        sort(robot.begin(), robot.end());
        sort(factory.begin(), factory.end());
        unordered_map<tuple<u8, u8, u8>, long long, MyHash> cache;
        // k: how many items have been processed in this factory
        function<long long(u8, u8, u8)> dp = [&](u8 rid, u8 fid, u8 k) {
            if (cache.count({rid, fid, k})) return cache[{rid, fid, k}];
            if (rid == robot.size()) return cache[{rid, fid, k}] = 0;
            if (fid == factory.size()) return cache[{rid, fid, k}] = -1;
            auto skip = dp(rid, fid+1, 0);
            int &fpos = factory[fid][0], &fcap = factory[fid][1];
            long long take = -1;
            if (fcap > k) {
                auto next = dp(rid+1, fid, k+1);
                if (next != -1) {
                    take = next + abs(robot[rid] - fpos);
                }
            }
            if (skip == -1 && take == -1) return cache[{rid, fid, k}] = -1;
            if (skip == -1) skip = LLONG_MAX;
            if (take == -1) take = LLONG_MAX;
            return cache[{rid, fid, k}] = min(skip, take);
        };
        return dp(0, 0, 0);
    }
};

Expected behavior

compile without any error

Screenshots

No response

Additional context

No response

Copy link

LeetCode Support commented: Thank you for reaching out regarding the compilation error you encountered with problem #2463. I understand how discrepancies between different compiler versions can lead to confusion and hinder your practice on our platform. We'd like to investigate this further to provide you with the best possible support. Could you please confirm the compiler version used by LeetCode during your submission? Additionally, if you have any other specific compiler insights, such as more details about the specific errors encountered on LeetCode's environment versus your local setup, it would be helpful. This information will assist us in determining if there indeed is an issue on our side or if it's a compatibility matter with certain compiler versions. We appreciate your patience and understanding as we work to address this matter.

LeetCode Support Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant