msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

Fail to compile after [419877cf] commit (Added -Wconversion support for C++.)

Open gviolator opened this issue 7 years ago • 14 comments

I see there is fixes for correct type cast.

Previously within pack.hpp was declaraion:

void append_buffer(const char* buf, size_t len)
    { m_stream.write(buf, len); }

Now there is couple of compile time overloads to respect valid type cast when len parameter is not size_t. But, what if method ::write has overloads ? In this case &Stream::write will not be compiled (because compiler has ambiguity).

I have class Writer with 'primary' write method

virtual void write(const std::byte*, size_t size) = 0;

and second 'utility' method:

void write(const char* bytes, size_t size)
{
	write(reinterpret_cast<const std::byte*>(bytes), size);
}

Second method introduced especially to using with msgpack-c, but for first parameter compatibility. As temporary solution i remove second 'utility' method from my Writer class and 'fix' pack.hpp as

void append_buffer(const char* buf, size_t len)
{
    append_buffer(m_stream, &Stream::write, buf, len);
}

template<typename S, typename R, typename Ptr, typename Size>
static void append_buffer(S& s, R (S::*) (Ptr, Size), const char* buf, size_t len)
{
  s.write(reinterpret_cast<Ptr>(buf), static_cast<Size>(len));
}

gviolator avatar Apr 29 '19 08:04 gviolator

Now there is couple of compile time overloads to respect valid type cast when len parameter is not size_t. But, what if method ::write has overloads ? In this case &Stream::write will not be compiled (because compiler has ambiguity).

Is your error the same as follows?

#include <cstdlib>
#include <utility>
#include <ostream>

template <typename Stream>
struct packer {
    packer(Stream& s):m_stream(s) {}
    void append_buffer(const char* buf, size_t len)
    {
        append_buffer(m_stream, &Stream::write, buf, len);
    }

    template <typename S, typename Write>
    typename std::enable_if<
        std::is_same<
            std::ostream& (std::ostream::*)(const char*, std::streamsize),
            Write
        >::value
    >::type
    append_buffer(S& s, Write, const char* buf, size_t len)
    {
        s.write(buf, static_cast<std::streamsize>(len));
    }

    template <typename S, typename Write>
    typename std::enable_if<
        !std::is_same<
            std::ostream& (std::ostream::*)(const char*, std::streamsize),
            Write
        >::value
    >::type
    append_buffer(S& s, Write, const char* buf, size_t len)
    {
        s.write(buf, len);
    }

    Stream& m_stream;
};

struct user_stream {
    void write(const char* bytes, size_t size) {}
    void write(const std::byte* bytes, size_t size) {}
};


int main() {
    user_stream us;
    packer<user_stream> p(us);
    char buf[] = "123";

    p.append_buffer(buf, sizeof(buf));
}
issue777.cpp:10:9: error: no matching member function for call to 'append_buffer'
        append_buffer(m_stream, &Stream::write, buf, len);
        ^~~~~~~~~~~~~
issue777.cpp:51:7: note: in instantiation of member function 'packer<user_stream>::append_buffer' requested here
    p.append_buffer(buf, sizeof(buf));
      ^
issue777.cpp:20:5: note: candidate template ignored: couldn't infer template argument 'Write'
    append_buffer(S& s, Write, const char* buf, size_t len)
    ^
issue777.cpp:32:5: note: candidate template ignored: couldn't infer template argument 'Write'
    append_buffer(S& s, Write, const char* buf, size_t len)
    ^
issue777.cpp:8:10: note: candidate function not viable: requires 2 arguments, but 4 were provided
    void append_buffer(const char* buf, size_t len)
         ^
1 error generated.

redboltz avatar Apr 30 '19 06:04 redboltz

Yes. I have similar error. This occur because there is no acceptable overload for append_buffer in my case - none of SFINAE expressions are acceptable (because of error).


Of course there is simply solution by providing "wrapper" class that have exacly one write method with correct signature

gviolator avatar Apr 30 '19 06:04 gviolator

I sent the PR #778. I removed SFINAE and added type extractor. Could you try this?

redboltz avatar Apr 30 '19 12:04 redboltz

Ok. This is nice solution. But not for my case. I will remove one ::write method from my Writer class, but my remaining method's signature is (const std::byte *, size_t) - and it can not be currently used . As wraparound I can use class thats provide only one write method with correct signature, but i can to not doing that if size_arg_type helper will be replaced with more extended declaraion (need to doing same thing with first method's arg):

template <typename Ret, typename Cls, typename T1, typename T2>
struct args_info<Ret (Cls::*)(T1, T2)> {
    typedef T1 ptr_type;
    typedef T2 size_type;
};

and

template <typename Write>
void append_buffer(Write, const char* buf, size_t len)
{
  typedef args_info<Write> info;

  static_assert(sizeof(std::remove_pointer_t<typename info::ptr_type>) == sizeof(char));

  m_stream.write(reinterpret_cast<typename info::ptr_type>(buf), static_cast<typename info::size_type>(len));
}

P.S.: in any case second specialization is required (for args_info or size_arg_type) where write method can be noexcept:

template <typename Ret, typename Cls, typename T1, typename T2>
struct args_info<Ret (Cls::*)(T1, T2)> {
    typedef T1 ptr_type;
    typedef T2 size_type;
};

template <typename Ret, typename Cls, typename T1, typename T2>
struct args_info<Ret(Cls::*)(T1, T2) noexcept> {
  typedef T1 ptr_type;
  typedef T2 size_type;
};

or

template <typename Ret, typename Cls, typename T1, typename T2>
struct size_arg_type<Ret (Cls::*)(T1, T2)> {
    typedef T2 type;
};

template <typename Ret, typename Cls, typename T1, typename T2>
struct size_arg_type<Ret (Cls::*)(T1, T2) noexcept> {
    typedef T2 type;
};

P.P.S: Thank you for great library !

gviolator avatar Apr 30 '19 20:04 gviolator

Hmm.... I wrote a minimal code to check overloaded functions.

#include <cstddef>

struct user_stream3 {
    void write(const char* bytes, size_t size) {}
    void write(const std::byte* bytes, size_t size) {}
};


template <typename Write>
void foo(Write) {}

int main() {
    foo(&user_stream3::write);
}

And got

clang++ -std=c++17 -Wconversion issue777_4.cpp 
issue777_4.cpp:13:5: error: no matching function for call to 'foo'
    foo(&user_stream3::write);
    ^~~
issue777_4.cpp:10:6: note: candidate template ignored: couldn't infer template argument 'Write'
void foo(Write) {}
     ^
1 error generated.

Compilation exited abnormally with code 1 at Wed May  1 08:56:58

In order to get a specific overloaded function, Function return type and parameter types are required. However, it is unpredictable. I think that there is no solution.

So, I revert the code and add pragma to avoid warnings on this place.

redboltz avatar May 01 '19 00:05 redboltz

Before I do that, I asked to stackoverflow and got answer. https://stackoverflow.com/questions/55930436/how-to-type-cast-as-template-member-function-parameter

I didn't check it yet but could be a good solution.

redboltz avatar May 01 '19 03:05 redboltz

I think that the solution works well. Could you try #778 (force updated) ?

redboltz avatar May 01 '19 05:05 redboltz

I think that the solution works well. Could you try #778 (force updated) ?

template <typename Ret, typename Cls, , typename SizeType>
void append_buffer(Ret (Cls::*)(const char*, SizeType), const char* buf, size_t len)
{
    m_stream.write(buf, static_cast<SizeType>(len));
}

I think this is nice and simplest solution. But there is still one issue and one last question. First all there is need to respect methods that can be noexcept(true). Unfortunately this not work:

template <typename Ret, typename Cls,  typename SizeType, bool NoExcept>
void append_buffer(Ret (Cls::*)(const char*, SizeType) noexcept(NoExcept), const char* buf, size_t len)
{
    m_stream.write(buf, static_cast<SizeType>(len));
}

So just need second overload:

template <typename Ret, typename Cls,  typename SizeType>
void append_buffer(Ret (Cls::*)(const char*, SizeType) noexcept(true), const char* buf, size_t len)
{
    m_stream.write(buf, static_cast<SizeType>(len));
}

And second (and last), why you allow to using any type for len parameter, but not to do same for buf parameter ?

template <bool NoExcept, typename Ret, typename Cls, typename PtrType, typename SizeType>
void append_buffer_(Ret (Cls::*)(const PtrType*, SizeType), const char* buf, size_t len)
{
    static_assert(sizeof(PtrType) == sizeof(char))

    m_stream.write(reinterpret_cast<const PtrType*>(buf), static_cast<SizeType>(len));
}

gviolator avatar May 01 '19 06:05 gviolator

I added noexcept overload. It is for C++11 or later only.

And second (and last), why you allow to using any type for len parameter, but not to do same for buf parameter ?

I believe that size_t is appropriate type for expressing buffer length. It is unsigned type. msgpack-c provides buffer type such as sbuffer and it has write() member function with size_t length. However standard library std::ostream::wirte() has signed type. So I want to support both. That is the motivation.

For buf parameter, I want to avoid strict aliasing and type-punning issue. https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule

I know C++17 introduces std::byte that is based on unsigned char. I believe that convert between unsigned char const* and char const* could be dangerous due to strict aliasing and type-punning Even if in most of platform, it is safe. I don't want to include such pointer conversion in msgpack-c.

redboltz avatar May 01 '19 07:05 redboltz

travis-ci reports error due to noexcept overload.

I wrote the following code and successfully compiled. It seems that noexcept overload isn't required. Am I missing something?

#include <cstdlib>
#include <utility>
#include <ostream>


template <typename Stream>
struct packer {
    packer(Stream& s):m_stream(s) {}
    void append_buffer(const char* buf, size_t len)
    {

        append_buffer(&Stream::write, buf, len);
    }

    template <typename Ret, typename Cls, typename SizeType>
    void append_buffer(Ret (Cls::*)(const char*, SizeType), const char* buf, size_t len)
    {
        m_stream.write(buf, static_cast<SizeType>(len));
    }
#if 0
    template <typename Ret, typename Cls,  typename SizeType>
    void append_buffer(Ret (Cls::*)(const char*, SizeType) noexcept, const char* buf, size_t len) noexcept
    {
        m_stream.write(buf, static_cast<SizeType>(len));
    }
#endif

    Stream& m_stream;
};

struct user_stream1 {
    void write(const char*, size_t) {}
};

struct user_stream2 {
    void write(const char*, std::streamsize) {}
};

struct user_stream3 {
    void write(const char* bytes, size_t size) noexcept(true) {}
    void write(const std::byte* bytes, size_t size) {}
};

#include <sstream>

int main() {

#if 1
    {
        user_stream1 s;
        packer w(s);
        char buf[] = "123";
        w.append_buffer(buf, sizeof(buf));
    }
    {
        user_stream2 s;
        packer w(s);
        char buf[] = "123";
        w.append_buffer(buf, sizeof(buf));
    }
    {
        user_stream3 s;
        packer w(s);
        char buf[] = "123";
        w.append_buffer(buf, sizeof(buf));
    }
    {
        std::stringstream s;
        packer w(s);
        char buf[] = "123";
        w.append_buffer(buf, sizeof(buf));
    }
#endif
}

redboltz avatar May 01 '19 08:05 redboltz

It seems noexcept really does not required...

gviolator avatar May 01 '19 09:05 gviolator

Merged. Thank you for your cooperation.

redboltz avatar May 01 '19 12:05 redboltz

Hi! After these changes fail to compile with error "couldn't deduce template parameter 'Ret'" (for void append_buffer(Ret (Cls::*)(const char*, SizeType), const char* buf, size_t len)) the following code with template write function:

struct Stream
{
template<typename T>
ssize_t write(const T *obj, size_t count);
};

ilnarb avatar Mar 02 '21 14:03 ilnarb

You need to write your buffer class as follows:

struct Stream
{
ssize_t write(const char *obj, size_t count);
};

See:

https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_packer#buffer

redboltz avatar Mar 02 '21 14:03 redboltz