// C++
const int a = 10;
const int& b = a;
const int* c = &a;
// &가 달려야 레퍼런스로 추론이 된다.
// &가 없으면 값으로 추론이 된다.
auto aa = a; // int
auto& bb = b; // const int&
auto cc = c; // const int*
// C++
#include <iostream>
#include <type_traits>
using std::cout;
using std::endl;
void func(float)
{
}
// -> decltype(t + s) : t + s를 계산할 때 나오는 타입을 리턴 타입으로 정함.
template<typename T, typename S>
auto add(T t, S s) -> decltype(t + s)
{
return t + s;
}
enum class Type
{
A, B
};
template<typename Enumeration>
auto asInteger(Enumeration value)
{
// underlying_type<> : enum의 기본 유형을 가져온다.
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}
int main()
{
int nums[] = { 1,2,3 };
auto nums0 = nums;
auto& nums1 = nums;
auto func0 = func; // 함수 포인터.
auto& func1 = func; // 함수의 레퍼런스.
cout << add(1, 1.2) << endl;
cout << asInteger(Type::A) << endl;
}
// C++
#include <iostream>
using std::cout;
using std::endl;
template<auto... Args>
auto sum(int num)
{
return (Args + ... + num); // 3번 방식.
}
int main()
{
int num = 5;
cout << sum<10, 2, 1, 30>(num);
}
Output:
48
// C++
#include <iostream>
using std::cout;
using std::endl;
int main()
{
// tuple packing.
std::tuple<int, float, std::string> t0{ 1,2.1f, "3" };
auto t1 = std::make_tuple(1, 2.1f, "3"); // 튜플 생성.
cout << std::get<0>(t0) << endl; // t0의 0번 요소.
cout << std::get<1>(t1) << endl; // t1의 1번 요소.
// unpacking (C++17 이상).
auto [x0, y1, z2] = t0;
cout << x0 << endl;
cout << y1 << endl;
cout << z2 << endl;
}
Output:
1
2.1
1
2.1
3