a tour of c++ : the basics

31
A Tour of C++ : The Basics C++ 스터디 2주차

Upload: jaewon-choi

Post on 22-Jan-2018

367 views

Category:

Software


1 download

TRANSCRIPT

Page 1: A tour of C++ : the basics

A Tour of C++: The Basics

C++ 스터디 2주차

Page 2: A tour of C++ : the basics

발표자

• 최재원

• 아주대학교컴공, 수학전공

• (주)펜타큐브인턴

• 관심분야:• System software

• Big data infrastructure

• GPGPU

2016. 6. 7. 2016 C++ study 2

Page 3: A tour of C++ : the basics

Previously…

• C++은시스템소프트웨어제작, 고성능분야에주로쓰인다.

• 다양한프로그래밍스타일을포함한언어.

• C++ best practices: 최대한컴파일할때문제를드러내게하라.

Page 4: A tour of C++ : the basics

Previously…

• 우리 C++ 스터디의 1차목표를정함• A Tours of C++ (TC++PL chapter 2 ~ chapter 5) 읽기

• 다읽으면서, 진도에방해되지않는선에서추가적인발표를한다.• E.g. C compatibility issues

Page 5: A tour of C++ : the basics

NOTICE

• 본슬라이드는 C++ code들을포함하고있습니다.• 모든 codes는 Mac OS X El Capitan ver. 10.11.5에서 g++

(Homebrew gcc 6.1.0) 6.1.0 를통해컴파일되고실행되었음.

• 본슬라이드는 극미량의 assembly code를포함하고있습니다.• 무서우신분들은방의불을활짝켜시고보시길...

Page 6: A tour of C++ : the basics

A Tour of C++: The Basics

Page 7: A tour of C++ : the basics

A Tour of C++: The Basics

• Informal introduction to C++

• 모든것을이해할필요도없고, 이해하는것을기대하지않는다.

• 다만, 재밌어보인것, 궁금한것, 어려운것들을보기위한조감도를그리기위함.

• C++ 11을기준으로진행될예정• 가끔 14도다룰예정.

• 참고) gcc 6.1 부터는 default 가 C++ 14.• 시대에뒤떨어지고있는우리…

Page 8: A tour of C++ : the basics

A Tour of C++: The Basics

• “Procedural programming”

• 어떤자료구조를만들고, 어떤알고리즘을만들지를고민하는프로그래밍스타일

• C를했다는 가정을하므로, 쉽게쉽게언급하고넘어가자.

Page 9: A tour of C++ : the basics

The “Basics”

• C++ is a compiled language• Interpreter vs Compiler

• Portability: 사실상 portable한몇안되는언어중하나

• Statically typed language• Type이란?

• 어떤형식으로데이터를저장할지결정하는것

• 해당자료에가할수있는연산의종류를정의하는것

• Statically typed language에서는컴파일러가변수의사용법을안다.

Page 10: A tour of C++ : the basics

The minimal C++ program

• Main: entry point.

Page 11: A tour of C++ : the basics

The minimal C++ program

• Return “int value” of main:• Non-zero return values indicate failure to system.

Page 12: A tour of C++ : the basics

Hello world program

• Namespace?

• Cout?

• Operator overloading?

Page 13: A tour of C++ : the basics

Types, Variables, and Arithmetics

• Type

• Variable

• Arithmetic

Page 14: A tour of C++ : the basics

Contants

• Const

• Constexpr• Assembly를보자

• Template meta programming에서강력하게작동• Compile time에모든걸계산

• Optimization을걸면 constexpr 이든뭐든똑같이작동함.

• Inlining vs constexpr• Inlining: removes function calls

• Constexpr: evaluates in compile times

• http://stackoverflow.com/questions/7113872/inline-vs-constexpr

Page 15: A tour of C++ : the basics

Tests and Loops

• If, else if, switch

• While

Page 16: A tour of C++ : the basics

Pointers, Arrays, and Loops(for)

• Pointer• Pointer vs Reference

• NULL(or number 0) vs nullptr• Nullptr eliminates potnetial confusion between integers and

pointers

• New, delete operators• Double deletion

• Undefined behavior

• Don’t use “naked’ new and delete operator• Use std::unique_ptr, std::shared_ptr

Page 17: A tour of C++ : the basics

Pointers, Arrays, and Loops(for)

• Arrays

• For• Naïve for

Page 18: A tour of C++ : the basics

Pointers, Arrays, and Loops(for)

• Arrays

• For• Ranged for

*이런식으로하면복사가발생하면서 i의생성자가호출되게된다.

Page 19: A tour of C++ : the basics

Pointers, Arrays, and Loops(for)

• Arrays

• For• Ranged for

Page 20: A tour of C++ : the basics

User-Defined Types

• Struct• C에서다형성을구현한다면?

• Alignment requirements

Page 21: A tour of C++ : the basics

User-Defined Types

• Class• The representation과 the operations 을더긴밀하게이어주기위해사용

• 사실 Class 와 struct는기본제한자(private이냐,public이냐) 외엔차이가없음

• 더자세한내용은다음 tour에서…

Page 22: A tour of C++ : the basics

User-Defined Types

• Class• Constructor

• 클래스의이름과같은함수.생성시에호출되는것이보장된다.

• 참고: std::initializer_list

Page 23: A tour of C++ : the basics

User-Defined Types

• Enumeration• Enum class

• Enum 이름지을때겹치지않게하기위해했던여러짓을안해도됨.

• 현재는아래와같이.

Page 24: A tour of C++ : the basics

User-Defined Types

• Enumeration• Enum class

• “class”: Strongly typed and that its enumerators are scoped.

• Explicit conversion이나enum name을드러내고싶지않다면기존의 plain enum을써도됨.

• 기본적으로 Assignment, initialization, and comparisions( == or <= ) 가정의되어있음

Page 25: A tour of C++ : the basics

Modularity

• “Declarations” vs “Definitions"

• Separate Compilation

• Namespaces• ::printf my_namespace::printf

Page 26: A tour of C++ : the basics

Modularity

• “Declarations” vs “Definitions"

• Separate Compilation

• Namespaces• ::printf my_namespace::printf

Page 27: A tour of C++ : the basics

Error handling

• Exceptions

• Invariants• (Class) invariant: What is assumed to be true

Page 28: A tour of C++ : the basics

Error handling

• Static asserts• Why use asserts

• 개발중 “당연히 ~해야하는것”을정의하고넘어가는작업

• 해당 scope에서그 assert 이후는 assert로걸러낸작업을믿고코딩할수있다.

• 많은문서에서 assert를 쓰는것은자유롭게허락하고있다.

• 어떻게보면 assert는 문서화기법이라고볼수도있다. assertion만보고도이코드의목적을알게할수도있기때문이다.

• 많은경우 Debug에서만 assert가돌게하므로, 디버그때assert로 검증을하고 runtime에선 exception을날리는식으로코딩한다.

• 적극추천!

Page 29: A tour of C++ : the basics

Error handling

• Static asserts• Why static asserts

• 이러한 assert 작업을컴파일타임에하는방법

• 상수범위체크에쓸수있다.

• 상수를현재 scope에다시정의하고, 이전에정의한상수들과관계로정의하는상황이있는데그때제일좋다.

• constexpr를써놓은 function을체크할수있다.

• 사실별쓸모없어보인다

Page 30: A tour of C++ : the basics

Error handling

• Design by contracts• assert를통해설계가확실한지를점검하는코드를넣는기법

• getter, setter 에 validation 하는코드를넣는 practices가바로써먹어볼수있는것.

• 이를단순한 if문에 exceptions 도넣지만 assertion을넣으면버그를잡기쉽다.

• https://en.wikipedia.org/wiki/Design_by_contract

Page 31: A tour of C++ : the basics

References

• TC++PL

• https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

• En.cppreference.com