c++ boost::any 定义我自己的 print ,
我正在努力寻找如何使用 boost::any
创建一个可以首先使用模板打印任何类型的打印函数。
template <typename T>
struct printer {
void print(ostream& os, const boost::any& a);
};
我需要首先定义 print()
。 我希望对any有真正的运算符<<
,这个想法很简单:将类的实例附加到每个any对象 printer
具有合适的 T,并在 any
的值类型更改时更改此对象。 第一个技术问题是打印机对象依赖于 T,而 any 不是(也不应该是)类模板。
拜托,我真的需要今晚或明天的帮助,我明天有截止日期,但我希望今晚就可以完成。
Am struggling a lot to find how to do to use boost::any
to create a print function that can print any type using template first.
template <typename T>
struct printer {
void print(ostream& os, const boost::any& a);
};
I need to define first print()
.
i wish to have the real operator <<
for any, The idea is simple: attach to each any object an instance of classprinter<T>
with the suitable T and change this object when the value type of the any
changes.
A first technical problem is that the printer object depends on T whereas any is not (and should not be) a class template.
Please I really need a hand is for tonight or tomorrow I have a deadline for tomorrow but I wish to work on it tonight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个非常简单的方法可以做到这一点,如“超越 C++ 标准库:简介”中所述Boost”:
然后使用
any_out
而不是boost::any
。There is quite easy way to do this, described in "Beyond the C++ Standard Library: An Introduction to Boost":
and then you use
any_out
instead ofboost::any
.我这样做,我认为这是干净和安全的:
any_extension.hpp:
any_extension.cpp:
对于您想要支持的任何类型,只需确保已为其类型和运算符调用 AnyWriter::register() 即可;<为它而存在。
例如:
any_test.cpp:
输出:
咕!
I do it like this, which I think is clean and safe:
any_extension.hpp:
any_extension.cpp:
For any type that you'd like supported, simply ensure that AnyWriter::register() has been called for its type and an operator<< exists for it.
For example:
any_test.cpp:
output:
cluck!
Pawel Zubrycki 的回答非常好(提到 Björn Karlsson 的书)。
但代码在以下几行中存在一些错误:
这是 Pawel Zubrycki 答案的正确版本,该版本有效(部分......)
此测试代码有效:
但是!!!
以下内容< strong>不起作用:
这里什么也没打印。
为什么??
那么在下面的构造函数中,类型就会混乱
那么,如果我们将 Streamer 实例化为从
构造函数中删除引用,
,即...是一种解决方案。
另一个解决方案是保留引用并调整
streamer_impl
的模板实例化。参见下文以下推荐解决方案带来的是:
上面带来一些麻烦的测试代码现在可以很好地工作(使用“推荐解决方案”):
Very nice answer by Pawel Zubrycki (mentioning Björn Karlsson's book).
But the code has a few errors in the following lines:
Here's a corrected version of Pawel Zubrycki's answer that works (partially...)
This test-code works:
However!!!!
The following does not work:
Here nothing gets printed.
Why??
Well the type is messed up, in the following constructor
if we then instantiate streamer as
Removing the reference from the constructor, i.e.
... is one solution.
Another solution is to leave the reference and tweak the template instantiation of
streamer_impl
. See belowWhich brings as to the following recommened solution is:
The test-code that gave some trouble above, now works nicely (with the "recommeded solution"):
在 Boost 邮件列表中查看此主题:
http://lists.boost.org/Archives/boost/2005/01 /79232.php
它有一些想法,其中一些看起来不错,而另一些(对我来说)则不然。不过,总的来说,这似乎是一项很难以一般方式完成的任务,因为(如该线程中提到的),某些类型永远不会是 ostream'able,但可以包含在 boost::any 中代码>对象。
Check out this thread on the Boost mailing list:
http://lists.boost.org/Archives/boost/2005/01/79232.php
It has a few ideas, some of which seem sort of OK and some of which don't (to me). Overall, though, this seems like a difficult task to accomplish in a general way, since (as mentioned in that thread), some types will never be ostream'able, yet could be contained in a
boost::any
object.