1

I have a boost::tuple made of pointers (the number of pointers is not known upfront being template metaprogramming).

For example:

boost::tuple<int*, Foo*, std::string*> mytuple

Is there a way to initialize the pointers to 0 ?

I have tried creating a predicate such as:

struct Nullify
{
    template <class TypePtr>
    void operator()(TypePtr& ptr) const
    {
        ptr = 0
    }
};

boost::fusion::for_each(mytuple, Nullify());

But I get error: no matching function for call to...

Ideally, if possible, I would like to use a boost::lambda within the for_each loop directly with no separate struct. (I'm using c++03)

eg. to nullify

boost::fusion::for_each(mytuple, boost::lambda::_1 = 0);

eg. to delete

boost::fusion::for_each(mytuple, delete boost::lambda::_1);
codeJack
  • 2,423
  • 5
  • 24
  • 31
  • 2
    You could use some [template magic](https://stackoverflow.com/questions/10766112/c11-i-can-go-from-multiple-args-to-tuple-but-can-i-go-from-tuple-to-multiple) to "unpack" the tuple and perform an operation on each element. [See here too](https://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer). – Cory Kramer Nov 18 '16 at 13:53
  • I really wanted to have it done in a "range" fashion : ideally I was looking for a boost::lambda expression (eg. _1 = 0) to include within the for_each loop to have it as concise as possible – codeJack Nov 18 '16 at 13:58
  • 1
    @codeJack The first approach [seems to compile](http://coliru.stacked-crooked.com/a/a343937e7c83fb73). Did you perhaps forget to include `boost/fusion/adapted/boost_tuple.hpp`, so that fusion can work with `boost::tuple`? – Dan Mašek Nov 18 '16 at 17:20
  • You were right I missed that include :) thanks!! Any chances I can get rid of the struct using a lambda or equivalent ? – codeJack Nov 18 '16 at 17:37

1 Answers1

2

It can be done simply by initializing it accordingly:

boost::tuple<int*, Foo*, std::string*> mytuple(nullptr, nullptr, nullptr);

(or use NULL if nullptr isn't available in your C++ version).

See here, subsection "constructing tuples".

davidhigh
  • 14,652
  • 2
  • 44
  • 75
  • Could work for initialising it to NULL, but I would need a solution working to delete each pointer as well. Plus, I don't know the number of tuple arguments upfront (it's part of some template metaprogramming) – codeJack Nov 18 '16 at 14:01
  • Updated question, sry and thanks anyways for the answer – codeJack Nov 18 '16 at 14:04