프로그래밍/c++
-
std::copy(memcpy)와 std::move(memmove)의 차이프로그래밍/c++ 2015. 2. 20. 23:05
memcopy는 서로 같은 메모리에 복사를 하는것이 아니라는걸 가정하에 하는 반면memmove는 서로 같은 메모리 영역에 복사를 할 수도 있는 상황을 가정한다. memmove위 그림처럼 같은 메모리에서 내 이동이 가능하므로 move라는 네이밍이 붙는다. 코드로 차이점을 풀어보자면... char buffer0[100];char buffer1[100]; std::memcpy(buffer1, buffer0, sizeof(buffer0)); // OKstd::memcpy(buffer0, buffer0, sizeof(buffer0)); // NO! std::memmove(buffer1, buffer0, sizeof(buffer0)); // OKstd::memmove(buffer0, buffer0, sizeof(b..
-
C++ 오브젝트 생성시 {}와 () =의 구분프로그래밍/c++ 2015. 2. 2. 00:07
C++11에서의 초기화 방법들은 다음과 같은것이 있다. int a = 0;int a(0);int a{0}; 여기에서 int a = { 0 } 이와 같은 문법은 {}만 같이 쓴거처럼 처리된다. 즉, int a = {0} == int a{0} {}는 C++11부터의 초기화 문법으로, {}를 사용하는 초기화를 유니폼 초기화(uniform initialization)라고 한다. * 모든 non static value의 초기화는 {}를 이용해서 가능하다 class widget{ int a(0); // error int b{0}; // ok int c = 0; // ok }; * 복사가 안 되는 오브젝트에 대해서는 ()는 되지만 =는 안 된다. std::atomic a{0}; // okstd::atomic b(0)..
-
C++11 가변 인자 템플릿의 사용 예프로그래밍/c++ 2014. 6. 29. 14:05
기존 C++에서는 여러개의 인자를 받는 템플릿 함수를 만들때123456789101112131415161718192021222324 void create() { instance = new Type{}; } template void create(T0 arg0) { instance = new Type{ arg0 }; } template void create(T0 arg0, T1 arg1) { instance = new Type{ arg0, arg1 }; } template void create(T0 arg0, T1 arg1, T2 arg2) { instance = new Type{ arg0, arg1, arg2 }; } .. 이런식으로 만드는 경우가 있었다.저런 코드를 C++11부터 추가된 기능인 가변 인자 ..