서의 공간

optional 예제 본문

C++/정보

optional 예제

홍서의 2021. 2. 3. 01:01
#include <iostream>
#include <optional>
using namespace std;

/*
	optional 예제
	함수의 리턴값이 존재하는지 안하는지에 따라 조건식을 구성할 수 있다.
*/

int branchNum = 0;

optional<int> GetIntOptional()
{
	if (branchNum > 10)
		return branchNum;
	return {};
}

int main()
{
	while (true)
	{
		cout << "branchNum: ";
		cin >> branchNum;
		// if문의 참 조건은 GetIntOptional()의 리턴값이 int일 경우에 참이다.
		// int일 경우만 if문을 실행한다. int가 아니라면 거짓이 됨.
		// while문은 10 이상의 숫자를 대입할 경우에만 종료가 된다.
		if (auto result = GetIntOptional())
		{
			cout << "In if, brandchNum > 10" << endl;
			system("pause");
			return 0;
		}
		cout << "branchNum < 10" << endl;
	}
}

'C++ > 정보' 카테고리의 다른 글

상호참조와 종속성을 최소화하는 방법  (0) 2021.05.17
클래스에 대해  (0) 2021.02.03
c++ 함수안에 함수 정의  (0) 2021.01.19
std::vector::erase(), std::vector::clear(), std::remove()  (0) 2020.12.16
C++용 XML 설명서  (0) 2020.12.13
Comments