Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Macros with modifiers

Return type modifiers
Filtering modifiers
Identifier modifiers
Splitting modifiers
Index modifiers
Modifiers and the single-element sequence

The basic functionality for VMD macros parsing data types has been given using the required parameters of those macros. This basic functionality may be perfectly adequate for macro programmers to use VMD effectively in their programming efforts.

A number of those macros take optional parameters, called in general "modifiers", which enhance or change the functionality of those macros in various ways. All modifiers are VMD identifiers.

In all situations modifiers are optional parameters which are parsed by VMD to provide enhanced functionality for some of its macros. They are never required as part of the basic functionality of a macro.

When modifiers are used as optional arguments to a macro they can be input after the required parameters in any order and VMD will still handle the optional parameters correctly.

There are two general types of modifiers, 'specific modifiers' and 'user-defined modifiers'. Specific modifers start with BOOST_VMD_ and are both registered and pre-detected identifiers known to VMD. The specific modifiers change the expansion of particular macros in various ways which will be subsequently explained. User-defined modifiers are user-defined identifiers which the end-user of specific macros must register and pre-detect for himself. They also change the expansion of particular macros in various ways which will be subsequently explained.

For any particular macro if a specific modifier is not appropriate it is just ignored. This means that VMD never generates a preprocessing error or gives an incorrect result just because a specific modifier does not apply for a particular macro. Any modifier which is not recognized as a specific modifier is treated as a user-defined modifier. In cases where a user-defined modifier is not appropriate it is also just ignored.

The situations where modifiers can be used to enhance the basic functionality of VMD macros can be divided by particular types of specific modifiers. Each particular type of a specific modifier has a name given to it, functionality, and set of identifiers associated with that particular type. Each particular type of a specific modifier may be used as optional parameters in one or more designated macros depending on the specific modifier type.

When more than one mutually exclusive specific modifier from a particular type of modifier is specified as an optional parameter the last specified takes effect. This allows the programmer to override a specific modifier by adding the overridden identifier as an optional argument to the end of the macro's invocation.

Header files for specific modifiers are automatically included when the header files for macros taking those specific modifiers are included.

Header files for user-defined modifiers, which register and pre-detect those user-defined modifiers, must be included as needed by the programmer using those modifiers.

The following topics will explain each particular type of modifier and where it may be used.

A number of macros are capable of returning the type of data as a v-type rather than, or along with, the data itself. The most obvious of these is BOOST_VMD_GET_TYPE which generically returns the type of the input.

Return type modifiers turn on, turn off, or change the type of the data returned in some way.

These modifiers are:

  • BOOST_VMD_RETURN_NO_TYPE, do not return the type of data.
  • BOOST_VMD_RETURN_TYPE, return the type of data parsing any tuple-like syntactical construct as its most specific type. This means that any tuple-like construct is parsed first as a possible list, next as a possible array if it is not a list, and finally as a tuple if it is not a list or an array.
  • BOOST_VMD_RETURN_TYPE_LIST, parse any tuple-like syntactical construct first as a possible list and only then as a tuple if it is not a list.
  • BOOST_VMD_RETURN_TYPE_ARRAY, parse any tuple-like syntactical construct first as a possible array and only then as a tuple if it is not an array.
  • BOOST_VMD_RETURN_TYPE_TUPLE, parse any tuple-like syntactical construct only as a tuple.

When VMD parses input generically it must determine the type of each data element of the input. For nearly all of the VMD data types this is never a problem. For the array or list data types this can be a problem, as explained when discussing parsing arrays and lists respectively using the specific macros BOOST_VMD_IS_ARRAY and BOOST_VMD_IS_LIST. The problem is that a valid tuple can be an invalid list or an invalid array, whose parsing as the more specific type will lead to UB. Because of this when VMD parses input generically, and only the data of an element is needed to continue parsing correctly, it parses all tuple-like data as a tuple and never as a list or an array.

When VMD parses input generically, and the type of the data is required in some way as part of the return of a macro, VMD by default parses for the most specific type of each data element in order to return the most accurate type. In this situation by default the BOOST_VMD_RETURN_TYPE modifier is internally in effect without having to be specified.

If more than one of the return type modifiers are specified as optional parameters the last one specified is in effect.

Usage with BOOST_VMD_GET_TYPE

The only macro in which VMD without the use of modifiers is being asked to return the type of data is BOOST_VMD_GET_TYPE. For this macro the BOOST_VMD_RETURN_TYPE modifier is internally in effect so if no return type modifiers are input as optional parameters BOOST_VMD_GET_TYPE looks for the most specific type.

For the BOOST_VMD_GET_TYPE macro the optional return type modifier BOOST_VMD_RETURN_NO_TYPE, if specified, is always ignored since the purpose of BOOST_VMD_GET_TYPE is solely to return the v-type.

Let's look at how this works with BOOST_VMD_GET_TYPE by specifying VMD sequences that have tuples which may or may not be valid lists or arrays.

#include <boost/vmd/get_type.hpp>

#define TUPLE_IS_ARRAY (2,(3,4))
#define TUPLE_IS_LIST (anydata,BOOST_PP_NIL)
#define TUPLE_IS_LIST_OR_ARRAY (2,(3,BOOST_PP_NIL))
#define TUPLE_BUT_INVALID_ARRAY (&2,(3,4))
#define TUPLE_BUT_INVALID_LIST (anydata,^BOOST_PP_NIL)
#define SEQUENCE_EMPTY
#define SEQUENCE_MULTI TUPLE_BUT_INVALID_ARRAY TUPLE_BUT_INVALID_LIST

BOOST_VMD_GET_TYPE(TUPLE_IS_ARRAY) will return BOOST_VMD_TYPE_ARRAY, the most specific type
BOOST_VMD_GET_TYPE(TUPLE_IS_ARRAY,BOOST_VMD_RETURN_TYPE_TUPLE) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_IS_ARRAY,BOOST_VMD_RETURN_TYPE_ARRAY) will return BOOST_VMD_TYPE_ARRAY
BOOST_VMD_GET_TYPE(TUPLE_IS_ARRAY,BOOST_VMD_RETURN_TYPE_LIST) will return BOOST_VMD_TYPE_TUPLE

BOOST_VMD_GET_TYPE(TUPLE_IS_LIST) will return BOOST_VMD_TYPE_LIST, the most specific type
BOOST_VMD_GET_TYPE(TUPLE_IS_LIST,BOOST_VMD_RETURN_TYPE_TUPLE) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_IS_LIST,BOOST_VMD_RETURN_TYPE_ARRAY) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_IS_LIST,BOOST_VMD_RETURN_TYPE_LIST) will return BOOST_VMD_TYPE_LIST

BOOST_VMD_GET_TYPE(TUPLE_IS_LIST_OR_ARRAY) will return BOOST_VMD_TYPE_LIST, the most specific type
BOOST_VMD_GET_TYPE(TUPLE_IS_LIST_OR_ARRAY,BOOST_VMD_RETURN_TYPE_TUPLE) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_IS_LIST_OR_ARRAY,BOOST_VMD_RETURN_TYPE_ARRAY) will return BOOST_VMD_TYPE_ARRAY
BOOST_VMD_GET_TYPE(TUPLE_IS_LIST_OR_ARRAY,BOOST_VMD_RETURN_TYPE_LIST) will return BOOST_VMD_TYPE_LIST

BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_ARRAY) will give UB
BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_ARRAY,BOOST_VMD_RETURN_TYPE_TUPLE) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_ARRAY,BOOST_VMD_RETURN_TYPE_ARRAY) will give UB
BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_ARRAY,BOOST_VMD_RETURN_TYPE_LIST) will return BOOST_VMD_TYPE_TUPLE

BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_LIST) will give UB
BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_LIST,BOOST_VMD_RETURN_TYPE_TUPLE) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_LIST,BOOST_VMD_RETURN_TYPE_ARRAY) will return BOOST_VMD_TYPE_TUPLE
BOOST_VMD_GET_TYPE(TUPLE_BUT_INVALID_LIST,BOOST_VMD_RETURN_TYPE_LIST) will give UB

BOOST_VMD_GET_TYPE(SEQUENCE_EMPTY)
   will always return BOOST_VMD_TYPE_EMPTY even if we add any return type modifiers
BOOST_VMD_GET_TYPE(SEQUENCE_MULTI)
   will always return BOOST_VMD_TYPE_SEQUENCE even if we add any return type modifiers
Usage with sequence converting macros

The sequence converting macros converts a sequence to a composite Boost PP data type or to variadic data, where each element's data in the sequence becomes an element in the destination composite type. The macros are:

  • BOOST_VMD_TO_ARRAY, converts the sequence to an array
  • BOOST_VMD_TO_LIST, converts the sequence to a list
  • BOOST_VMD_TO_SEQ, converts the sequence to a seq
  • BOOST_VMD_TO_TUPLE, converts the sequence to a tuple
  • BOOST_VMD_ENUM, converts the sequence to variadic data

When it does the conversion, using just the required parameter of the sequence itself, it converts only the data value of each sequence element to the elements of a composite Boost PP data type or variadic data. Because it needs only the data value of each sequence element it determines the type of each sequence element as the most general type that it can be. This means that all tuple-like data are parsed as tuples rather than as possible lists or arrays.

Using a return type modifier we can convert from a VMD sequence to a Boost PP composite data type or variadic data and retain the type of data of each element in the sequence as part of the conversion. When doing this each of the converted elements of the composite data type becomes a two-element tuple where the first element is the type of the data and the second element is the data itself.

For the sequence conversion macros the default return type modifier internally set is BOOST_VMD_RETURN_NO_TYPE, which means that the type is not retained. By specifying another optional return type modifier we tell the conversion to preserve the type in the conversion output.

If the sequence is empty, since there are no sequence elements, any return type modifier we use accomplishes nothing but is fine to use.

First we show how sequence conversion macros work with the BOOST_VMD_RETURN_TYPE modifier, which always parses for the most specific type.

#include <boost/vmd/enum.hpp>
#include <boost/vmd/to_array.hpp>
#include <boost/vmd/to_list.hpp>
#include <boost/vmd/to_seq.hpp>
#include <boost/vmd/to_tuple.hpp>

#define BOOST_VMD_REGISTER_ANID (ANID)
#define SEQUENCE_EMPTY_1
#define SEQUENCE_SINGLE 35
#define SEQUENCE_SINGLE_ID ANID
#define SEQUENCE_SINGLE_ARRAY (3,(0,1,2))
#define SEQUENCE_SINGLE_LIST (data,(more_data,BOOST_PP_NIL))
#define SEQUENCE_MULTI_1 (0,1) (2)(3)(4)
#define SEQUENCE_MULTI_2 BOOST_VMD_TYPE_SEQ (2,(5,6))

BOOST_VMD_TO_ARRAY(SEQUENCE_EMPTY_1) will return an empty array '(0,())'
BOOST_VMD_TO_ARRAY(SEQUENCE_EMPTY_1,BOOST_VMD_RETURN_TYPE) will return an empty array '(0,())'

BOOST_VMD_TO_LIST(SEQUENCE_SINGLE) will return a one-element list '(35,BOOST_PP_NIL)'
BOOST_VMD_TO_LIST(SEQUENCE_SINGLE,BOOST_VMD_RETURN_TYPE)
   will return a one-element list '((BOOST_VMD_TYPE_NUMBER,35),BOOST_PP_NIL)'

BOOST_VMD_TO_SEQ(SEQUENCE_SINGLE_ID) will return a one-element seq '(ANID)'
BOOST_VMD_TO_SEQ(SEQUENCE_SINGLE_ID,BOOST_VMD_RETURN_TYPE)
   will return a one-element seq '((BOOST_VMD_TYPE_IDENTIFIER,ANID))'

BOOST_VMD_TO_TUPLE(SEQUENCE_SINGLE_ARRAY) will return a single element tuple '((3,(0,1,2)))'
BOOST_VMD_TO_TUPLE(SEQUENCE_SINGLE_ARRAY,BOOST_VMD_RETURN_TYPE)
   will return a single element tuple '((BOOST_VMD_TYPE_ARRAY,(3,(0,1,2))))'

BOOST_VMD_ENUM(SEQUENCE_SINGLE_LIST) will return the single-element '(data,(more_data,BOOST_PP_NIL))'
BOOST_VMD_ENUM(SEQUENCE_SINGLE_LIST,BOOST_VMD_RETURN_TYPE)
   will return the single element '(BOOST_VMD_TYPE_LIST,(data,(more_data,BOOST_PP_NIL)))'

BOOST_VMD_TO_TUPLE(SEQUENCE_MULTI_1) will return a multi-element tuple '((0,1),(2)(3)(4))'
BOOST_VMD_TO_TUPLE(SEQUENCE_MULTI_1,BOOST_VMD_RETURN_TYPE)
   will return a multi-element tuple '((BOOST_VMD_TYPE_TUPLE,(0,1)),(BOOST_VMD_TYPE_SEQ,(2)(3)(4)))'

BOOST_VMD_ENUM(SEQUENCE_MULTI_2) will return multi-element variadic data 'BOOST_VMD_TYPE_SEQ,(2,(5,6))'
BOOST_VMD_ENUM(SEQUENCE_MULTI_2,BOOST_VMD_RETURN_TYPE)
   will return multi-element variadic data '(BOOST_VMD_TYPE_TYPE,BOOST_VMD_TYPE_SEQ),(BOOST_VMD_TYPE_ARRAY,(2,(5,6)))'

Lets look at how we might use other return type modifiers when doing conversions to avoid UB if we want the type as part of the conversion but the type might be a valid tuple while being an invalid list or array.

#define TUPLE_IS_VALID_ARRAY (2,(3,4))
#define TUPLE_IS_VALID_LIST (anydata,BOOST_PP_NIL)
#define TUPLE_BUT_INVALID_ARRAY_2 (&2,(3,4))
#define TUPLE_BUT_INVALID_LIST_2 (anydata,^BOOST_PP_NIL)

#define SEQUENCE_MULTI_T1 TUPLE_IS_VALID_ARRAY TUPLE_IS_VALID_LIST
#define SEQUENCE_MULTI_T2 TUPLE_BUT_INVALID_ARRAY_2 TUPLE_IS_VALID_LIST
#define SEQUENCE_MULTI_T3 TUPLE_IS_VALID_ARRAY TUPLE_BUT_INVALID_LIST_2
#define SEQUENCE_MULTI_T4 TUPLE_BUT_INVALID_ARRAY_2 TUPLE_BUT_INVALID_LIST_2

We present a number of uses of various sequence conversions with each of our four different sequences, and show their results.

#include <boost/vmd/to_seq.hpp>

BOOST_VMD_TO_SEQ(SEQUENCE_MULTI_T1,BOOST_VMD_RETURN_TYPE)
   will return the seq '((BOOST_VMD_TYPE_ARRAY,(2,(3,4)))) ((BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL)))'
BOOST_VMD_TO_SEQ(SEQUENCE_MULTI_T1,BOOST_VMD_RETURN_TYPE_ARRAY)
   will return the seq '((BOOST_VMD_TYPE_ARRAY,(2,(3,4)))) ((BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL)))'
BOOST_VMD_TO_SEQ(SEQUENCE_MULTI_T1,BOOST_VMD_RETURN_TYPE_LIST)
   will return the seq '((BOOST_VMD_TYPE_TUPLE,(2,(3,4)))) ((BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL)))'
BOOST_VMD_TO_SEQ(SEQUENCE_MULTI_T1,BOOST_VMD_RETURN_TYPE_TUPLE)
   will return the seq '((BOOST_VMD_TYPE_TUPLE,(2,(3,4)))) ((BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL)))'

The SEQUENCE_MULTI_T1 is a valid array followed by a valid list. All return type modifiers produce their results without any UBs.

#include <boost/vmd/to_tuple.hpp>

BOOST_VMD_TO_TUPLE(SEQUENCE_MULTI_T2,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid array as an array
BOOST_VMD_TO_TUPLE(SEQUENCE_MULTI_T2,BOOST_VMD_RETURN_TYPE_ARRAY)
   will produce UB when attempting to parse the invalid array as an array
BOOST_VMD_TO_TUPLE(SEQUENCE_MULTI_T2,BOOST_VMD_RETURN_TYPE_LIST)
   will return the tuple '((BOOST_VMD_TYPE_TUPLE,(&2,(3,4))),(BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL)))'
BOOST_VMD_TO_TUPLE(SEQUENCE_MULTI_T2,BOOST_VMD_RETURN_TYPE_TUPLE)
   will return the tuple '((BOOST_VMD_TYPE_TUPLE,(&2,(3,4))),(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL)))'

The SEQUENCE_MULTI_T2 is an invalid array, but valid tuple, followed by a valid list.

In sequence conversion we will get UB whenever we use a return type modifier that parses the data type of the invalid array as an array. But if we use the return type modifiers BOOST_VMD_RETURN_TYPE_LIST or BOOST_VMD_RETURN_TYPE_TUPLE we are never parsing the invalid array as an array but as a tuple instead and therefore we successfully return the type of the invalid array as a BOOST_VMD_TYPE_TUPLE.

#include <boost/vmd/to_array.hpp>

BOOST_VMD_TO_ARRAY(SEQUENCE_MULTI_T3,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid list as a list
BOOST_VMD_TO_ARRAY(SEQUENCE_MULTI_T3,BOOST_VMD_RETURN_TYPE_LIST)
   will produce UB when attempting to parse the invalid list as a list
BOOST_VMD_TO_ARRAY(SEQUENCE_MULTI_T3,BOOST_VMD_RETURN_TYPE_ARRAY)
   will return the array '(2,((BOOST_VMD_TYPE_ARRAY,(2,(3,4))),(BOOST_VMD_TYPE_TUPLE,(anydata,^BOOST_PP_NIL))))'
BOOST_VMD_TO_ARRAY(SEQUENCE_MULTI_T3,BOOST_VMD_RETURN_TYPE_TUPLE)
   will return the array '(2,((BOOST_VMD_TYPE_TUPLE,(2,(3,4))),(BOOST_VMD_TYPE_TUPLE,(anydata,^BOOST_PP_NIL))))'

The SEQUENCE_MULTI_T3 is a valid array followed by an invalid list, but a valid tuple.

In sequence conversion we will get UBs whenever we use a return type modifier that parses the data type of the invalid list as a list. But if we use the return type modifiers BOOST_VMD_RETURN_TYPE_ARRAY or BOOST_VMD_RETURN_TYPE_TUPLE we are never parsing the invalid list as a list but as a tuple instead and therefore we successfully return the type of the invalid list as a BOOST_VMD_TYPE_TUPLE.

#include <boost/vmd/to_list.hpp>

BOOST_VMD_TO_LIST(SEQUENCE_MULTI_T4,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid array or invalid list
BOOST_VMD_TO_LIST(SEQUENCE_MULTI_T4,BOOST_VMD_RETURN_TYPE_ARRAY)
   will produce UB when attempting to parse the invalid array
BOOST_VMD_TO_LIST(SEQUENCE_MULTI_T4,BOOST_VMD_RETURN_TYPE_LIST)
   will produce UB when attempting to parse the invalid list
BOOST_VMD_TO_LIST(SEQUENCE_MULTI_T4,BOOST_VMD_RETURN_TYPE_TUPLE)
   will return the list '((BOOST_VMD_TYPE_TUPLE,(&2,(3,4))),((BOOST_VMD_TYPE_TUPLE,(anydata,^BOOST_PP_NIL)),BOOST_PP_NIL))'

The SEQUENCE_MULTI_T4 is an invalid array, but valid tuple, followed by an invalid list, but valid tuple.

In sequence conversion we will get UBs whenever we use a return type modifier that parses the sequence other than looking for only valid tuples. So here we must use the return type modifier BOOST_VMD_RETURN_TYPE_TUPLE for a sequence conversion without generating UBs.

Usage with BOOST_VMD_ELEM

The default functionality of BOOST_VMD_ELEM when the required parameters are used is to return the particular element's data. When BOOST_VMD_ELEM does this it parses all elements of the sequence by determining the most general type of data for each element. The parsing algorithm stops when it reaches the element number whose data is returned. This means that all tuple-like data are parsed as tuples rather than as possible lists or arrays.

Using return type modifiers as optional parameters we can tell BOOST_VMD_ELEM to return the type of the element found, as well as its data, in the form of a two element tuple. The first element of the tuple is the type of the data and the second element of the tuple is the data itself.

When we use a return type modifier with BOOST_VMD_ELEM, which tells us to return the type of the data along with the data, the particular modifier only tells BOOST_VMD_ELEM how to parse the type of data for the element found. BOOST_VMD_ELEM will continue to parse elements in the sequence preceding the element found by determining the most general type of the data since this is the safest way of parsing the data itself.

Using the return type modifier BOOST_VMD_RETURN_TYPE with BOOST_VMD_ELEM is perfectly safe as long as the particular element found is not an invalid list or array, but a valid tuple. It is when the element found may be an invalid list or invalid array that we must use other return type modifiers in order to parse the type of the element correctly.

#include <boost/vmd/elem.hpp>

#define BOOST_VMD_REGISTER_ANID_E (ANID_E)
#define SEQUENCE_SINGLE_E 35
#define SEQUENCE_SINGLE_E2 ANID_E
#define SEQUENCE_MULTI_E (0,1) (2)(3)(4)
#define SEQUENCE_MULTI_E_2 BOOST_VMD_TYPE_SEQ (2,(5,6))

BOOST_VMD_ELEM(0,SEQUENCE_SINGLE_E) will return '35'
BOOST_VMD_ELEM(0,SEQUENCE_SINGLE_E,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_NUMBER,35)'

BOOST_VMD_ELEM(0,SEQUENCE_SINGLE_E2) will return 'ANID_E'
BOOST_VMD_ELEM(0,SEQUENCE_SINGLE_E2,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_IDENTIFIER,ANID_E)'

BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E) will return '(2)(3)(4)'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_SEQ,(2)(3)(4))'

BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E_2) will return 'BOOST_VMD_TYPE_SEQ'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E_2,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_TYPE,BOOST_VMD_TYPE_SEQ)'

When we use the other return type modifiers with BOOST_VMD_ELEM we do so because the element we want may be an invalid list or an invalid array but a valid tuple and yet we still want its type returned as part of the result.

Let's look at how this works with BOOST_VMD_ELEM by specifying VMD sequences that have tuples which are in the form of arrays or lists which cannot be parsed as such by VMD without generating UBs.

#define TUPLE_IS_VALID_ARRAY_E (2,(3,4))
#define TUPLE_IS_VALID_LIST_E (anydata,BOOST_PP_NIL)
#define TUPLE_BUT_INVALID_ARRAY_E (&2,(3,4))
#define TUPLE_BUT_INVALID_LIST_E (anydata,^BOOST_PP_NIL)

#define SEQUENCE_MULTI_E1 TUPLE_IS_VALID_ARRAY_E TUPLE_IS_VALID_LIST_E
#define SEQUENCE_MULTI_E2 TUPLE_BUT_INVALID_ARRAY_E TUPLE_IS_VALID_LIST_E
#define SEQUENCE_MULTI_E3 TUPLE_IS_VALID_ARRAY_E TUPLE_BUT_INVALID_LIST_E
#define SEQUENCE_MULTI_E4 TUPLE_BUT_INVALID_ARRAY_E TUPLE_BUT_INVALID_LIST_E

We present a number of uses of BOOST_VMD_ELEM with each of our four different sequences, and show their results.

#include <boost/vmd/elem.hpp>

BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_ARRAY,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE_ARRAY) will return '(BOOST_VMD_TYPE_ARRAY,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE_LIST) will return '(BOOST_VMD_TYPE_TUPLE,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(2,(3,4)))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE_ARRAY) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE_LIST) will return '(BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E1,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'

The SEQUENCE_MULTI_E1 is a valid array followed by a valid list. All return type modifiers produce their results without any UBs.

#include <boost/vmd/elem.hpp>

BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid array as an array
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE_ARRAY)
   will produce UB when attempting to parse the invalid array as an array
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE_LIST) will return '(BOOST_VMD_TYPE_TUPLE,(&2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(&2,(3,4)))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE_ARRAY) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE_LIST) will return '(BOOST_VMD_TYPE_LIST,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E2,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'

The SEQUENCE_MULTI_E2 is an invalid array, but valid tuple, followed by a valid list.

When we access element 0 of our sequence, and use a return type modifier that parses its data type as an array we will get UB. But if we use the return type modifiers BOOST_VMD_RETURN_TYPE_LIST or BOOST_VMD_RETURN_TYPE_TUPLE we are never parsing the invalid array as an array but as a tuple instead and therefore we successfully return the type of the invalid array as a BOOST_VMD_TYPE_TUPLE.

When we access element 1 of our sequence, which is both a valid list and a valid tuple, we will never get UB. We will get the return type of the element based on which return type modifier we use.

#include <boost/vmd/elem.hpp>

BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE) will return '(BOOST_VMD_TYPE_ARRAY,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE_ARRAY) will return '(BOOST_VMD_TYPE_ARRAY,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE_LIST) will return '(BOOST_VMD_TYPE_TUPLE,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(2,(3,4)))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid list as a list
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE_ARRAY) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE_LIST)
   will produce UB when attempting to parse the invalid list as a list
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E3,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'

The SEQUENCE_MULTI_E3 is a valid array followed by an invalid list, but valid tuple.

When we access element 0 of our sequence, which is both a valid array and a valid tuple, we will never get UB. We will get the return type of the element based on which return type modifier we use.

When we access element 1 of our sequence, and use a return type modifier that parses its data type as a list we will get UB. But if we use the return type modifiers BOOST_VMD_RETURN_TYPE_ARRAY or BOOST_VMD_RETURN_TYPE_TUPLE we are never parsing the invalid list as a list but as a tuple instead and therefore we successfully return the type of the invalid list as a BOOST_VMD_TYPE_TUPLE.

#include <boost/vmd/elem.hpp>

BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid array
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE_ARRAY)
   will produce UB when attempting to parse the invalid array
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE_LIST) will return '(BOOST_VMD_TYPE_TUPLE,(2,(3,4)))'
BOOST_VMD_ELEM(0,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(2,(3,4)))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE)
   will produce UB when attempting to parse the invalid list
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE_ARRAY) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE_LIST)
   will produce UB when attempting to parse the invalid list
BOOST_VMD_ELEM(1,SEQUENCE_MULTI_E4,BOOST_VMD_RETURN_TYPE_TUPLE) will return '(BOOST_VMD_TYPE_TUPLE,(anydata,BOOST_PP_NIL))'

The SEQUENCE_MULTI_E4 is an invalid array, but valid tuple, followed by an invalid list, but valid tuple.

When we access element 0 of our sequence, which is an invalid array but a valid tuple, we must use a return type modifier which does not parse element 0 as an array, else we will get UB. This means we must use the return type modifiers BOOST_VMD_RETURN_TYPE_LIST or BOOST_VMD_RETURN_TYPE_TUPLE so we are never parsing the invalid array as an array but as a tuple instead and therefore we successfully return the type of the invalid array as a BOOST_VMD_TYPE_TUPLE.

When we access element 1 of our sequence, which is an invalid list but a valid tuple, we must use a return type modifier which does not parse element 1 as a list, else we will get UB. This means we must use the return type modifiers BOOST_VMD_RETURN_TYPE_ARRAY or BOOST_VMD_RETURN_TYPE_TUPLE so we are never parsing the invalid list as a list but as a tuple instead and therefore we successfully return the type of the invalid list as a BOOST_VMD_TYPE_TUPLE.

Usage with other modifiers of BOOST_VMD_ELEM

We have not yet discussed the rest of the modifiers which may be used with BOOST_VMD_ELEM, but return type modifiers are completely independent of any of them. This means they can be combined with other modifiers and whenever the element of the sequence is returned the return type modifiers determine of what the value of that element consists; whether it be just the element data or the element as a type/data tuple with the type parsed according to our return type modifier. When we subsequently discuss the use of other modifiers with BOOST_VMD_ELEM and refer to the element being returned, we are referring to that element as it is determined by the return type modifiers, which by default only returns the element's data.


PrevUpHomeNext