mirror hosted by tehsausage.com
PTypes: variant: assignments and typecasts

C++ Portable Types Library (PTypes) Version 2.1


Top: Basic types: variant: Assignments and typecasts


Variants may hold values of the following types:

Variants are compatible with variables and constants of all numeric types, the string type and a pointer to component, which means, you can assign any of these values to a variant object and vice versa, including passing as parameters to functions. The type of a variant can be changed at run-time by simply assigning a new value to it.

By default variants are initialized to an unassigned state, which is the same as calling clear() for a variant object.

You may store datetime values in variants, since datetime is equivalent to large in PTypes. Besides, arbitrary binary data can be stored in a dynamic string and then assigned to a variant.

The variant class declares (or rather, overloads) all possible typecast operators to other fundamental data types. These typecast operators always try to return some value, even when it is not sensible. Below is a list of rules for typecasts that may make sense in your program, leaving the other less meaningful cases as 'unspecified'. Although variants store integers as 64-bit values, for simplicity we use the word int in this list.

Typecast operators never affect the actual value of a variant object.

When trying to cast a variant to signed or unsigned 32-bit int, the typecast operator first casts the value to 64-bit large according to the rules above, and then checks the resulting value. If the value is out of range, an exception of type (evariant*) is raised. You may always cast variants to large to get rid of extra exception handling code in your program.

See also: string, datetime, unknown & component, Arrays, Object references, Utilities


PTypes home