首页 > 解决方案 > What does the ampersand do here?

问题描述

I'm trying to understand this section of code, and I've come across something I just cannot interpret:

template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
void CellBasedSimulationArchiver<ELEMENT_DIM, SIM, SPACE_DIM>::Save(SIM* pSim)
{
    // Do a bunch of stuff

    boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();

    // Archive the simulation (const-ness would be a pain here)
    (*p_arch) & pSim;  //<--------------- What is this?
}

The point of this function is to archive the state of a simulation using boost, so it can be reloaded at a later time and continued. I'm trying to understand how the archiving actually happens, but the line with the arrow baffles me. At some point the actual saving has to happen, and I guess the arrow is where it happens.

Something must be going on with boost, but what exactly does this line mean and what does the & do here? Is it some kind of reference? I looked at some documentation and it contains the << operator, but nowhere is there a &

标签: c++pointersboostreference

解决方案


这里的 & 符号是二元&运算符。对于整数类型,这将是按位与运算符(就像<<左移运算符一样),但boost::archive::text_oarchive不是整数类型。必须为此类定义了运算符重载函数。因此,此运算符将调用该函数。

根据文档

sa << x
sa & x

这些表达式必须执行完全相同的功能。他们将 x 的值与其他信息一起附加到 sa。此其他信息由存档的实现定义。通常,此信息是相应加载存档类型正确恢复 x 值所需的信息。

因此,(*p_arch) & pSim;附加pSimp_arch.


推荐阅读