make_from_tuple — std::make_from_tuple
make_from_tuple — std::make_from_tuple
std::make_from_tuple는 튜플의 원소들을 인자로 하여 타입 T를 생성하는 함수예요. C++17에서 도입됐어요. <tuple> 헤더에 있어요.
튜플을 풀어서 T의 생성자를 호출해 객체를 만들어요.
출처: cppreference
본문
// <tuple> 헤더, C++17
template< class T, class Tuple >
constexpr T make_from_tuple( Tuple&& t );
사용 예
#include <tuple>
#include <string>
struct Point { int x, y; Point(int a, int b) : x(a), y(b) {} };
std::tuple<int, int> t(3, 4);
Point p = std::make_from_tuple<Point>(t); // Point(3, 4)
여러 생성자·타입
struct Person {
Person(std::string name, int age) { /* ... */ }
};
auto args = std::make_tuple(std::string("Kim"), 30);
Person kim = std::make_from_tuple<Person>(args);
특징
- 튜플인자를 "풀어서" 그대로 생성자에 전달.
std::apply와 비슷하지만,apply는 함수 호출이고make_from_tuple은 타입 생성이에요.- tuple-like 타입도 지원 (C++23).
// apply와 대비
auto f = std::make_from_tuple<SomeType>(tuple); // 객체 생성
std::apply(someFunction, tuple); // 함수 호출
std::make_from_tuple은 튜플로 묶인 인자로 타입의 객체를 간편하게 생성하는 도구예요. 데이터를 튜플로 다루는 제네릭 코드에 유용해요.