Boost-pp: how to determine if a macro parameter is a tuple

A tuple is a comma-separated list enclosed in a parens, for example

()
(,)
(thing,)
(2,3)

If i have

#define ISTUPLE(x) \\...

I would like something like resolving ISTUPLE(nope)for 0 and ISTUPLE((yep))for resolving 1.

[FWIW, I _ RTFM_'d a lot.]

+4
source share
1 answer

This can probably be done in the Preprocessing library with little work, but the Variadic macro data library (added to Boost since this question was posted) has a ready-made solution. BOOST_VMD_IS_TUPLEdefined in boost / vmd / is_tuple.hpp should do what you are looking for:

#include <iostream>

#include <boost/vmd/is_tuple.hpp>

#if BOOST_VMD_IS_TUPLE() != 0
  #error BOOST_VMD_IS_TUPLE() != 0
#endif

#if BOOST_VMD_IS_TUPLE(nope) != 0
  #error BOOST_VMD_IS_TUPLE(nope) != 0
#endif

#if BOOST_VMD_IS_TUPLE((yep)) != 1
  #error BOOST_VMD_IS_TUPLE((yep)) != 1
#endif

#if BOOST_VMD_IS_TUPLE(()) != 1
  #error BOOST_VMD_IS_TUPLE(()) != 1
#endif

#if BOOST_VMD_IS_TUPLE((,)) != 1
  #error BOOST_VMD_IS_TUPLE((,)) != 1
#endif

#if BOOST_VMD_IS_TUPLE((thing,)) != 1
  #error BOOST_VMD_IS_TUPLE((thing,)) != 1
#endif

#if BOOST_VMD_IS_TUPLE((2,3)) != 1
  #error BOOST_VMD_IS_TUPLE((2,3)) != 1
#endif

static_assert(BOOST_VMD_IS_TUPLE() == 0,"BOOST_VMD_IS_TUPLE() != 0");
static_assert(BOOST_VMD_IS_TUPLE(nope) == 0,"BOOST_VMD_IS_TUPLE(nope) != 0");
static_assert(BOOST_VMD_IS_TUPLE((yep)) == 1,"BOOST_VMD_IS_TUPLE((yep)) != 1");
static_assert(BOOST_VMD_IS_TUPLE(()) == 1,"BOOST_VMD_IS_TUPLE(()) != 1");
static_assert(BOOST_VMD_IS_TUPLE((,)) == 1,"BOOST_VMD_IS_TUPLE((,)) != 1");
static_assert(BOOST_VMD_IS_TUPLE((thing,)) == 1,"BOOST_VMD_IS_TUPLE((thing,)) != 1");
static_assert(BOOST_VMD_IS_TUPLE((2,3)) == 1,"BOOST_VMD_IS_TUPLE((2,3)) != 1");

int main(void)
{
    using std::cout;
    using std::endl;
    cout << "BOOST_VMD_IS_TUPLE() == " << BOOST_VMD_IS_TUPLE() << endl;
    cout << "BOOST_VMD_IS_TUPLE(nope) == " << BOOST_VMD_IS_TUPLE(nope) << endl;
    cout << "BOOST_VMD_IS_TUPLE((yep)) == " << BOOST_VMD_IS_TUPLE((yep)) << endl;
    cout << "BOOST_VMD_IS_TUPLE(()) == " << BOOST_VMD_IS_TUPLE(()) << endl;
    cout << "BOOST_VMD_IS_TUPLE((,)) == " << BOOST_VMD_IS_TUPLE((,)) << endl;
    cout << "BOOST_VMD_IS_TUPLE((thing,)) == " << BOOST_VMD_IS_TUPLE((thing,)) << endl;
    cout << "BOOST_VMD_IS_TUPLE((2,3)) == " << BOOST_VMD_IS_TUPLE((2,3)) << endl;

    return 0;
}

Conclusion:

BOOST_VMD_IS_TUPLE() == 0
BOOST_VMD_IS_TUPLE(nope) == 0
BOOST_VMD_IS_TUPLE((yep)) == 1
BOOST_VMD_IS_TUPLE(()) == 1
BOOST_VMD_IS_TUPLE((,)) == 1
BOOST_VMD_IS_TUPLE((thing,)) == 1
BOOST_VMD_IS_TUPLE((2,3)) == 1

(http://coliru.stacked-crooked.com/a/6e41eaf17437c5d5)

+1

Source: https://habr.com/ru/post/1542936/


All Articles