우리가 템플릿을 사용할 때 어떤 타입에 대한 제한을 두고 싶을 때 아래와 같이 static_assert를 이용했었다.
template<class T>
engine_ptr<T> NewComponent(const std::wstring& newName, const engine_ptr<const T> newOther)
{
LOG_ENTRY;
static_assert(std::is_base_of<Component, T>::value, "NewComponent must base of Component");
engine_ptr<T> component = Entity::NewComponent<T>(newName, this, newOther);
components_.emplace_back(component);
return component;
}
static_assert()를 이용할 때에는 함수 구현부에 해당 내용이 들어가야한다.
C++ 20에서는 concepts(https://en.cppreference.com/w/cpp/language/constraints)를 이용할 수 있다.
아래와 같이 requires 키워드를 이용하여 해당 타입일 때에만 template가 될 수 있게 할 수 있다.
// 1) Requires Clause(절)
template<typename T>
requires std::integral<T>
void TestConcept1(T number)
{
cout << number << endl;
}
// 2) Trailing Requires Clause (뒤에 붙는~)
template<typename T>
void TestConcept2(T number) requires std::integral<T>
{
cout << number << endl;
}
// 3) Constrained Template Parameter (강요된)
template<std::integral T>
void TestConcept3(T number)
{
cout << number << endl;
}
// 4) Abbreviated Function Template
void TestConcept4(std::integral auto number)
{
cout << number << endl;
}
이것을 응용하면 아래와 같이 커스터마이징을 할 수 있다.
class GameObject
{
};
class Knight : public GameObject
{
};
template<typename T>
requires std::derived_from<T, GameObject>
void TestObj(T* obj)
{
}
template<typename T>
concept MyConcept = !std::is_integral_v<T> && std::derived_from<T, GameObject>;
template<typename T>
concept Addable = requires (T a, T b)
{
a + b;
}; // a + b가 가능해야 함
template<typename T>
concept Equality = requires(T a, T b)
{
{ a == b } -> std::convertible_to<bool>;
{ a != b } -> std::convertible_to<bool>;
};
template<typename T>
concept Integral = std::is_integral_v<T>;
template<typename T>
concept SignedInt = Integral<T> && std::is_signed_v<T>;
ref - https://openmynotepad.tistory.com/69
C++20) Concepts ( 콘셉트, 개념 ) - 1
Concepts : Generic Programming의 미래 모든 내용은 Bjarne Stroustrup 교수님의 Good_Concepts에서 발췌하였습니다. Concepts 의 사용은 기존의 Unconstrained Template ( 제한되지 않은, 제약 없는 ) 에 비해 Runtime 비용이
openmynotepad.tistory.com
https://openmynotepad.tistory.com/66
C++20) Coroutine ( 코루틴 ) - 2
Coroutine 상태가 할당되면, 해당 할당이 힙에서 발생할 수도 있습니다. ( 힙에 발생해야한다고 생각해야하지만, 컴파일러가 최적화 할 수도 있습니다. ) 만약 힙에서 발생하고, 우리가 만든 promise_t
openmynotepad.tistory.com
https://openmynotepad.tistory.com/71
C++20) Concepts ( 콘셉트, 개념 ) - 3
Concepts: Generic Programming의 미래 모든 내용은 Bjarne Stroustrup 교수님의 Good_Concepts에서 발췌하였습니다. 5. Concepts로 디자인 하기 좋은 Concept는 무엇인가? 이상적으로 Concept는 어떤 영역에서 근본적인 '
openmynotepad.tistory.com
https://openmynotepad.tistory.com/72
C++20) Concepts ( 콘셉트, 개념 ) - 4
Concepts: Generic Programming의 미래 모든 내용은 Bjarne Stroustrup 교수님의 Good_Concepts에서 발췌하였습니다. 6. 개념 오버로딩 ( overloading ) generic programming은 다른 타입에 대한 동일한 작업에 동일한 이름을
openmynotepad.tistory.com
'Basic Programming > C++ 20' 카테고리의 다른 글
C++20 - 지정된 초기화 (Designated Initialization) (1) | 2023.12.17 |
---|---|
C++20 - 삼항 비교 연산자(Three-Way Comparison) (0) | 2023.12.17 |
C++20 - Coroutine (1) | 2023.12.17 |
C++20 - Ranges (0) | 2023.12.10 |
C++20 - Module (0) | 2023.12.10 |