Skip to content

Instantly share code, notes, and snippets.

@LusainKim
Created December 2, 2016 12:32
Show Gist options
  • Save LusainKim/e69da8975d0394b195cacc6133c58c20 to your computer and use it in GitHub Desktop.
Save LusainKim/e69da8975d0394b195cacc6133c58c20 to your computer and use it in GitHub Desktop.
Build Array
#include <iostream>
#include <array>
using namespace std;
template<typename Ty, typename... Args>
constexpr auto BuildArray(Args&&... args) noexcept { return array<Ty, sizeof...(args)>{ args... }; }
template <typename Ty, size_t N> using TyArr = Ty[N];
// 들어갈 수 있는지 확인하는 용도
void foo(int* arr) {}
int main()
{
foo(BuildArray<int>(1, 2, 3, 4).data());
foo(TyArr<int, 4>{ 1, 2, 3, 4 });
// error: 균일 초기화로 만든 배열과 포인터는 묵시적 형변환이 되지 않는다.
// foo({ 1,2,3,4 });
for (auto& p : BuildArray<int>(1, 2, 3, 4)) cout << p << " " << endl;
for (auto& p : TyArr<int, 4>{ 1, 2, 3, 4 }) cout << p << " " << endl;
for (auto& p : { 1, 2, 3, 4 }) cout << p << " " << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment