如何类型转换为模板成员函数参数?
How to type-cast as template member function parameter?
我实现了类模板writer
.writer
具有模板成员变量s_
。s_
的类型是Stream
.writer
期望Stream
有一个成员函数,可以使用参数const char* buf
和size_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 *
第一个参数),或者只需要用户代码中的特定签名。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法