We present programming techniques to illustrate the facilities and principles of C++ generic programming using concepts. Concepts are C++'s way to express constraints on generic code. As an initial example, we provide a simple type system that eliminates narrowing conversions and provides range checking without unnecessary notational or run-time overheads. Concepts are used throughout to provide user-defined extensions to the type system. The aim is to show their utility and the fundamental ideas behind them, rather than to provide a detailed or complete explanation of C++'s language support for generic programming or the extensive support provided by the standard library. Generic programming is an integral part of C++, rather than an isolated sub-language. In particular, key facilities support general programming as well as generic programming (e.g., uniform notation for types, lambdas, variadic templates, and C++26 static reflection). Finally, we give design rationales and origins for key parts of the concept design, including use patterns, the relationship to Object-Oriented Programming, value arguments, notation, concept type-matching, and definition checking.
본 논문은 개념(concepts)을 사용하여 C++ 제네릭 프로그래밍 기능과 원리를 설명하는 프로그래밍 기법을 제시합니다. 개념은 C++에서 제네릭 코드의 제약 조건을 표현하는 방식입니다. 초기 예제로서, 본 논문은 축소 변환을 제거하고 불필요한 기호나 런타임 오버헤드 없이 범위 검사를 제공하는 간단한 타입 시스템을 제공합니다. 개념은 사용자 정의 타입 시스템 확장을 제공하는 데 광범위하게 사용됩니다. 본 논문은 개념의 실용성과 그 기저의 기본 아이디어를 보여주는 것을 목표로 하며, C++ 제네릭 프로그래밍 언어 지원이나 표준 라이브러리 광범위한 지원에 대한 상세하거나 완전한 설명을 제공하는 것은 아닙니다. 제네릭 프로그래밍은 C++의 구성 요소이지, 고립된 부분 언어가 아닙니다. 마지막으로, 본 논문은 사용 패턴, 객체 지향 프로그래밍과의 관계, 값 매개변수, 기호 표현, 개념 타입 매칭 및 정의 검사를 포함한 개념 설계의 핵심 부분에 대한 설계 철학과 기원을 제시합니다.
template<typename T, typename U>
concept Can_narrow_to =
! same_as<T, U> // 다른 타입
&& Num<T> && Num<U> // 모두 수치 타입
&& (
(floating_point<T> && integral<U>) // 소수 부분 손실 가능
|| (numeric_limits<T>::digits > numeric_limits<U>::digits) // 절단 가능
|| (signed_integral<T>!=signed_integral<U> && sizeof(T)==sizeof(U)) // 부호 변경 가능
);
template<Num U, Num T>
constexpr bool will_narrow(U u) {
if constexpr (!Can_narrow_to<T, U>)
return false;
// 축소 가능성이 있을 때만 런타임 검사 수행
T t = u;
return (t != u);
}
template<Num U, Num T>
constexpr bool will_narrow(U u) {
if constexpr (!Can_narrow_to<T, U>)
return false; // 컴파일 시간에 결정되어 런타임 비용 없음
// 필요한 경우에만 런타임 검사 수행
}
논문은 C++ 언어 설계에서 제네릭 프로그래밍 이론에 이르는 다양한 측면을 다루는 풍부한 참고문헌을 포함합니다. 주요 내용은 다음과 같습니다:
STL 및 제네릭 프로그래밍에 대한 Alex Stepanov의 획기적인 업적
C++ 표준 위원회의 기술 보고서 및 제안
저자 본인의 C++ 설계 및 진화에 관한 역사 문서
관련 프로그래밍 언어 이론 연구
요약: 이것은 C++ 언어 창시자가 직접 작성한 중요한 이론적, 실용적 가치를 지닌 논문입니다. 개념의 C++ 제네릭 프로그래밍 응용을 포괄적으로 보여주며, 실용적인 프로그래밍 기법뿐만 아니라 설계 철학도 심층적으로 설명합니다. C++ 프로그래머와 프로그래밍 언어 연구자 모두에게 중요한 참고 가치가 있습니다.