modern C++ 스마트포인터 unique_ptr Smart Pointer의 unique_ptr 사용하기 포인터 대입 복사 안됨. 소유권 이전시 move() 사용 unique_ptr로 할당한 메모리는 reset()으로 초기화, delete로 초기화시 에러 #include #include using namespace std; class Point { int x, y; public: Point(int _x, int _y) : x(_x), y(_y) { cout 개발 2019.12.26
modern C++ begin/end begin(), end() 함수 사용 #include #include #include #include using namespace std; template void show (T& c) { auto p1 = begin(c); auto p2 = end(c); while (p1 != p2) { cout 개발 2019.12.15
modern C++ noexcept c++17 #include using namespace std; int func1() noexcept // { throw 1; // error, call terminate() } int func2() noexcept(false) { throw 1; } int main() { try { // func1(); // terminate() func2(); } catch(...) { cout 개발 2019.12.15
modern C++ using 사용법 #include using namespace std; // 1. type alias using DWORD = int ; using FP = void(*)(int); // 2. template alias template class Point { T x, y; }; template using PT = Point; // 3. 템플릿 인자 고정 template using Duo = pair; template using I_Duo = pair; // 4. 복잡한 문법 단순화 template using remove_pointer_t = typename remove_pointer::type ; int main (void) { DWORD n ; // int n FP p ; // void(*p)(int) Point p1.. 개발 2019.12.15
modern C++ static_assert c++ 11에서 추가 컴파일 타임에서 주어진 식이 false면 메시지 출력 & 컴파일 종료 변수는 사용 못함(중간에 값 변경 가능성이 있음) #include using namespace std; int main (void) { static_assert( sizeof(void*) == 4 , "32bit"); static_assert( sizeof(void*) == 8 , "64bit"); } 빌드 환경이 32bit인지 64bit인지 확인 가능 개발 2019.12.15
modern C++ 11/14 iteral & type, auto & decltype #include using namespace std; int main (void) { // literal & type int n1 = 0b1001; // 2진수로 표시 int n2 = 010; // 8진수 int n3 = 10 ; // 10진수 int n4 = 0x10; // 16진수 int number = 1'001'000; // 컴파일에서 '문자가 무시된다. 단위 셀때 편함. long long n3; // 64bit 정수 변수 // auto / decltype double x[3] = {0, 1, 2}; // auto n4 = x[0]; // 우변의 수식으로 좌변 타입이 결정됨 decltype(n1) d2; // int d2와 같음 } 개발 2019.12.15
Moden C++ 추가 type #include using namespace std; int main() { int n1 = 10; // 10진수 int n2 = 010; // 8진수 int n3 = 0x10; // 16진수 int n4 = 0b10 ;// 2진수 C++11 추가 int n5 = 1'000'000; // 숫자 자리 표시 가능, 컴파일러가 무시, 가독성 증가 C++11 long long n6 ; // 64bit C++11 int x[3] = {1, 2, 3}; auto n7 = x[0]; // 우변의 수식으로 좌변 타입 결정 C++11 decltype(n7) d2; // int d2 괄호속 type으로 선언 C++11 // decltype((n7)) d3 ; // int& d3 -> error: ‘.. 개발 2019.11.09
C++ 공부 준비 참고 사이트 [https://www.isocpp.org] [https://www.cppreference.com] [https://github.com/codenuri/CPPINTERMEDIATE] Test [https://ide.goorm.io/] 공부할 내용 member function call this pointer member function pointer member object user defined literal decltype(auto) 활용 R value reference variable argument invoke() 사용 개발 2019.11.09
임시 객체 활용 #include #include using namespace std; // template T* memAlloc(int sz, T) // { // return (T*)malloc(sz); // } class memAlloc { int size; public: inline memAlloc(int sz) : size(sz) {} templateoperator T*() { return (T*)malloc(size); } }; int main(void) { double* p = memAlloc(40); // class name() : 임시객체... // 임시객체.operator double*() int* p2 = memAlloc(40); cout n ; // 'a' if (cin.fail()) c.. 개발 2019.11.09