C++ 获取ostream';s插入运算符
我有一个类,我想把它包装成一个C++ 获取ostream';s插入运算符,c++,templates,operator-overloading,wrapper,ostream,C++,Templates,Operator Overloading,Wrapper,Ostream,我有一个类,我想把它包装成一个ostringstream。我是这样做的: class Foo { ostringstream os; public: template <typename T> decltype(ostream() << T(), Foo)& operator <<(const T& param) { os << param; return *this;
ostringstream
。我是这样做的:
class Foo {
ostringstream os;
public:
template <typename T>
decltype(ostream() << T(), Foo)& operator <<(const T& param) {
os << param;
return *this;
}
}
class-Foo{
ostringstream os;
公众:
模板
decltype(ostream()std::ostream
没有默认构造函数,Foo
不是可以在decltype
中使用的表达式。相反,您可以在第一个表达式中直接使用os
。为了更容易地返回Foo&
,我会使用一个尾随的返回类型,并使用*这个
template <typename T>
auto operator<<(const T& param) -> decltype(os << param, *this);
模板
自动运算符std::ostream
没有默认构造函数,Foo
不是可以在decltype
中使用的表达式。相反,您可以在第一个表达式中直接使用os
。为了更容易地返回Foo&
,我会使用尾部返回类型并使用*this
template <typename T>
auto operator<<(const T& param) -> decltype(os << param, *this);
模板
auto operator这纯粹是基于用户的答案和您指向的链接
我不确定使用什么函数来测试重载11,但10和12是用std::hex
和std::endl
测试的
#include <iomanip>
#include <iostream>
#include <sstream>
class Foo {
private:
std::ostringstream os{};
public:
using char_type = std::ostringstream::char_type;
using traits_type = std::ostringstream::traits_type;
// generic, with perfect forwarding instead of "const T&"
template<typename T>
auto operator<<(T&& param) -> decltype(os << std::forward<T>(param), *this) {
os << std::forward<T>(param);
return *this;
}
// overload 10
Foo& operator<<(std::ios_base& (*func)(std::ios_base&)) {
func(os);
return *this;
}
// overload 11
Foo& operator<<(std::basic_ios<char_type, traits_type>& (*func)(
std::basic_ios<char_type, traits_type>&)) {
func(os);
return *this;
}
// overload 12
Foo& operator<<(std::basic_ostream<char_type, traits_type>& (*func)(
std::basic_ostream<char_type, traits_type>&)) {
func(os);
return *this;
}
auto str() { return os.str(); }
};
int main() {
Foo a;
a << "Hello Worl"; // generic
a << std::hex << 13; // 10 + generic
a << std::endl; // 12
std::cout << a.str() << "\n";
}
#包括
#包括
#包括
福班{
私人:
std::ostringstream os{};
公众:
使用char\u type=std::ostringstream::char\u type;
使用traits\u type=std::ostringstream::traits\u type;
//通用,具有完美转发,而不是“const T&”
模板
auto operator这纯粹是基于用户的答案和您指向的链接
我不确定使用什么函数来测试重载11,但10和12是用std::hex
和std::endl
测试的
#include <iomanip>
#include <iostream>
#include <sstream>
class Foo {
private:
std::ostringstream os{};
public:
using char_type = std::ostringstream::char_type;
using traits_type = std::ostringstream::traits_type;
// generic, with perfect forwarding instead of "const T&"
template<typename T>
auto operator<<(T&& param) -> decltype(os << std::forward<T>(param), *this) {
os << std::forward<T>(param);
return *this;
}
// overload 10
Foo& operator<<(std::ios_base& (*func)(std::ios_base&)) {
func(os);
return *this;
}
// overload 11
Foo& operator<<(std::basic_ios<char_type, traits_type>& (*func)(
std::basic_ios<char_type, traits_type>&)) {
func(os);
return *this;
}
// overload 12
Foo& operator<<(std::basic_ostream<char_type, traits_type>& (*func)(
std::basic_ostream<char_type, traits_type>&)) {
func(os);
return *this;
}
auto str() { return os.str(); }
};
int main() {
Foo a;
a << "Hello Worl"; // generic
a << std::hex << 13; // 10 + generic
a << std::endl; // 12
std::cout << a.str() << "\n";
}
#包括
#包括
#包括
福班{
私人:
std::ostringstream os{};
公众:
使用char\u type=std::ostringstream::char\u type;
使用traits\u type=std::ostringstream::traits\u type;
//通用,具有完美转发,而不是“const T&”
模板
自动运算符您是否尝试使用decltype(ostream())“为什么你不使用<代码> FoO和<代码>作为返回类型?”NaNaOLIVER,我的意思是,我没有定义一个替代的……但是我确实希望在调用代码中出错,如果用户试图调用一个不能插入的类型,则不在我的代码中。
实际上可以是一个std::ostream
及其所有功能。Boost.Iostreams可以提供帮助。这是一篇有点旧但很好的文章:如果没有默认的构造函数,这对t不起作用,您需要std::declval。您是否尝试使用decltype(ostream())“为什么你不使用<代码> FoO和<代码>作为返回类型?”NaNaOLIVER,我的意思是,我没有定义一个替代的……但是我确实希望在调用代码中出错,如果用户试图调用一个不能插入的类型,则不在我的代码中。
实际上可以是一个std::ostream
及其所有功能。Boost.Iostreams可以提供帮助。一篇有点老但很好的文章:如果没有默认的构造函数,这对t不起作用,您需要std::declval。这可能是我的体系结构的问题,但我似乎无法处理Foo()@JonathanMee-Well,std::endl
是一个函数模板,所以对于Foo()我想你是在告诉我,我还需要实现https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt
此列表中的运算符10、11和12。我的问题是为什么?这些只是接受函数指针的方法,对吗?不应该在我的模板中定义它们吗?@JonathanMee不,它们还没有定义Foo()的问题可能是我的体系结构的问题,但我似乎无法处理Foo()@JonathanMee的问题,std::endl
是一个函数模板,所以为了Foo()我想你是在告诉我,我还需要实现https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt
此列表中的运算符10、11和12。我的问题是为什么?这些只是接受函数指针的方法,对吗?不应该在我的模板中定义它们吗?@JonathanMee不,它们还没有定义.Foo()的问题