프로그래밍 검색 블로그

C++ optional 본문

연습장

C++ optional

코딩조무사 2017. 10. 15. 11:11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool someThing(std::string& out){
    out = "a";
}
int main(){
    std::string str;
    
 
    if(someThing(str)){
        cout << str << endl;
    }
    else{
        cout << "null" << endl;
    }
}
cs




1
2
3
4
5
6
7
8
9
10
11
std::optional<std::string> someThing(){
    return {"a"};
}
 
int main(){
    if(auto str{someThing()}){
        cout << "get" << str << endl;
    }else{
        cout << "null" << endl;
    }
}
cs


굳이 예제를 들자면 이정도지만 

optional에 대해서는 편해지기 보다는 일부러 좀더 불편하게 되도록 설계한듯 하다 

Comments