0

I have this function:

void CGuild::AddComment(LPCHARACTER ch, const std::string& str)
{   if (str.length() > GUILD_COMMENT_MAX_LEN)
        return;

    char text[GUILD_COMMENT_MAX_LEN * 2 + 1];
    DBManager::instance().EscapeString(text, sizeof(text), str.c_str(), str.length());

    DBManager::instance().FuncAfterQuery(void_bind([this](auto&& data) { return this->RefreshCommentForce(data); }, ch->GetPlayerID()),
            "INSERT INTO guild_comment%s(guild_id, name, notice, content, time) VALUES(%u, '%s', %d, '%s', NOW())", 
            get_table_postfix(), m_data.guild_id, ch->GetName(), (str[0] == '!') ? 1 : 0, text);
}

and I'm compiling with clang++-devel in C++1z, at a Linux enviroment. The file generates this error:

./any_function.h:41:15: error: no type named 'argument_type' in '(lambda at
      guild.cpp:1023:49)'
                typename F::argument_type value;
                ~~~~~~~~~~~~^~~~~~~~~~~~~
guild.cpp:1023:39: note: in instantiation of template class 'void_binder<(lambda
      at guild.cpp:1023:49)>' requested here
        DBManager::instance().FuncAfterQuery(void_bind([this](auto&& dat...
                                             ^

and here is the any_function.h:

template <class F>
class void_binder
{
    protected:
        F f;
        typename F::argument_type value;
    public:
        void_binder(const F& f, const typename F::argument_type x)
            : f(f), value(x) {}
        void operator()() const {
            return f(value);
        }
};

template <class F, class Arg> 
inline void_binder<F> void_bind(const F& f, const Arg& arg)
{
    typedef typename F::argument_type arg_type;
    return void_binder<F>(f, arg_type(arg));
}

How can I resolve this error?

Thank you for your comments.

max66
  • 65,235
  • 10
  • 71
  • 111
Dev
  • 677
  • 1
  • 8
  • 19

1 Answers1

1

If I understand correctly, you're using in void_bind() the type derived from the first template argument, F,

F::argument_type

that is a typename (deprecated, in C++17) defined in std::function<R(Args...)> but only when Args... is only one type (only when the function receive only one argument).

But you pass to void_bind() a generic lambda

FuncAfterQuery(void_bind([this](auto&& data) { return this->RefreshCommentForce(data); }, [...]

that isn't a std::function and doesn't define a argument_type

So I suggest (but I can't prove... your example is sub-optimal) to save the lambda in an appropriate std::function and pass the std::function to void_bind()

Something as

std::function<RetType(ArgType)>
   f { [this](auto&& data) { return this->RefreshCommentForce(data); } };

FuncAfterQuery(void_bind(f, ch->GetPlayerID()), [...]

with the appropriate (?) RetType and ArgType types.

max66
  • 65,235
  • 10
  • 71
  • 111