hydroshiba's blog

By hydroshiba, history, 21 month(s) ago, In English

In last night AtCoder Beginner Contest, my submission for problem D received a Compile Error verdict using GCC (link). That very same code (besides including some library manually because Clang doesn't have bits/stdc++.h) got accepted when I used Clang (link).

More on the error, I tried to read the error message and it seemed to be an error about std::set. However the message was too long for me to either understand it or google it (man I hate C++ error messages).

Error message is included in the submission link but if anyone's interested here it is

Can anyone give me some insights on this problem?

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Use this comparator:

struct comp{
    bool operator()(node a, node b) const {
        return (a.top < b.top);
    }
};
  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The submission got AC, can you please tell why adding const made it AC?

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      These are const member functions, which are not allowed to:

      • modify the member variables inside its struct/class
      • call any member function which is non-const

      You can find more explanations here.

      Comparator used in STL algorithms/containers is expected to be const member functions because it does not make sense to modify values inside a comparator.

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Then how could my code compile and run in Clang?

        • »
          »
          »
          »
          »
          21 month(s) ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I don't know why your code compiles on clang. If you look at your submission, your code generates warning about non-const comparator:

          In file included from ./Main.cpp:1:
          /usr/lib/llvm-10/bin/../include/c++/v1/set:602:30: warning: the specified comparator type does not provide a viable const call operator [-Wuser-defined-warnings]
                  static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
                                       ^
          ./Main.cpp:39:17: note: in instantiation of member function 'std::__1::set<node, comp, std::__1::allocator<node> >::~set' requested here
          set<node, comp> card;
                          ^
          /usr/lib/llvm-10/bin/../include/c++/v1/__tree:976:5: note: from 'diagnose_if' attribute on '__diagnose_non_const_comparator<node, comp>':
              _LIBCPP_DIAGNOSE_WARNING(!std::__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
              ^                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          /usr/lib/llvm-10/bin/../include/c++/v1/__config:1297:21: note: expanded from macro '_LIBCPP_DIAGNOSE_WARNING'
               __attribute__((diagnose_if(__VA_ARGS__, "warning")))
                              ^           ~~~~~~~~~~~
          1 warning generated.
          
»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Just write if((int)pile[a.id].size() == k) instead of if(pile[a.id].size() == k) in both lines 56 and 74. This sort of issue occurs in many compilers.