/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <array>
#include <iostream>
using namespace std;
// Test that array has linkage
template<auto& arr>
void test() {}
// Get array with linkage
template<size_t... vals>
constexpr auto valsToArr = array<size_t, sizeof...(vals)>{ vals... };
// Lambda to array with linkage
template<size_t... Idx>
constexpr auto& arrWithLinkage(auto lam, index_sequence<Idx...>) {
return valsToArr<lam()[Idx]...>;
}
template<size_t=0> // Only works with this...
constexpr auto& arrWithLinkage1(auto lam) {
constexpr size_t N = lam().size();
return arrWithLinkage(lam, make_index_sequence<N>{});
}
template<typename Lam> // A little less gross-looking, but still concerned that this is UDB
constexpr auto& arrWithLinkage2(Lam lam) {
constexpr size_t N = lam().size();
return arrWithLinkage(lam, make_index_sequence<N>{});
}
int main()
{
auto lam = [](){ return array{1, 2, 3}; };
constexpr const array<size_t, 3>& res = arrWithLinkage1(lam);
test<res>();
return 0;
}