검색결과 리스트
C++14에 해당되는 글 3건
- 2015.04.20 [C++14] integer_sequence와 make_integer_sequence
- 2014.07.22 [C++14] Transparent operator functors
- 2013.04.04 C++14의 새로운 기능 (3)
C++14에서 추가된 STL 유틸리티이다.
integer_sequence
임의의 정수형 시퀸스를 컴파일 타임 때 만든다
namespace std {
template <class T, T... I>
struct integer_sequence {
typedef T value_type;
static constexpr size_t size() noexcept { return sizeof...(I); }
};
}
< 예제 >
#include <iostream>
#include <utility>
void g(int a, int b, int c)
{
std::cout << a << ", " << b << ", " << c << std::endl;
}
template <class T, T... Seq>
void f(std::integer_sequence<T, Seq...>)
{
g(Seq...);
}
int main()
{
f(std::integer_sequence<int, 0, 1, 2>());
}
// 결과
0, 1, 2
make_integer_sequence
지정한 수만큼 0에서 순차적으로 증가한 요소를 만든다.
namespace std {
template <class T, T N>
using make_integer_sequence = integer_sequence<T, 0, 1, …, N - 1>;
}
T: 시퀸스 요소의 정수 형
N: 요소 수
<예제 >
#include <iostream>
#include <utility>
void g(int a, int b, int c)
{
std::cout << a << ", " << b << ", " << c << std::endl;
}
template <class T, T... Seq>
void f(std::integer_sequence<T, Seq...>)
{
g(Seq...);
}
int main()
{
// integer_sequence<int, 0, 1, 2>를 만든다
f(std::make_integer_sequence<int, 3>());
}
// 결과
0, 1, 2
출처: http://cpprefjp.github.io/reference/utility/integer_sequence.html
http://cpprefjp.github.io/reference/utility/make_integer_sequence.html
모던 C++ : C++14의 핵심 기능을 중심으로
http://www.hanbit.co.kr/ebook/look.html?isbn=9788968487460
최흥배 지음
국내서 2015년 03월 31일페이지 : 69쪽
ISBN : 9788968487460 난이도 : 초/중급 변환코드 : 2746C++14에서 추가되는 기능
비교, 연산용 클래스 템플릿인 less, greater, plus, multiplies 등을 템플릿 실인수 없이 사용할 수 있다.
[c++11]
vector<unsigned> v { 10, 50, 20, 30 };
sort(v.begin(),v.end(),
greater<unsigned>());
vector<int> v { -10, 50, 20, 30 };
sort(v.begin(),v.end(), greater<unsigned>());
위의 경우 벡터 v가 int로 바뀌었는데 greater 템플릿은 실인수로 unsigned로 되어 있어서 컴파일 에러가 발생한다.
그러나 아래의 C++14에서는 템플릿에 실인수를 넣지 않아서 컴파일러가 알아서 처리하도록 한다.
[C++14]
vector<unsigned> v { 10, 50, 20, 30 };
sort(v.begin(), v.end(), greater<>());
Meeting C++에 차기 C++에 들어갈 기능이나 사양 변경을 소개하는 기사 「A look at C++14: Papers Part I」가 게재.
소개된 기능이나 변경을 간단하게 정리하면 다음과 같다.
Polymorphic Systems allocater의 도입.
초기화 처리 확장.
boost::optional 등의 형을 std::optional 등의 STL에 넣기.
OpenMP이 실현하고 있는 기능을 STL에 넣기.
UDL(User Defined Literals)를 STL에 넣기.
다이나믹 배열을 STL에 넣기.
스레드 세이프한 병렬성을 가진 행렬을 STL에 넣기.
C++에 쉘의 파이프라인과 같은 기능을 도입.
스트림에 대한 mutex 도입.
글로벌에 대한 delete 오퍼레이터의 구현
상세 지정을 할 수 있는 메모리 얼로케이션의 도입.
Const 지정으로 넘긴 참조 또는 값에 관련된 문제 해결.
임의 정도의 정수를 취급하는 기능을 STL에 넣기.
std::priority_queue/std::stack/std::queue를 컨테이너 클래스로 바꾸기.
integral_constant 개선.
TransformationTraits로 다루기 쉬운 앨리어스(alias)를 도입.
random에 대해 몇 가지 제안.
특수한 수치 연산 함수 도입.
댓글