4

A tuple is a comma-separated list enclosed by parens, e.g.

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

If I have

#define ISTUPLE(x) \\...

I'd like something like ISTUPLE(nope) to resolve to 0 and ISTUPLE((yep)) to resolve to 1.

[FWIW, I have _RTFM_'d a plenty.]

Clay Bridges
  • 10,746
  • 10
  • 62
  • 110

1 Answers1

1

It could likely be done in the Preprocessing library with a little work, but the Variadic Macro Data Library (added to Boost since this question was posted) has a ready-made solution. BOOST_VMD_IS_TUPLE, defined in boost/vmd/is_tuple.hpp, should do what you're 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;
}

Output:

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)

jerry
  • 2,545
  • 1
  • 18
  • 32