提问人: 提问时间:12/22/2019 最后编辑:iammilind 更新时间:1/6/2020 访问量:1605
有没有更好的方法来重载 ostream 运算符<<?
Are there better ways to overload ostream operator<<?
问:
假设您有以下代码:
#include <iostream>
template <typename T>
class Example
{
public:
Example() = default;
Example(const T &_first_ele, const T &_second_ele) : first_(_first_ele), second_(_second_ele) { }
friend std::ostream &operator<<(std::ostream &os, const Example &a)
{
return (os << a.first_ << " " << a.second_);
}
private:
T first_;
T second_;
};
int main()
{
Example example_(3.45, 24.6); // Example<double> till C++14
std::cout << example_ << "\n";
}
这是使 ?operator<<
friend std::ostream &operator<<(std::ostream &os, const Example &a)
{
return (os << a.first_ << " " << a.second_);
}
就性能而言,这是使它重载的最佳方式,还是有更好的选择来执行此实现?
答:
这是实现它的明显方法。它也可能是最有效的。使用它。
您在问题中演示的方式是最基本的方式,这也可以在各种 C++ 书籍中找到。就我个人而言,我可能不喜欢在我的生产代码中,主要是因为:
- 必须为每个类编写样板代码。
friend operator<<
- 添加新的类成员时,可能还必须单独更新方法。
我建议从C++14开始遵循以下方式:
图书馆
// Add `is_iterable` trait as defined in https://stackoverflow.com/a/53967057/514235
template<typename Derived>
struct ostream
{
static std::function<std::ostream&(std::ostream&, const Derived&)> s_fOstream;
static auto& Output (std::ostream& os, const char value[]) { return os << value; }
static auto& Output (std::ostream& os, const std::string& value) { return os << value; }
template<typename T>
static
std::enable_if_t<is_iterable<T>::value, std::ostream&>
Output (std::ostream& os, const T& collection)
{
os << "{";
for(const auto& value : collection)
os << value << ", ";
return os << "}";
}
template<typename T>
static
std::enable_if_t<not is_iterable<T>::value, std::ostream&>
Output (std::ostream& os, const T& value) { return os << value; }
template<typename T, typename... Args>
static
void Attach (const T& separator, const char names[], const Args&... args)
{
static auto ExecuteOnlyOneTime = s_fOstream =
[&separator, names, args...] (std::ostream& os, const Derived& derived) -> std::ostream&
{
os << "(" << names << ") =" << separator << "(" << separator;
int unused[] = { (Output(os, (derived.*args)) << separator, 0) ... }; (void) unused;
return os << ")";
};
}
friend std::ostream& operator<< (std::ostream& os, const Derived& derived)
{
return s_fOstream(os, derived);
}
};
template<typename Derived>
std::function<std::ostream&(std::ostream&, const Derived&)> ostream<Derived>::s_fOstream;
用法
为那些需要设施的类继承上述类。将通过 base 自动包含在这些类的定义中。所以没有额外的工作。例如:operator<<
friend
ostream
class MyClass : public ostream<MyClass> {...};
优选地,在它们的构造函数中,可以打印要打印的成员变量。例如:Attach()
// Use better displaying with `NAMED` macro
// Note that, content of `Attach()` will effectively execute only once per class
MyClass () { MyClass::Attach("\n----\n", &MyClass::x, &MyClass::y); }
例
从你分享的内容来看,
#include"Util_ostream.hpp"
template<typename T>
class Example : public ostream<Example<T>> // .... change 1
{
public:
Example(const T &_first_ele, const T &_second_ele) : first_(_first_ele), second_(_second_ele)
{
Example::Attach(" ", &Example::first_, &Example::second_); // .... change 2
}
private:
T first_;
T second_;
};
演示
此方法对变量的每次打印都具有指针访问,而不是直接访问。从性能的角度来看,这种可以忽略不计的间接性绝不应该成为代码中的瓶颈。
出于实际目的,演示稍微复杂一些。
要求
- 这里的目的是提高打印变量的可读性和均匀性
- 无论继承如何,每个可打印类都应该有其单独的
ostream<T>
- 对象应该已定义或继承,以便能够编译
operator<<
ostream<T>
设施
现在,这是一个很好的库组件。以下是我到目前为止添加的附加设施。
- 使用宏,我们也可以以某种方式打印变量;通过修改库代码,始终可以根据需要自定义变量打印
ATTACH()
- 如果基类是可打印的,那么我们可以简单地传递一个 typecasted ;休息会照顾好
this
- 现在支持具有兼容性的容器,其中包括
std::begin/end
vector
map
开头显示的代码较短,以便快速理解。有兴趣的人可以点击上面的演示链接。
评论
Attach
operator<<
ostream<<
Attach
std::function
Attach
Attach()
friend
ostream
Attach()
friend operator<<
virtual
不需要继承。编辑。@n.'代词'm. 成员以 1 种方式打印已经在上面的链接中演示了。在最新版本中,我还添加了对类似容器的支持(还没有,但应该很容易)。对于自定义打印,可以随时定义其自定义或修改上述库。顺便说一句,担心事情(继承或方法调用)通常是过早的优化。特别是在这种情况下,I/O 操作很繁重。std::vector
map
operator<<
virtual
我相信这些评论已经很好地回答了你的问题。从纯粹的性能角度来看,可能没有“更好”的方法来使输出流的运算符过载,因为您的函数可能一开始就不是瓶颈。<<
我建议有一种“更好”的方法来编写函数本身来处理一些极端情况。
现在存在的重载将在尝试执行某些输出格式化操作时“中断”。<<
std::cout << std::setw(15) << std::left << example_ << "Fin\n";
这不会使整个输出对齐。相反,它只左对齐成员。这是因为您一次将一个项目放入流中。 将抓取下一个项目以左对齐,这只是类输出的一部分。Example
first_
std::left
最简单的方法是生成一个字符串,然后将该字符串转储到输出流中。像这样的东西:
friend std::ostream &operator<<(std::ostream &os, const Example &a)
{
std::string tmp = std::to_string(a.first_) + " " + std::to_string(a.second_);
return (os << tmp);
}
这里值得注意的是几件事。首先,在这个特定示例中,您将获得尾随 0,因为您无法控制如何格式化其值。这可能意味着编写特定于类型的转换函数来为您执行任何修整。您也可以使用(以恢复一些效率(同样,这可能无关紧要,因为功能本身可能仍然不是您的瓶颈)),但我没有使用它们的经验。std::to_string()
std::string_views
通过一次将对象的所有信息放入流中,左对齐现在将对齐对象的完整输出。
还有关于朋友与非朋友的争论。如果存在必要的吸气剂,我认为非朋友是要走的路。友元很有用,但也会破坏封装,因为它们是具有特殊访问权限的非成员函数。这进入了意见领域,但我不会写简单的 getter,除非我觉得它们是必要的,而且我不会将重载视为必要。<<
评论
据我了解,这个问题提出了两个歧义点:
您是否专门针对模板化类。
我假设答案是肯定的。是否有更好的方法来重载(与-way相比),如问题标题中发布的那样(并假设“更好”是指性能),或者有其他方法,如正文中发布的那样(“这是唯一的方法......”?
我将假设第一个,因为它包含第二个。ostream operator<<
friend
我设想了至少 3 种方法来使 :ostream operator<<
- -way,正如你发布的那样。
friend
- 非正向,具有返回类型。
friend
auto
- 非正向,具有返回类型。
friend
std::ostream
它们在底部举例说明。 我进行了几次测试。从所有这些测试中(见下面用于该测试的代码),我得出结论:
在优化模式(with )下编译/链接,并分别循环10000次后,所有3种方法都提供基本相同的性能。
-O3
std::cout
在调试模式下编译/链接,无需循环
t1 ~ 2.5-3.5 * t2 t2 ~ 1.02-1.2 * t3
即,1 比 2 和 3 慢得多,它们的性能相似。
我不知道这些结论是否适用于整个系统。 我也不知道您是否会看到更接近 1(最有可能)或 2(在特定条件下)的行为。
定义重载运算符
的三种方法的代码<<
(我删除了默认构造函数,因为它们在这里无关紧要)。
方法 1(如 OP 中所示):
template <typename T>
class Example
{
public:
Example(const T &_first_ele, const T &_second_ele) : first_(_first_ele), second_(_second_ele) { }
friend std::ostream &operator<<(std::ostream &os, const Example &a)
{
return (os << a.first_ << " " << a.second_);
}
private:
T first_;
T second_;
};
方法2:
template <typename T>
class Example2
{
public:
Example2(const T &_first_ele, const T &_second_ele) : first_(_first_ele), second_(_second_ele) { }
void print(std::ostream &os) const
{
os << this->first_ << " " << this->second_;
return;
}
private:
T first_;
T second_;
};
template<typename T>
auto operator<<(std::ostream& os, const T& a) -> decltype(a.print(os), os)
{
a.print(os);
return os;
}
方法3:
template <typename T>
class Example3
{
public:
Example3(const T &_first_ele, const T &_second_ele) : first_(_first_ele), second_(_second_ele) { }
void print(std::ostream &os) const
{
os << this->first_ << " " << this->second_;
return;
}
private:
T first_;
T second_;
};
// Note 1: If this function exists, the compiler makes it take precedence over auto... above
// If it does not exist, code compiles ok anyway and auto... above would be used
template <typename T>
std::ostream &operator<<(std::ostream &os, const Example3<T> &a)
{
a.print(os);
return os;
}
// Note 2: Explicit instantiation is not needed here.
//template std::ostream &operator<<(std::ostream &os, const Example3<double> &a);
//template std::ostream &operator<<(std::ostream &os, const Example3<int> &a);
用于测试性能
的代码(所有内容都放在一个源文件中,其中包含
#include <iostream>
#include <chrono>
在顶部):
int main()
{
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
const int nout = 10000;
Example example_(3.45, 24.6); // Example<double> till C++14
begin = std::chrono::steady_clock::now();
for (int i = 0 ; i < nout ; i++ )
std::cout << example_ << "\n";
end = std::chrono::steady_clock::now();
const double lapse1 = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
std::cout << "Time difference = " << lapse1 << "[us]" << std::endl;
Example2 example2a_(3.5, 2.6); // Example2<double> till C++14
begin = std::chrono::steady_clock::now();
for (int i = 0 ; i < nout ; i++ )
std::cout << example2a_ << "\n";
end = std::chrono::steady_clock::now();
const double lapse2a = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
std::cout << "Time difference = " << lapse2a << "[us]" << std::endl;
Example2 example2b_(3, 2); // Example2<double> till C++14
begin = std::chrono::steady_clock::now();
for (int i = 0 ; i < nout ; i++ )
std::cout << example2b_ << "\n";
end = std::chrono::steady_clock::now();
const double lapse2b = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
std::cout << "Time difference = " << lapse2b << "[us]" << std::endl;
Example3 example3a_(3.4, 2.5); // Example3<double> till C++14
begin = std::chrono::steady_clock::now();
for (int i = 0 ; i < nout ; i++ )
std::cout << example3a_ << "\n";
end = std::chrono::steady_clock::now();
const double lapse3a = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
std::cout << "Time difference = " << lapse3a << "[us]" << std::endl;
std::cout << "Time difference lapse1 = " << lapse1 << "[us]" << std::endl;
std::cout << "Time difference lapse2a = " << lapse2a << "[us]" << std::endl;
std::cout << "Time difference lapse2b = " << lapse2b << "[us]" << std::endl;
std::cout << "Time difference lapse3a = " << lapse3a << "[us]" << std::endl;
return 0;
}
评论
example*_
评论
operator<<()
friend