Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 获取ostream';s插入运算符_C++_Templates_Operator Overloading_Wrapper_Ostream - Fatal编程技术网

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()的问题