Fold expression 

가변인자를 받게 해주는 c++17 기능

https://tech.io/playgrounds/2205/7-features-of-c17-that-will-simplify-your-code/fold-expressions


constexpr

컴파일타임에 상수를 체크하게 하는 예약어, 함수에도 붙을수 있다 

c++11에서 처음 등장해 c++14에서 업그레이드

14에서는 간단한 한줄의 변수를 사용할수 있게 되었다.

http://www.qaupot.com/wordpress/?p=2641



constexpr로 팩토리얼을 만든 예제가 있길래

심심해서 두개를 합쳐봄


https://github.com/vegemil/Effective-C--/blob/master/Effective%20C%2B%2B/Fold%20expression/Fold%20expression/Fold%20expression.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <iostream>
 
constexpr int integer2 = 2;
constexpr int startNumber = integer2 + 1;
 
template <typename... Args>
constexpr int Sigma(Args... args)
{
    int result = (args + ... + 1);
    return result;
}
 
int main(int argc, const char * argv[])
{
    int arrayWithFactorial[Sigma(startNumber)];
 
    printf("%d\n", Sigma(startNumber));
 
    return 0;
}
cs


'C++' 카테고리의 다른 글

c++ 이름 감추기  (0) 2018.03.27
[MAC] VS Code C++ debuging, compile  (0) 2018.03.12
auto decltype  (0) 2018.01.08
c++11 typedef 사용하기  (0) 2018.01.08
enum to string  (0) 2017.08.03

+ Recent posts