Consider:
#include <boost/any.hpp>
#include <iostream>
#include <vector>
#include <string>
int main() {
//Code snippet 1
boost::any variable(std::string("Hello "));
std::string* string_ptr_any = boost::any_cast<std::string>(&variable);
std::cout << "Pointer value is " << string_ptr_any <<", variable's address is " << &variable << std::endl;
//End Code snippet 1
//Code snippet 2
std::string normalstring = "hello_normal";
std::string* normalstringptr = &normalstring;
std::cout << "Pointer value is " << normalstringptr << ", variable's address is " << &normalstring << std::endl;
//End Code snippet 2
//Code snippet 3
std::string& reference_of_string = boost::any_cast<std::string&>(variable);
reference_of_string = "World!";
std::cout << reference_of_string << std::endl;
std::cout << boost::any_cast<std::string>(variable) << std::endl;
//End Code snippet 3
getchar();
}
Running this on compiler explorer gives as output:
Pointer value is 0xc2feb8, variable's address is 0x7ffec1620d68
Pointer value is 0x7ffec1620d40, variable's address is 0x7ffec1620d40
Hello World!
Hello World!
(Q1) As expected, Code snippet 2 gives the same value for both the address of the variable as well as the pointer (0x7ffec1620d40
in this case). But what explains the similar code in Code snippet 1 giving rise to two different addresses? (0xc2feb8
vs 0x7ffec1620d68
) What is the relationship between these two addresses in memory?
(Q2) I am unable to see a consistent syntax rule to cast a boost::any
variable. In Code snippet 1, to obtain a pointer, the syntax is:
std::string* string_ptr_any = boost::any_cast<std::string>(&variable);
while in Code snippet 3, to obtain a reference, the syntax is:
std::string& reference_of_string = boost::any_cast<std::string&>(variable);
More specifically, why is not the syntax in Code snippet 1:
std::string* string_ptr_any = boost::any_cast<std::string*>(&variable);
Is there a general rule of which these syntaxes are consistent special cases?
CodePudding user response:
But what explains the similar code in Code snippet 1 giving rise to two different addresses? (0xc2feb8 vs 0x7ffec1620d68) What is the relationship between these two addresses in memory?
string_ptr_any
is the address of the std::string
object. &variable
is the address of the boost::any
object.
why is not the syntax in Code snippet 1:
std::string* string_ptr_any = boost::any_cast<std::string*>(&variable);
Because the boost::any
doesn't contain an object of type std::string*
. You can only cast to the type that is contained, or a reference to it. This would be an alternative way to write snippet 1:
std::string* string_ptr_any = &boost::any_cast<std::string&>(variable);