반응형

프로그래밍 언어/C/C++ 31

[C++] 소스코드에서 컴파일러 구분하기?

원본 : https://quuxplusone.github.io/blog/2021/01/13/conversion-operator-lookup/ 친구의 페이스북에 올라온 C++ 컴파일러 구분하기 란 글을 보았다. 역시 난 C++을 모르는듯 ㅎㅎ 여기서 핵심은 U 와 T가 무엇으로 lookup되어야 하는지를 정하는게 표준에 있는데 컴파일러가 제멋대로 구현했다는 것 같다. U, T 모두에 대해서 gcc만 제대로 A scope으로 룩업한다고... code link : https://godbolt.org/z/jo3dc4 struct T1 {}; struct T2 {}; struct U1 {}; struct U2 {}; struct A { using T = T1; using U = U1; operator U1 T1:..

[C++11] std::enable_shared_from_this

std::shared_ptr은 std::unique_ptr과 함께 C++11부터 사용 가능한 smart pointer 이다. std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. (http://en.cppreference.com/w/cpp/memory/shared_ptr)std::unique_ptr과 다른점은 ownership을 공유(shared)한다는 점이다.std::shared_ptr 객체를 만들때 한가지 문제가 되는 부분이 있는데, 아래와 같은 상황이 발생하지 않도록 주의해야 한다는 점이다.Bad* p = new Bad;std::shared_ptr a1(p);std::shared_..

[읽은 글] const correctness

https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/https://herbsutter.com/2013/05/24/gotw-6b-const-correctness-part-2/https://herbsutter.com/2013/05/28/gotw-6b-solution-const-correctness-part-2/ * 번역이 아니라 이해하고 정리한 내용입니다. 따라서 원 글의 의도가 전달되지 않을 수 있으며, 수정 될 수 있습니다. [ ] https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ 1. shared variableShared variable이란 하나 이상의 t..

[C++11] std::function의 성능

std::function의 성능이 생각보다 많이 느리다는 것을 알게 되었다. ryuan@webkit:~$ g++ ./test.cpp --std=c++11 -O3 -DINTERFACE_VERSION ryuan@webkit:~$ ./a.out main:96] 8 elapsed ryuan@webkit:~$ g++ ./test.cpp --std=c++11 -O3 -DINTERFACE_VERSION_NO ryuan@webkit./a.out main:96] 437 elapsed 못쓰겠네 -_-;http://probablydance.com/2013/01/13/a-faster-implementation-of-stdfunction/ [Update]Modern Effective C++ Item 5 에 따르면std::fun..

GCC options

> 성능과 관련된 옵션들 -fno-rtti (runtime type information)rtti는 아래와 같이 두가지 경우에 사용됩니다. - #include typeinfo(this).name()### c++class type_info { public: virtual ~type_info(); bool operator==(const type_info& rhs) const; bool operator!=(const type_info& rhs) const; int before(const type_info& rhs) const; const char* name(__type_info_node* __ptype_info_node = &__type_info_root_node) const; const char* raw_n..

[C++11] Range based for loop

목차로 가기 - 업데이트 * 박진수님이 지적해주신 오타 수정 C++11 에는 새로운 형태의 for loop인 range based for loop를 지원합니다.이미 python같은 언어에서는 지원되던 기능이기도 합니다. python에서는 아래처럼 루프를 사용할 수 있었습니다.### pythonpeoples = ['ryuan', 'yesum', 'w.third'] for p in peoples: print 'people :', p 이것을 c++에서 구현하려면 아래와 같이 해야 할 것입니다. ### c++#include #include using namespace std; int main() { vector s; s.push_back("ryuan"); s.push_back("yesum"); s.push_ba..

[c++11] rvalue reference

목차로 가기 이미 C++ 에서는 Reference(참조자)를 지원하고 있습니다.rvalue Reference를 설명하기 전에 기존의 Reference의 예제를 잠깐 살펴보겠습니다. ### c++#include using namespace std; int main() { int zero = 0; int& a = zero; //OK int& b = 0; // Compile Error return 0; } 아시겠지만, Reference에 0을 할당하는 것은 불가능합니다.예제를 컴파일 해보면 아래와 같은 에러를 볼 수 있습니다. (gcc 기준)g++ rvalue_reference.cpp rvalue_reference.cpp: In function ‘int main()’: rvalue_reference.cpp:8..

[C++11] Variadic template

목차로 가기 먼저 짧은 영어 탓에 variadic이라는 단어부터 찾아봤습니다.하지만, variadic이란 단어는 원래 존재했던 단어는 아닌듯 합니다. (http://en.wiktionary.org/wiki/variadic)어원은 variable 과 -adic 이 결합된 단어라고 하며, `임의의 많은 변수를 취하는` 이란 뜻입니다. C언어를 접해서 쓰고 있는 사람이라면 printf를 생각하면 쉬울 듯 합니다.printf는 대표적인 variadic function입니다.### c++// printf의 선언int printf(const char *format, ...); Variadic Template을 지원한다는 것은 결국 임의의 많은 template parameter를 취하는 템플릿을 지원한다는 것을 의미..

[C++11] unique_ptr

목차로 가기 unique_ptr는 C++에서 제공하는 스마트 포인터입니다.(TODO: 스마트 포인터에 대한 설명 추가) unique_ptr를 사용하면 동적으로 힙 영역에 할당된 인스턴스에 대한 소유권을 얻고 unique_ptr 객체가 사라지면 해당 인스턴스를 해제하게 됩니다.소유권을 갖기 때문에 인스턴스를 소유하는 unique_ptr은 동시에 두개가 되어선 안됩니다.(둘이 된 경우 소멸되는 과정에서 double free 에러가 발생하게 될 것입니다.) unique_ptr를 사용하려면 헤더를 인클루드하면 됩니다. 간단한 unique_ptr의 사용 예제입니다.### c++#include #include using namespace std; void f() { unique_ptr a(new int(3)); c..

반응형