C++/Effective Modern C++

C++/Effective Modern C++

[Effective Modern C++] Chapter 4: Smart Pointers

raw pointer의 단점 1. 포인터의 선언에서 하나의 객체를 가리키는지 배열을 가리키는지 나타내지 않는다. 2. 포인터가 가리키는 객체에 대한 사용이 끝났을 때, 포인터가 가리키는 것을 파괴해야 하는지 여부, 즉 포인터가 가리키는 것을 포인터가 소유하고 있는지 여부에 대해 아무것도 나타내지 않는다. 3. 포인터가 가리키는 것을 파괴해야 한다고 결정했다면, 파괴할 방법을 알 방법이 없다. 4. delete가 객체를 파괴할 올바른 방법이라는 것을 알 때, delete를 사용해야 할지 delete[]를 사용해야 할지 알 수 없다. 잘못된 방법을 사용하면 정의되지 않은 결과가 발생한다. 5. 코드의 모든 경로를 따라 정확히 한 번만 파괴를 수행하도록 보장하기 어렵다. 경로를 잃으면 메모리 누수가 발생하며,..

C++/Effective Modern C++

[Effective Modern C++] Chapter 3: Moving to Modern C++(1)

Item 7: Distinguish between () and {} when casting objects C++11에서 객체 초기화는 혼란스럽다. 일반적인 규칙은, 초기화 값을 parentheses(소괄호), equal sign, 또는 braces(중괄호)로 지정하는 것이다. equal sign과 braces를 함께 사용하는 것도 가능하다. C++는 일반적으로 euqals-sign-plus-braces 문법을 braces-only와 같은 방식으로 처리한다. int x(0);// initializer is in parentheses int y = 0;// initializer follows "=" int z{0};// initializer is in braces int w = {0};// initializ..

C++/Effective Modern C++

[Effective Modern C++] Chapter 2: Auto

Item 5: Prefer auto to explicit type declarations auto를 사용하면 어떤 장점이 있는가? Auto variables must be initialized : Avoiding uninitialized variable problems auto variables는 initializer로부터 타입을 추론하기 때문에, 항상 초기화되어야 한다. 따라서, 초기화되지 않은 변수 문제로부터 벗어날 수 있다. Represent types knwon only to compilers auto는 type deduction을 사용하기 때문에, 컴파일러에만 있는 타입을 지정할 수 있다. Auto vs std::function std::function은 C++11 표준 라이브러리의 템플릿으로,..

C++/Effective Modern C++

[Effective Modern C++] Chapter 1: Deducing Types

Item 1 : Understand template type deduction Type deduction for templates는 modern C++의 auto를 기반으로 한다. template void f(ParamType param); f(expr); 위 코드는, 컴파일 시간 동안 T와 ParamType이라는 두가지 타입을 추론하기 위해 expr을 사용한다. 보통 ParamType은 종종 const, reference와 같은 adornment를 포함하기 때문에 두 타입은 자주 다르다. 예를들어, template이 다음과 같이 선언된 경우, template void f(const T& param); int x = 0; f(x); T는 int로 추론되지만, ParamType은 const int&로 추론..

Tuesberry
'C++/Effective Modern C++' 카테고리의 글 목록