개발/C·C++
-
[Rvalue reference] Rvalue Reference(3)개발/C·C++ 2021. 5. 21. 17:53
>> 번역글입니다 >> 의역은 있고 오역이 있을 수 있습니다 >> 출처 http://thbecker.net/articles/rvalue_references/section_03.html X가 어떤 타입이라고 할 때 X&&는 X에 대한 rvalue reference라고 합니다. X&는 lvalue reference입니다. rvalue reference는 lvalue reference인 X&와 비슷하게 동작합니다. 여러 예외 상황과 함께요. 함수 오버로딩 문제에서 가장 중요한 점은 rvalue는 새로운 rvalue reference를 선택하고 lvalue는 lvalue reference를 선택한다는 것입니다. void foo(X& x); // lvalue reference overload void foo(X&..
-
[Rvalue reference] Move Semantics(2)개발/C·C++ 2021. 5. 12. 11:31
>> 번역글입니다 >> 의역은 있고 오역이 있을 수 있습니다 >> 출처 thbecker.net/articles/rvalue_references/section_02.html X가 어떤 리소스에 대한 포인터나 핸들을 갖고 있는 클래스라고 가정하겠습니다. m_pResource라고 합시다. 리소스로 인해서, 객체를 생성하고 복사하고 파괴하는 데에 적지 않은 노력이 듭니다. 동적 할당된 배열에 객체들을 갖고 있는 std::vector가 좋은 예가 될 것 같습니다. 클래스 X의 복사 할당 연산자는 다음과 같을 것입니다. X& X::operator=(X const & rhs) { // [...] // Make a clone of what rhs.m_pResource refers to. // Destruct the r..
-
[Rvalue reference] Introduction(1)개발/C·C++ 2021. 5. 12. 10:29
>> 번역글입니다 >> 의역은 있고 오역이 있을 수 있습니다 >> 출처 thbecker.net/articles/rvalue_references/section_01.html rvalue reference는 C++11 표준에 추가된 특징입니다. rvalue reference를 파악하기 조금 어렵게 만드는 것은, 처음 봤을 때 목적이 무엇이고 어떤 문제를 해결할 수 있는지 명확하게 알 수 없다는 점입니다. 일단 rvalue reference가 무엇인지 설명하겠습니다. 해결할 수 있는 문제부터 시작해, rvalue reference가 이를 어떻게 해결할 수 있는지 보여드리겠습니다. 이 방법이 rvalue reference의 정의가 타당하고 자연스럽게 보이도록 해줄 겁니다. rvalue reference는 최소 ..
-
번역 예정 아티클 / 정리할 개념개발/C·C++ 2021. 5. 7. 19:46
>> Perfect forwarding thbecker.net/articles/rvalue_references/section_08.html >> ADL(Argument-dependant name lookup) en.wikipedia.org/wiki/Argument-dependent_name_lookup >> Single-responsibility principle en.wikipedia.org/wiki/Single-responsibility_principle >> Copy and swaop idiom stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom >> Optimize out deleting null pointers stackove..
-
[template] 템플릿 특수화개발/C·C++ 2021. 5. 1. 13:12
템플릿은 기본적으로 일반화 프로그래밍(generic programming)을 위해 사용하지만 경우에 따라서는 다른 동작을 해야 한다거나 타입에 따라 효율성을 위해 구현을 달리 해야 하는 상황이 일어날 수 있습니다. STL 중에는 vector가 bool 타입에 대해서만 특수화가 되어 있죠. 템플릿 특수화는 함수에도 사용할 수 있고 클래스에도 사용할 수 있습니다. 먼저 함수를 살펴보겠습니다. 특수화에는 완전 특수화와 부분 특수화가 있는데, 완전 특수화는 타입을 구체적으로 명시하는 것을 말합니다. 특수화를 할 때는 일반적으로 만들어놓은 템플릿의 형식을 벗어나면 안 됩니다. 다르게 하면 그냥 다른 함수가 되는 것입니다. #include using std::cout; using std::endl; class Cl..
-
[template] 함수 템플릿(function template)개발/C·C++ 2021. 4. 30. 20:29
클래스 자체는 객체가 아니듯이 함수 템플릿은 함수가 아닙니다. 함수를 만들어내는 형판 같은 것입니다. template이라는 키워드는 일반화 프로그래밍을 하게 해줍니다. 영어로는 generic programming이라고 합니다. 객체 지향을 명확하게 몇 가지 문장으로 정의하기 어렵듯이 일반화 프로그래밍도 비슷합니다. 좁은 범위에서는, 특정 타입에 얽매이고 다양한 타입에 대응할 수 있는 프로그램을 작성하는 것, 정도로 생각할 수 있습니다. 다음 예제에서 함수 템플릿을 확인해보겠습니다. template void swap(T& x, T& y) { T temp = x; x = y; y = temp; } int main() { int x = 20, y = 30; //swap(x, y) // explicitly(in..
-
[template] 왜 typename parameter에 0을 주나요?개발/C·C++ 2021. 4. 30. 16:38
※ 번역글입니다 ※ 출처> stackoverflow.com/questions/38981588/why-give-a-typename-template-parameter-a-default-value-of-0-in-c ※ 의역이 있고 오역이 있을 수 있습니다 template 이 코드는 이름 있는 템플릿 타입 매개변수로 T를 선언하고, 이름 없는 템플릿 값 매개변수를 선언한 것입니다. lfEnableIf::Type 타입은 가능한 조건을 만나면 lfInt로 바뀝니다. template 그러나 두 번째 템플릿 매개변수는 lfEnablelf 템플릿의 nested type이기 때문에, typename 키워드를 이용해 컴파일러에게 Type이 실제로 타입이라는 것을 명시해줘야 합니다. 템플릿의 두 번째 파라티머는 이름이 없지만..
-
[예외 처리] 너라고 예외는 아니야개발/C·C++ 2021. 4. 28. 02:54
완벽한 프로그램은 없다고 생각합니다. 인간이 불완전하기 때문에 인간이 만드는 결과물은 무엇이 됐든 불완전할 수밖에 없습니다. 예외 처리는 불완전한 프로그램이 그 속성을 유지하면서 지속적으로 기능할 수 있게 도와줍니다. 사람들이 사용하고 있는 서비스가 시도 때도 없이 종료가 되어 다시 시작해야 한다면 많이 불편할 겁니다. 예외 처리에 try-catch 구문을 사용합니다. 예외가 발생했다고 알리는 키워드는 throw입니다. throw는 단어 그대로 던진다는 의미를 가집니다. re-throw가 아닌 이상 예외를 알릴 무언가를 던지면 됩니다. 문자열이어도 되고 숫자여도 되며 특정 클래스도 가능하고, std::exception 클래스를 상속한 클래스여도 됩니다. 무엇은 던지든 catch만 잘하면 됩니다. try ..