c++ boost라이브러리의 variant의 원리에 대해 궁금합니다.
조회수 1482회
안녕하세요. c++을 공부하고 있는 학생입니다.
제가 하는 프로젝트에서 boost에 있는 variant와 유사한 기능을 가진 클래스가 필요하게 되었습니다.
하지만 boost를 추가하기에는 비용이 너무 커서 허접하지만 직접 구현하려고 했습니다.
그래서 boost variant.hpp를 열어서 보았지만 도저히 이해가 안되서 이렇게 질문글 올립니다.
variant의 원리가 궁금하고 variant를 구현하는데 있어서 가장 중요한 c++문법이 뭔지 궁금합니다!
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
C++11 이상을 사용하실 수 있으시면 tuple 을 사용하시면 됩니다.
http://www.cplusplus.com/reference/tuple/tuple/
// http://www.cplusplus.com/reference/tuple/tuple/ // tuple example #include <iostream> // std::cout #include <tuple> // std::tuple, std::get, std::tie, std::ignore int main () { std::tuple<int,char> foo (10,'x'); auto bar = std::make_tuple ("test", 3.1, 14, 'y'); std::get<2>(bar) = 100; // access element int myint; char mychar; std::tie (myint, mychar) = foo; // unpack elements std::tie (std::ignore, std::ignore, myint, mychar) = bar; // unpack (with ignore) mychar = std::get<3>(bar); std::get<0>(foo) = std::get<2>(bar); std::get<1>(foo) = mychar; std::cout << "foo contains: "; std::cout << std::get<0>(foo) << ' '; std::cout << std::get<1>(foo) << '\n'; return 0; }
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력