PHP | var_dump() Function The var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure.
@JMTyler var_export returns a parsable string—essentially PHP code—while var_dump provides a raw dump of the data. So, for example, if you call var_dump on an integer with the value of 1, it would print int(1) while var_export just prints out 1 .
4. Which is true about var_dump() function? Explanation: var_dump() cuts off loop after getting the same element three times is true about var_dump() function.
var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type.
There is no such functionality in C++. You can of course write your own Dump() functions. The reason such a feature cannot be generally provided is that the C++ compilation process removes the object metadata needed to structure the dump output. You can of course display structure contents in a debugger, where such metadata is maintained in the debug information.
BTW, are you asking about C or C++? The two languages are quite different, both in features and approach, although neither has var_dump() or similar.
C++ in itself doesn't provide something like var_dump, but with libraries like Boost.Fusion and the ADAPT_STRUCT and ADAPT_ADT facility it's easily doable.
Indeed as told in the other response, the C++ compiler doesn't generate the metadata needed to generate such an output. However it is possible to generate these metadata and use a bit of template metaprogramming to use them.
That way I've implemented here an adapted_struct_printer, which can print std::container, any classes or structures, boost::variant and boost::tuple.
Now you can easily do the following :
#include <iostream>
#include <pre/json/to_json.hpp>
struct customer {
std::string name;
size_t money_spent;
std::vector<std::string> interests;
};
BOOST_FUSION_ADAPT_STRUCT(customer,
name,
money_spent,
interests)
...
customer my_customer{
"Mr. Dupond",
1000,
{"sport articles", "food", "tools"}
};
std::cout << pre::json::to_json(my_customer) << std::endl;
You can inversely with this library also do from_json to populate structures from json.
A documentation is available here : http://daminetreg.github.io/lib-cpp-pre/html/namespacepre_1_1json.html#a4325d2cdd64a7e321303fd4428f298b9
The only requirement is that you call BOOST_FUSION_ADAPT_STRUCT/BOOST_FUSION_ADAPT_ADT on your classes (See http://www.boost.org/doc/libs/1_57_0/libs/fusion/doc/html/fusion/adapted.html)
So that this example :
#include <iostream>
#include <swissarmyknife/boost/fusion/adapted_struct_printer.hpp>
#include <boost/fusion/include/define_struct.hpp>
#include <boost/variant.hpp>
#include <boost/tuple/tuple.hpp>
namespace bla {
struct someclass {
int i = 12;
int j = 15;
};
using boost::fusion::detail::operator <<;
}
BOOST_FUSION_ADAPT_STRUCT(bla::someclass,
(int, i)
(int, j)
)
BOOST_FUSION_DEFINE_STRUCT((bla), innerbim,
(std::string, mystring)
)
BOOST_FUSION_DEFINE_STRUCT((bla), bimbim,
(int, boom)
(int, bam)
(bla::innerbim, my_inner_bim)
)
typedef boost::variant<int, double, bla::innerbim> myvariant_t;
typedef boost::tuple<std::string, int, bla::innerbim, myvariant_t> my_tuple_t;
BOOST_FUSION_DEFINE_STRUCT((bla), blabla,
(bla::bimbim, bim)
(int, i)
(int, j)
(std::vector<double>, list)
(std::list<bla::bimbim>, list_of_bimbim)
(my_tuple_t, mytuple)
(myvariant_t, myvariant)
)
int main(int argc, char** argv) {
using namespace swak;
bla::blabla instance{
{22, 12, bla::innerbim{"COOL"} },
23,
43,
{2.00, 39.07, 24.05},
{
{24, 9, bla::innerbim{"FEEL GOOD"} },
{26, 14, bla::innerbim{"SO BAD"} },
},
{"Hey that's not an int", 1, bla::innerbim{"hello"}, 12},
bla::innerbim("I'm in the variant")
};
std::cout << instance << std::endl;
bla::someclass otherinstance{};
std::cout << "Other instance : " << otherinstance << std::endl;
return 0;
}
Prints out the following :
{
bim :
{
boom : 22,
bam : 12,
my_inner_bim :
{
mystring : COOL,
}
}
i : 23,
j : 43,
list : [2, 39.07, 24.05],
list_of_bimbim : [
{
boom : 24,
bam : 9,
my_inner_bim :
{
mystring : FEEL GOOD,
}
}
,
{
boom : 26,
bam : 14,
my_inner_bim :
{
mystring : SO BAD,
}
}
],
mytuple :
{
0 (Ss) : Hey that's not an int,
1 (i) : 1,
2 (N3bla8innerbimE) :
{
mystring : hello,
}
3 (N5boost7variantIidN3bla8innerbimENS_6detail7variant5void_ES5_S5_S5_S5_S5_S5_S5_S5_S5_S5_S5_S5_S5_S5_S5_S5_EE) :
{
12}
}
myvariant :
{
{
mystring : I'm in the variant,
}
}
}
Other instance :
{
i : 12,
j : 15,
}
I'm improving the implementation to get it at some point as a possible new feature in boost fusion, but it's already usable as shown there :
https://github.com/daminetreg/lib-cpp-swissarmyknife/blob/feature/adapted_struct_printer_improved/test/adapted_struct_printer.cpp
It is possible but it would take a lot of work if debug symbols were enabled and all optimisations were disabled. Also it would be slow and perhaps not very reliable[*1].
Anything that a debugger can do could be replicated by a dump() function that causes a breakpoint, and logged out information.
Some debuggers can be automated, so perhaps the dump function itself would be written in the debugger.
*1 e.g. debuggers sometimes crash when dealing with some breakpoints. e.g. the program would need to have a breakpoint and halt all threads before trying to dump data. e.g. programs that need to deal with realtime interrupts probably wouldn't work. e.g. the debugger needs to be reliable enough to deal with many many breakpoints and not introduce other issues.
In microsoft article have some solution:
https://msdn.microsoft.com/pt-br/library/7fthz5xd.aspx
template <typename T> void print_elem(const T& t) {
cout << "(" << t << ") ";
}
template <typename T> void print_collection(const T& t) {
cout << " " << t.size() << " elements: ";
for (const auto& p : t) {
print_elem(p);
}
cout << endl;
}
and the call for this:
cout << "vector data: " << endl;
print_collection(v);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With