如何类型转换为模板成员函数参数?

How to type-cast as template member function parameter?

本文关键字:成员 函数 参数 类型转换      更新时间:2023-10-16

我实现了类模板writer.writer具有模板成员变量s_s_的类型是Stream.writer期望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));
}
}

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

运行演示: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

有没有好方法可以知道大小类型,或者在没有杂注的情况下抑制警告?

在第二个版本中获取写入函数的地址时,它不知道要获取哪个重载的地址。 您需要充分约束模板参数,以便重载不会模棱两可:

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函数,这仍然会失败。在这种情况下,您必须找到更多的约束(例如,必须const char *第一个参数),或者只需要用户代码中的特定签名。