C++ - shared_from_this가 가리키는 곳을 변경하지 못하도록 규정

출처 this 포인터를 std::shared_ptr로 얻을 수 있는 기능으로 std::enable_shared_from_this 기본 클래스와 멤버 함수 shared_from_this()가 있다. std::enable_shared_from_this에서 파생된 클래스의 개체를 new 하고 shared_ptr의 생성자에 전달하면 이 개체의 this를 shared_ptr로 취득 할 수 있다. 그러나 아래와 같은 포인터에서 복수의 shared_ptr 개체를 만든...
더 읽기

C++17 - false sharing와 true sharing 제어

출처 병행 프로그래밍으로 문제가 될 수 있는 캐시 무효화 문제를 제어 할 수 있다. false sharing 제어 struct keep_apart { atomic<int> cat; atomic<int> dog; }; 이런 구조가 있으면 cat와 dog가 같은 캐시 라인에 탈 수 있다. 스레드 1은 cat 변수,...
더 읽기

C++17 - 랜덤 샘플링 알고리즘

std::sample()이 <algorithm>에 추가 되었다. #include <iostream> #include <string> #include <iterator> #include <random> #include <algorithm> int main() { const std::string input = "abcdef"; // input에서 3개 요소를 무작위로 추출한다 // 기본 난수 생성기를 사용한다 { std::string result; std::sample(input.begin(), input.end(), std::back_inserter(result), 3);...
더 읽기

C++17 - map과 unordered_map의 try_emplace()와 insert_or_assign()

try_emplace() 멤버 함수: 삽입 실패 시에 주어진 파라미터 args…를 변경하지 않는다. insert_or_assign() 멤버 함수: 삽입에 실패하면 덮어 쓴다. std::map<std::string, std::unique_ptr<Foo>> m; m["foo"] = nullptr; std::unique_ptr<Foo> p(new Foo); auto res = m.try_emplace("foo", std::move(p)); assert(p); // p는 유효 std::map<std::string, int> m; auto...
더 읽기

C++ - 우측 값 참조 value category

value category C++의 식은 타입과는 별도로 value category라는 것을 갖는다. value category는 prvalue, xvalue, lvalue 중 하나이고, prvalue와 xvalue를 모아서 우측 값 이라고 부른다. 주의해야 할 것으로 같은 형을 가진 식에서도 다른 value category를 가질 있다. 같은 객체를 돌려주는 식이라도...
더 읽기

C++ - C 언어 보다 왜 컴파일이 느릴까?

일반적으로 C++로 작성된 코드가 C 보다 컴파일 시간이 느린 이유 그 이유는 주로 아래의 5개이다. C++ 문법의 복잡성, 소스 코드의 parse(어휘 분석, 구문 분석, 의미 분석)에 시간을 요하고 있다 템플릿의 실체화에 시간이 걸린다 복잡한 최적화를 실시 할 필요가 있다 표준...
더 읽기