xtd 0.2.0
Loading...
Searching...
No Matches
as.cpp

Shows how to use xtd::as operator.

#include <xtd/as>
#include <xtd/console>
#include <xtd/convert>
#include <xtd/random>
using namespace xtd;
auto main() -> int {
double d = 41.6;
int i1 = convert::to_int32(d);
int i2 = as<int>(d);
console::write_line("d = {0}, convert::to_int32(d) = {1}, as<int>(d) = {2}", d, i1, i2);
int i = 42;
string s1 = convert::to_string(i);
string s2 = as<string>(i);
console::write_line("i = {0}, convert::to_string(i) = {1}, as<string>(i) = {2}", i, s1, s2);
std::any a = "string";
s1 = convert::to_string(a);
s2 = as<string>(a);
console::write_line("a = {0}, convert::to_string(a) = {1}, as<string>(a) = {2}", a, s1, s2);
ptr<object> value = new_ptr<string>("a");
ptr<string> value2 = as<string>(value);
console::write_line("value = {0}, as<string>(value) = {1}", *value, *value2);
try {
console::write_line("as<string>(rand) = {0}", as<string>(rand));
} catch (const invalid_cast_exception& e) {
console::write_line("Bad cast : {0} !", e.message());
}
ptr<object> ver = new_ptr<version>(1, 1, 5);
ptr<string> str = as<string>(ver);
if (str == nullptr)
console::write_line("Bad cast!");
}
// This code produces the following output :
//
// d = 41.6, convert::to_int32(d) = 42, as<int>(d) = 42
// i = 42, convert::to_string(i) = 42, as<string>(i) = 42
// a = string, convert::to_string(a) = string, as<string>(a) = string
// value = a, as<string>(value) = a
// Bad cast : std::bad_cast !
// Bad cast!
The exception that is thrown for invalid casting or explicit conversion.
Definition invalid_cast_exception.hpp:19
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:40
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10