以下代码编译并完成了“正确的事情”:
boost::variant如何存储引用?根据C++标准,引用的存储完全取决于编译器。实际上,
#include <boost/variant.hpp>
#include <iostream>
int main()
{
int a = 10;
boost::variant<int&, float&> x = a;
a = 20;
std::cout << boost::get<int&>(x) << "\n";
return 0;
}
boost::variant如何存储引用?根据C++标准,引用的存储完全取决于编译器。实际上,
boost::variant
如何知道引用占用了多少字节呢?sizeof(T&) == sizeof(T)
,所以它不能使用sizeof()
运算符。现在,我知道引用很可能是使用指针实现的,但是语言没有保证。当变量存储引用时,如何使用get<>
和访问工作的良好解释可以获得额外的分数 :)
<< sizeof(std :: vector <char>&),sizeof(std :: vector <char>),sizeof(T); struct T {std :: vector <char>& r; };
56、56、8。 - Lightness Races in Orbit