Macro BOOST_IDL_PROTECT

Description
Headers
Reference
Examples

Description

The macro BOOST_IDL_PROTECT is used to prevent commas within a type expression from being interpretted as macro argument separators by the preprocessor.

Headers

<boost/interfaces.hpp>
<boost/interfaces/detail/idl/protect.hpp>

Reference

BOOST_IDL_PROTECT

#define BOOST_IDL_PROTECT(expr) ...
Parameters
expr- A parenthesized expression of the form (type_expr)

The macro BOOST_IDL_PROTECT is used to prevent commas within a type expression from being interpretted as macro argument separators by the preprocessor.

If type_expr denotes a type T, BOOST_IDL_PROTECT((type_expr)) expands to an expression denoting the decayed type of T. The decayed type of T is usually the same as T, but can differ from T if T involves array or function types or has top-level cv-qualification.

Example

Consider the following pseudocode definition:

template<typename Ch, typename Tr, typename Alloc>
interface INamed {
    std::basic_string<Ch, Tr, Alloc> name() const;
};

An initial attempt to render the above using the IDL might be as follows:

template<typename Ch, typename Tr, typename Alloc>
BOOST_IDL_BEGIN3(INamed)
    BOOST_IDL_CONST_FN0(name, std::basic_string<Ch, Tr, Alloc>)
BOOST_IDL_END3(INamed)

Unfortunately, the preprocessor interprets the third line as an invocation of the macro BOOST_IDL_CONST_FN0 with four arguments:

  1. name
  2. std::basic_string<Ch
  3. Tr
  4. Alloc>

This is an error, since BOOST_IDL_CONST_FN0 takes only two arguments.

The solution, using BOOST_IDL_PROTECT, is as follows:

template<typename Ch, typename Tr, typename Alloc>
BOOST_IDL_BEGIN3(INamed)
    BOOST_IDL_CONST_FN0(
        name, 
        BOOST_IDL_PROTECT((std::basic_string<Ch, Tr, Alloc>))
    )
BOOST_IDL_END3(INamed)

(Note the double parentheses around the expression std::basic_string<Ch, Tr, Alloc>.)


Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah