1

Let's say that I have an std::tuple:

std::tuple< int, float, bool > my_tuple;

and a function:

void my_function( int& i, float& f, bool& b);

Is it possible to retrieve the my_tuple object from within my_function if I can guarantee that its arguments i, f, b are members of my_tuple?

For example - is it legal to:

void my_function( int& i, float& f, bool& b){
  using tuple_type = std::tupl< int, float, bool >;
  tuple_type& my_tuple = *reinterpret_cast<tuple_type*>(reinterpret_cast<void*>(&i))
  // do stuff with my_tuple, like call its copy constructor, etc
}
Evg
  • 25,259
  • 5
  • 41
  • 83
tsuki
  • 907
  • 4
  • 17
  • No, it's not possible. – Passer By Nov 06 '18 at 07:30
  • 1
    I believe you could if you use a non-standard implementation of `tuple` that's guaranteed to be a [standard-layout type](https://en.cppreference.com/w/cpp/types/is_standard_layout) and guarantees which data member is first. You probably also need a `launder` in there even with those conditions fulfilled. This all begs for some standardese, though. – chris Nov 06 '18 at 07:39

0 Answers0