C++
-
c++ 클래스 상속 관계에서의 메모리 정렬프로그래밍/c++ 2018. 9. 16. 20:27
https://stackoverflow.com/questions/2006504/c-data-alignment-member-order-inheritance 0. 상속 관계일 때 베이스 클래스의 멤버 변수의 메모리 위치class base { int i; }; class derived : public base // same for private inheritance { int k; };일 때int i; // baseint k; // derived 순서로 정렬 됨 1. 거의 모든 컴파일러는 클래스의 나머지 멤버 앞에 가상 테이블 포인터를 배치합니다. 즉, vftable 포인터와 클래스 멤버 사이에 약간의 패딩이있을 수 있습니다.
-
enable_shared_from_this를 사용했는데 예외가 발생한 경우프로그래밍/의문 2018. 9. 13. 00:22
class session : std::enable_shared_from_this{public: session(tcp::socket socket) : socket(std::move(socket)){} //...}; void do_accept() { acceptor.async_accept(socket, [&](auto error) { if (error) { return; } std::shared_ptr newSession = std::make_shared(std::move(socket)); newSession->run(); do_accept(); }); } void do_receive(){ auto self = shared_from_this(); socket.async_receive(asio::buffer(..
-
c++17 if statement with initializer프로그래밍/c++ 2018. 8. 22. 12:58
if(init; statement){ // ...} 이런식으로 쓸 수 있다.using namespace std; map m = { {1, "a"}, {2, "b"}, {3, "c"}}; if(auto i = m.find(1); i == m.end()){ // not found} for문에 초기화 구문이 있는 것처럼, if문에도 초기화 구문을 지원하는 것이다. 반환값이 iterator인 것이 많은데 이 부분에서 좀 더 깔끔한 코드 작성이 가능해질 것이라 생각한다.
-
Visual Studio 2017에서 google test를 사용하기프로그래밍/c++ 2018. 8. 20. 11:52
vs2017 15.5 버전에서 google test가 visual studio ide에 통합되었다. 덕분에 google test를 손쉽게 사용할 수 있다. 0. 새 프로젝트 추가 -> Visual C++ 탭에서 테스트 -> Google Test로 프로젝트를 생성한다. visual studio installer를 실행하고, c++를 통한 데스크탑 개발 (desktop development with c++)에서 test adapter for google test를 내려받는다. 1. 테스트할 프로젝트를 선택한다. 2. 테스트 할 모듈을 정의를 include하고, 빌드 Google Test가 기본적으로 제공하는 콘솔 창도 있지만, 비주얼 스튜디오의 테스트 탐색기로도 테스트 결과를 확인할 수 있다. 콘솔로 테스트..
-
stl 컨테이너 환형 순회프로그래밍/c++ 2018. 5. 23. 08:41
UI 프로그래밍을 담당하시는 하시는 분이 Form 안의 UI들을 환형으로 순회하는 걸 간단하게 표현하고 싶은데 어떻게 하면 심플하게 할 수 있을지 여쭤보셨다. 사용처는 컨테이너의 맨 끝 요소에 있는 상태에서 우측 방향키를 누르면 다시 처음 위치의 UI에 위치하도록 하는 게 목적이다.기존에 이미 작성된 코드가 있었기에 해당 부분을 별다른 변경 없이 사용할 수 있도록 값으로 회전할 수 있어야 했고,난 다음과 같은 코드를 만들어줬다. iterator로template inline typename Container::iterator GetNextIteratorCircular(const Container& container, typename Container::iterator..
-
c++ 문자열 스플릿프로그래밍/코드 조각 2018. 5. 10. 19:59
#include #include #include std::vector SplitString(const std::string& source, const std::string& spliters){ const std::regex r{ spliters }; std::sregex_token_iterator begin{ source.begin(), source.end(), r, -1 }; std::sregex_token_iterator end{}; std::vector tokens; tokens.reserve(std::distance(begin, end)); for (auto i = begin; i != end; ++i) { tokens.push_back(*i); } return tokens;} 샘플 코드int m..