【问题标题】:How to type-cast as template member function parameter?如何类型转换为模板成员函数参数?
【发布时间】:2019-05-01 00:56:24
【问题描述】:

我实现了类模板writerwriter 具有模板成员变量 s_s_ 的类型是 Streamwriter 期望 Stream 有一个可以使用参数 const char* bufsize_t len 调用的成员函数。

这是writer的第一个版本:

// clang++ -Wconversion test.cpp

#include <cstddef>
#include <ostream>

template <class F> struct size_arg_type;

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

template <typename Stream>
struct writer {
    writer(Stream& s):s_(s) {}

    void write(const char* buf, size_t len) {
        // The type of 2nd parameter depends on Stream
        s_.write(buf, len);
    }

    Stream& s_;
};

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*, size_t) {}
    void write() {}
};

#include <sstream>

int main() {
    {   // size_type is size_t
        user_stream1 s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
    {   // size_type is std::streamsize
        user_stream2 s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
#if 1
    {   // size_type is size_t but has overloaded member function
        user_stream3 s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
#endif
    {   // size_type is std::streamsize return type is std::ostream&
        std::stringstream s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
}

运行演示:https://wandbox.org/permlink/JtEHDG3plWxe4vwB

如果我将 -Wconversion 标志设置为 clang++,我会收到以下警告。

clang++ -std=c++17 -Wconversion test.cpp 
test.cpp:18:23: warning: implicit conversion changes signedness: 'size_t' (aka 'unsigned long') to
      'std::streamsize' (aka 'long') [-Wsign-conversion]
        s_.write(buf, len);
           ~~~~~      ^~~
test.cpp:50:11: note: in instantiation of member function 'writer<user_stream2>::write' requested here
        w.write(buf, sizeof(buf));
          ^
test.cpp:18:23: warning: implicit conversion changes signedness: 'size_t' (aka 'unsigned long') to
      'std::streamsize' (aka 'long') [-Wsign-conversion]
        s_.write(buf, len);
           ~~~~~      ^~~
test.cpp:64:11: note: in instantiation of member function 'writer<std::__cxx11::basic_stringstream<char>
      >::write' requested here
        w.write(buf, sizeof(buf));
          ^
2 warnings generated.

Compilation finished at Wed May  1 09:37:37

我试图找到一种在没有编译指示的情况下抑制警告的方法。 我想出了static_cast 的方法。为了做static_cast,我需要知道第二个参数类型。

所以我实现了一些参数类型提取器:

// clang++ -std=c++17 -Wconversion test.cpp

#include <cstddef>
#include <ostream>

template <class F> struct size_arg_type;

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

template <typename Stream>
struct writer {
    writer(Stream& s):s_(s) {}

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

    template <typename Write>
    void write_impl(Write, const char* buf, size_t len)
    {
        s_.write(buf, static_cast<typename size_arg_type<Write>::type>(len));
    }

    Stream& s_;
};

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*, size_t) {}
    void write() {}
};

#include <sstream>

int main() {
    {   // size_type is size_t
        user_stream1 s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
    {   // size_type is std::streamsize
        user_stream2 s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
#if 1
    {   // size_type is size_t but has overloaded member function
        user_stream3 s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
#endif
    {   // size_type is std::streamsize return type is std::ostream&
        std::stringstream s;
        writer w(s);
        char buf[] = "123";
        w.write(buf, sizeof(buf));
    }
}

它按我的预期工作。但是在user_stream3重载了write()成员函数的情况下,就会发生编译错误。

运行演示:https://wandbox.org/permlink/TDPlQ3nXzIKjSlhY

为了获得特定的成员函数重载,我需要知道完整的成员函数指针类型。但是,它是不可预测的。

    clang++ -std=c++17 -Wconversion test.cpp 
    test.cpp:18:9: error: no matching member function for call to 'write_impl'
            write_impl(&Stream::write, buf, len);
            ^~~~~~~~~~
    test.cpp:63:11: note: in instantiation of member function 'writer<user_stream3>::write' requested here
            w.write(buf, sizeof(buf));
              ^
    test.cpp:22:10: note: candidate template ignored: couldn't infer template argument 'Write'
        void write_impl(Write, const char* buf, size_t len)
             ^
    1 error generated.

    Compilation exited abnormally with code 1 at Wed May  1 09:48:42

有什么好方法可以知道大小类型,或者在没有编译指示的情况下抑制警告?

【问题讨论】:

  • 要求流类中的所有write 函数具有相同的参数类型不是更容易吗?否则有人可能想创建一个write(const void *, short); 方法。

标签: c++ casting overloading member-functions


【解决方案1】:

在第二个版本中获取 write 函数的地址时,它不知道要获取哪个重载地址。您需要充分限制模板参数,以免重载不明确:

template <typename Stream>
struct writer {
    writer(Stream& s):s_(s) {}

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

    template <typename Ret, typename Cls, typename T1, typename T2>
    void write_impl(Ret (Cls::*)(T1, T2), const char* buf, size_t len)
    {
        s_.write(buf, static_cast<T2>(len));
    }

    Stream& s_;
};

https://wandbox.org/permlink/7Qb6xAoUQPRz3o2u

但是,如果有多个 2 参数 write 函数,这仍然会失败。在这种情况下,您必须找到更多约束(例如,第一个 arg 必须是 const char *)或者只需要用户代码的特定签名。

【讨论】:

  • 谢谢!这是我需要的东西。
猜你喜欢
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 2019-11-10
相关资源
最近更新 更多