Monday, December 4, 2017

C++17: std::make_from_tuple()

C++17 added the template function std::make_from_tuple() that allows you to construct an object from a tuple. Here is an example:

#include <iostream>
#include <tuple>

struct MyClass
{
  MyClass(int a, int b, int c)
  {
    std::cout << a << " " << b << " " << c << std::endl;
  }
};

int main()
{ 
  MyClass myObject = std::make_from_tuple<MyClass>(
                                       std::make_tuple(1, 2, 3));
  
  return 0;
}
// Output: 1 2 3
References:
http://en.cppreference.com/w/cpp/utility/make_from_tuple
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0209r2.pdf

No comments:

Post a Comment