Variadic template

Kim suho·2021년 12월 30일
0

C++11 이후에는 아래와 같이 variadic template이 지원된다.
https://modoocode.com/290

사용할때는 템플릿 특수화와 parameter pack으로 reflection 기능 비슷하게 사용할 수 있다.

이전에 이를 이용해서 SOC개념의 통신 모듈을 개발한 적이 있다.
이 template class 에 전달받은 parameter pack을 기본으로 하는 tuple 변수를 생성하고, 이 param을 하나씩 call하면서 send/receive하고, 외부에서는 class를 선언하고 내부에 send/receive에 사용할 method만 std::function 형태로 기술하면, caller는 이 tuple변수를 이용해서 data를 serialize해서 다른 process로 던지고, 받는 쪽은 선언된 method를 implementation하고 내부에서 data를 deserialize해서 method의 인자로 넘거주는 기능을 가졌다.

이용자는 lvalue, rvalue다 넘겨줄수 있어야 하므로 이를 구현하려면 perfect forwarding, decay, lvalue-rvalue, std::referece_wrapper등을 잘 이해해야한다.

profile
평범한 개발자

0개의 댓글