C++ std::ostream作为可选(!)函数参数

C++ std::ostream作为可选(!)函数参数,c++,cout,optional-parameters,ostream,C++,Cout,Optional Parameters,Ostream,我想声明一个函数,该函数默认情况下写入std::out,但也可以选择允许写入另一个输出流(如果提供)。例如: print_function(std::string & str, std::ostream & out = std::cout, std::ostream & other = nullptr) // <-- how to make it optional??? { out <&l

我想声明一个函数,该函数默认情况下写入
std::out
,但也可以选择允许写入另一个输出流(如果提供)。例如:

print_function(std::string & str, 
               std::ostream & out = std::cout, 
               std::ostream & other = nullptr) // <-- how to make it optional???
{
    out << str;
    if (other == something) // if optional 'other' argument is provided
    {
        other << str;
    }
}
打印函数(std::string和str), std::ostream&out=std::cout,
std::ostream&other=nullptr)/带指针,或
boost::可选

void print_function(std::string & str, 
               std::ostream & out = std::cout, 
               std::ostream* other = nullptr)
{
    out << str;
    if (other)
    {
        *other << str;
    }
}
void print_函数(std::string和str,
std::ostream&out=std::cout,
std::ostream*other=nullptr)
{

out您可以使用
boost::optional
或指针,如所示。但是,这两种方法都强制您在函数体中使用条件语句

以下是另一种方法,其优点是函数体的简单性:

 void print_function(std::string & str, 
              std::ostream & out = std::cout, 
              std::ostream& other = null_stream)
{
     out << str;
     other << str;  //no "if" required here.
 }
这里的
null\u流
是一个不做任何事情的
std::ostream


希望能有所帮助。

我只想使用函数重载,而不是默认参数

// declare the functions in a header

void print_function(std::string &str);
void print_function(std::string &str, std::ostream &ostr);
void print_function(std::string &str, std::ostream &ostr, std::ostream &other);

// and in some compilation unit, define them

#include "the_header"

void print_function(std::string &str)
{
       print_function(str, std::cout);
}

void print_function(std::string &str, std::ostream &ostr)
{
     // whatever
}

void print_function(std::string & str, 
                    std::ostream &ostr, 
                    std::ostream &other)
{
    print_function(str, ostr);

    other << str;
}
//在头中声明函数
void print_函数(std::string和str);
void print_函数(std::string&str,std::ostream&ost);
void print_函数(std::string&str,std::ostream&ost,std::ostream&other);
//在某些编译单元中,定义它们
#包括“标题”
void print_函数(std::string和str)
{
打印功能(str,std::cout);
}
void print_函数(std::string&str,std::ostream&ost)
{
//随便
}
void print_函数(std::string和str,
标准::奥斯特雷姆和奥斯特,
标准::奥斯特雷姆和其他)
{
打印功能(str、ostr);

其他相关:在我看来,有两个重载更为自然,一个有两个参数,一个有三个参数。@Hurkyl:或者保留一个有两个参数的重载并传递一个“
tee\u流”
”。有一个流吞没输入(如接受的答案)在这里,这是一个糟糕的解决方案。我只保留
boost::optional
方法,或者至少在指针方法之前。@Nawaz:有些人不希望依赖关系提升:-(。因此,直到
optional
在``std:`…可选引用中,我上次检查时从std中排除。或者使用C++17:
std::optional
)这也许是最简单、最直接的解决方案,也不需要boost。
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/null.hpp>

boost::iostreams::stream<boost::iostreams::null_sink> null_stream {
          boost::iostreams::null_sink{} 
};
// declare the functions in a header

void print_function(std::string &str);
void print_function(std::string &str, std::ostream &ostr);
void print_function(std::string &str, std::ostream &ostr, std::ostream &other);

// and in some compilation unit, define them

#include "the_header"

void print_function(std::string &str)
{
       print_function(str, std::cout);
}

void print_function(std::string &str, std::ostream &ostr)
{
     // whatever
}

void print_function(std::string & str, 
                    std::ostream &ostr, 
                    std::ostream &other)
{
    print_function(str, ostr);

    other << str;
}