라벨이 Template인 게시물 표시

Implementing a function that accepts only the user defined types in using the template specialization of C++.

A foo() in below source code will be accepted only the two types like myString and String2, but other types will occur an errors on compile time.

CRTP(Curiously recurring template pattern) 소개

CRTP(Curiously recurring template pattern) CRTP는 클래스 X가 X 자신을 템플릿 인자로 사용하여 클래스 템플릿 인스턴스화로부터 파생되게 하는 C++ 표현(Idiom, 관용구 )이다. 이 표현 이름은 1995년에 Jim Coplien에 의해 만들어 졌다. [Wikipedia 정의] The curiously recurring template pattern (CRTP) is an idiom in C++ in which a class X derives from a class template instantiation using X itself as template argument.[1] More generally it is known as F-bound polymorphism, and it is a form of F-bounded quantification. The technique was formalized in the 1980s as "F-bounded quantification."The name "CRTP" was independently coined by Jim Coplien in 1995, who had observed it in some of the earliest C++ template code as well as in code examples that Timothy Budd created in his multiparadigm language Leda. 1. 일반적 형식 // The Curiously Recurring Template Pattern (CRTP) template < class T > class Base { // methods within Base can use template to access members of Derived }; class Derived : public Base < Derived ...

C++ 템플릿 특수화(Template Specialization)을 이용하여 특정 타입만 허용하는 함수 구현

소스 코드에서 foo() 함수는 myString과 ccString2 타입만 허용하도록 하여, 다른 타입들이 들어오면 컴파일 오류를 발생하는 예제