Why does a C++ template instantiation fail?

In the RefPerSys GPLv3+ project for Linux/x86-64 (on a Debian/Sid system), git commit 37172c9af257865d, compiling with GCC 10, invoked as g++ -std=gnu++17 -Og -g3 -Wall -Wextra etc... I am getting the following error message:

  refpersys.hh: In instantiation of ‘PaylClass* Rps_ObjectZone::put_new_arg3_payload(Arg1Class, Arg2Class, Arg3Class) [with PaylClass = Rps_PayloadWebex; Arg1Class = long unsigned int; Arg2Class = Onion::Request*; Arg3Class = Onion::Response*]’:
  httpweb_rps.cc:314:71:   required from here
  refpersys.hh:2162:76: error: no matching function for call to ‘Rps_ObjectZone::rps_allocate4<Rps_PayloadWebex, long unsigned int, Onion::Request*, Onion::Response*>(Rps_ObjectZone*, long unsigned int&, Onion::Request*&, Onion::Response*&)’
   2162 | Zone::rps_allocate4<PaylClass,Arg1Class,Arg2Class,Arg3Class>(this,arg1,arg2,arg3);
        | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~

  In file included from headweb_rps.hh:37,
                   from httpweb_rps.cc:34:
  refpersys.hh:1701:3: note: candidate: ‘template<class ZoneClass, class Arg1Class, class Arg2Class, class Arg3Class, class Arg4Class> static ZoneClass* Rps_QuasiZone::rps_allocate4(Arg1Class, Arg2Class, Arg3Class, Arg4Class)’
   1701 |   rps_allocate4(Arg1Class arg1, Arg2Class arg2, Arg3Class arg3, Arg4Class arg4)
        |   ^~~~~~~~~~~~~
  refpersys.hh:1701:3: note:   template argument deduction/substitution failed:
  In file included from headweb_rps.hh:37,
                   from httpweb_rps.cc:34:
  refpersys.hh:2162:76: note:   cannot convert ‘(Rps_ObjectZone*)this’ (type ‘Rps_ObjectZone*’) to type ‘long unsigned int’
   2162 | Zone::rps_allocate4<PaylClass,Arg1Class,Arg2Class,Arg3Class>(this,arg1,arg2,arg3);
        | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~

I am not easily able to give a small example, but I am capable of giving the following explanations. It is about implementing some dynamically typed language (expert system like rules, semantically inspired by Common Lisp) with a multi-threaded implementation (and web interface, with our precise tracing garbage collector).

The main header file is refpersys.hh and is #included everywhere. A companion header file is headweb_rps.hh relevant only to web-related code using libonion, which is some HTTP server library (with namespace Onion::), and it is #includeing the refpersys.hh header.

No multiple-inheritance is used in the C++ sense.

We have an enum Rps_Type (in file refpersys.hh line 903) defining the tag of some tagged union type. That tagged union type is the top class Rps_TypedZone (in file refpersys.hh line 1630) with an obvious constructor Rps_TypedZone::Rps_TypedZone(const Rps_Type ty) defined in refpersys.hh line 1637.

We have a class Rps_QuasiZone subclass of Rps_TypedZone (in file refpersys.hh line 1646).

We have a class Rps_ZoneValue (in refpersys.hh:1755) subclass of Rps_QuasiZone.

The class Rps_ObjectZone (in refpersys.hh:1964) is a subclass of Rps_ZoneValue. Let's call RefPerSys objects any C++ instance of a subclass of that Rps_ObjectZone.

The class Rps_ObjectRef (in refpersys.hh:694) is our GC-ed smart pointer to Rps_ObjectZone.

The class Rps_Value (in refpersys.hh:967) is a single-word smart pointer (a bit like in Common Lisp, SBCL-value like).

The class Rps_Payload (in refpersys.hh:2264) carries some optional extra data inside Rps_ObjectZone. Each such payload belongs to a single RefPerSys object (some Rps_ObjectZone), called its owner.

The put_new_arg3_payload template member function of Rps_ObjectZone is simply (in refpersys.hh:2157 and following lines) with ob_payload being a member field of Rps_ObjectZone declared as std::atomic<Rps_Payload*> ob_payload; in line refpersys.hh:1981:

  PaylClass* put_new_arg3_payload(Arg1Class arg1, Arg2Class arg2, Arg3Class arg3)
  {
    std::lock_guard<std::recursive_mutex> gu(ob_mtx);
    PaylClass*newpayl =
      Rps_QuasiZone::rps_allocate4<PaylClass,Arg1Class,Arg2Class,Arg3Class>(this,arg1,arg2,arg3);
    Rps_Payload*oldpayl = ob_payload.exchange(newpayl);
    if (oldpayl)
      delete oldpayl;
    return newpayl;
  };                // end put_new_arg3_payload

Many web interactions (that is an HTTP request, in C++ some Onion::Request, and the corresponding HTTP reply, in C++ some Onion::Reply, itself a subclass of C++ std::ostream) are reified as C++ instances of class Rps_PayloadWebex declared in file headweb_rps.hh line 65, and subclass of Rps_Payload.

The template member function Rps_QuasiZone::rps_allocate4 is defined (at line refpersys.hh:1699) as:

template <typename ZoneClass, typename Arg1Class, typename Arg2Class, typename Arg3Class, typename Arg4Class>
static ZoneClass*
rps_allocate4(Arg1Class arg1, Arg2Class arg2, Arg3Class arg3, Arg4Class arg4)
{
  return new(nullptr) ZoneClass(arg1, arg2, arg3, arg4);
};

The call frames of our "interpreter" are reified as class Rps_ProtoCallFrame; and we have (in refpersys.hh line 691) a typedef Rps_ProtoCallFrame Rps_CallFrame;

The class Rps_ProtoCallFrame is a subclass of Rps_TypedZone defined in refpersys.hh:2823.

The faulty line httpweb_rps.cc:314 is inside:

 Rps_ObjectRef
 Rps_PayloadWebex::make_obwebex(Rps_CallFrame*callerframe, Onion::Request*req, Onion::Response*resp,
                                uint64_t reqnum)
 {
   RPS_ASSERT(callerframe != nullptr && callerframe->is_good_call_frame());
   RPS_ASSERT(req != nullptr);
   RPS_ASSERT(resp != nullptr);
   auto web_exchange_ob = RPS_ROOT_OB(_8zNtuRpzXUP013WG9S);
   RPS_DEBUG_LOG(WEB, "Rps_PayloadWebex::make_obwebex start reqnum#" << reqnum
                );
   RPS_LOCALFRAME(/*descr:*/ web_exchange_ob,
                             /*prev:*/callerframe,
                             /*locals:*/
                             Rps_ObjectRef obwebex);
   _f.obwebex = Rps_ObjectRef::make_object(&_, web_exchange_ob);
   auto paylwebex = ////////////////////////////////////// FAULTY LINE BELOW
     _f.obwebex->put_new_arg3_payload<Rps_PayloadWebex>(reqnum,req,resp);
   RPS_DEBUG_LOG(WEB, "Rps_PayloadWebex::make_obwebex end reqnum#" << reqnum
                 << " obwebex=" << _f.obwebex << " startim:" <<  paylwebex->webex_startim);
   RPS_ASSERT(paylwebex != nullptr);
   return _f.obwebex;
 } // end PayloadWebex::make_obwebex

In above code, RPS_ASSERT, RPS_ROOT_OB, RPS_LOCALFRAME, RPS_DEBUG_LOG are C++ macros. The macroexpansion of above code is:

Rps_ObjectRef
Rps_PayloadWebex::make_obwebex(Rps_CallFrame*callerframe, Onion::Request*req, Onion::Response*resp,
                               uint64_t reqnum)
{
  do { if (__builtin_expect(!!(!((callerframe != nullptr
                                  && callerframe->is_good_call_frame()))),0))
      { fprintf(
                //# 302 "httpweb_rps.cc" 3
                stderr
                //# 302 "httpweb_rps.cc"
                , "\n\n" "%s*** RefPerSys ASSERT failed: %s%s\n" "%s:%d: {%s}\n\n", (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[1m"):""),
                "(callerframe != nullptr && callerframe->is_good_call_frame())",
                (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[0m"):""),
                "httpweb_rps.cc",302,__PRETTY_FUNCTION__);
        rps_fatal_stop_at("httpweb_rps.cc",302); }}
  while(0);
  do
    { if (__builtin_expect(!!(!((req != nullptr))),0)) {
        fprintf(
                //# 303 "httpweb_rps.cc" 3
                stderr
                //# 303 "httpweb_rps.cc"
                , "\n\n" "%s*** RefPerSys ASSERT failed: %s%s\n" "%s:%d: {%s}\n\n",
                (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[1m"):""), "(req != nullptr)",
                (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[0m"):""), "httpweb_rps.cc",303,__PRETTY_FUNCTION__);
        rps_fatal_stop_at("httpweb_rps.cc",303); }} while(0);
  do { if (__builtin_expect(!!(!((resp != nullptr))),0)) {
      fprintf(
              //# 304 "httpweb_rps.cc" 3
              stderr
              //# 304 "httpweb_rps.cc"
              , "\n\n" "%s*** RefPerSys ASSERT failed: %s%s\n" "%s:%d: {%s}\n\n",
              (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[1m"):""), "(resp != nullptr)",
              (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[0m"):""), "httpweb_rps.cc",304,__PRETTY_FUNCTION__);
      rps_fatal_stop_at("httpweb_rps.cc",304); }} while(0);
  auto web_exchange_ob = rps_rootob_8zNtuRpzXUP013WG9S;
  do { if ((rps_debug_flags & (1 << RPS_DEBUG_WEB)))
      { std::ostringstream _logstream_306;
        _logstream_306 << "Rps_PayloadWebex::make_obwebex start reqnum#" << reqnum << std::flush;
        rps_debug_printf_at("httpweb_rps.cc", 306, RPS_DEBUG_WEB,
                            "%s", _logstream_306.str().c_str()); } } while (0)
    ;
  struct RpsFrameData308 {/*locals:*/ Rps_ObjectRef obwebex; };
  typedef Rps_FieldedCallFrame<RpsFrameData308> Rps_FldCallFrame308;
  class Rps_FrameAt308 : public Rps_FldCallFrame308
  { public:
    Rps_FrameAt308(Rps_ObjectRef obd308, Rps_CallFrame* prev308) :
      Rps_FldCallFrame308(obd308, prev308) { }; };
  Rps_FrameAt308 _((/*descr:*/ web_exchange_ob),(/*prev:*/callerframe));
  auto& _f = *_.fieldsptr();


  ;
  _f.obwebex = Rps_ObjectRef::make_object(&_, web_exchange_ob);
  auto paylwebex =
    _f.obwebex->put_new_arg3_payload<Rps_PayloadWebex>(reqnum,req,resp);
  do { if ((rps_debug_flags & (1 << RPS_DEBUG_WEB)))
      { std::ostringstream _logstream_315;
        _logstream_315 << "Rps_PayloadWebex::make_obwebex end reqnum#"
                       << reqnum << " obwebex=" << _f.obwebex << " startim:"
                       << paylwebex->webex_startim << std::flush; rps_debug_printf_at("httpweb_rps.cc",
                                                                                      315, RPS_DEBUG_WEB, "%s",
                                                                                      _logstream_315.str().c_str()); } } while (0)
    ;
  do { if (__builtin_expect(!!(!((paylwebex != nullptr))),0)) {
      fprintf(
              //# 317 "httpweb_rps.cc" 3
              stderr
              //# 317 "httpweb_rps.cc"
              , "\n\n" "%s*** RefPerSys ASSERT failed: %s%s\n" "%s:%d: {%s}\n\n",
              (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[1m"):""), "(paylwebex != nullptr)",
              (rps_stderr_istty?(rps_without_terminal_escape?"":"\033[0m"):""), "httpweb_rps.cc",317,__PRETTY_FUNCTION__);
      rps_fatal_stop_at("httpweb_rps.cc",317); }} while(0);
  return _f.obwebex;
} // end PayloadWebex::make_obwebex

What am I doing wrong?

Upvotes: 0

Views: 107

Answers (1)

Micha&#235;l Roy
Micha&#235;l Roy

Reputation: 6481

This line towards the end of the error message is clear, it hints that long unsigned int should be looking for...

cannot convert ‘(Rps_ObjectZone*)this’ (type ‘Rps_ObjectZone*’) to type ‘long unsigned int’   

This, allied to this line at the beginning of the error message:

refpersys.hh:2162:76: error: no matching function for call to Rps_ObjectZone::rps_allocate4<Rps_PayloadWebex, long unsigned int, Onion::Request*, Onion::Response*>(Rps_ObjectZone*, long unsigned int&, Onion::Request*&, Onion::Response*&)’

And this, the first line of error:

refpersys.hh: In instantiation of ‘PaylClass* Rps_ObjectZone::put_new_arg3_payload(Arg1Class, Arg2Class, Arg3Class) [with PaylClass = Rps_PayloadWebex; Arg1Class = long unsigned int; Arg2Class = Onion::Request*; Arg3Class = Onion::Response*

The important part being Arg1Class = long unsigned int;

All this means that long unsigned int (the type of variable reqnum is not a valid type for the first parameter in your call to _f.obwebex->put_new_arg3_payload<Rps_PayloadWebex>();

It seems that ArgClass1 should be at least some kind of pointer, and even more likely, a pointer to a type derived from Rps_ObjectZone.

I haven't used this specific library, but that's what the error message means.

I think the most likely error is that you've mixed the order of the first two parameters of this function call.

   Rps_QuasiZone::rps_allocate4<PaylClass,Arg1Class,Arg2Class,Arg3Class> (this,arg1,arg2,arg3);

Either there, or in the call to the constructor for class ZoneClass.

The mixed order of parameters is only a supposition, I haven't seen your constructor for class ZoneClass.

As an aside note: If Arg1Class MUST be convertible to a ZoneClass*, this means the code in question is declaring template parameter types where it should not.

Upvotes: 1

Related Questions