应用程序图像的实现具有通过由编译器计算其指数的函数初始化它的元素,并已存储在数据部分的结果的C++ 11阵列的一种方式(.RODATA)是使用模板,部分特和constexpr如下:
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 1000000;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> constexpr A fs() { return A{{ f(i)... }}; }
template<int...> struct S;
template<int... i> struct S<0,i...>
{ static constexpr A gs() { return fs<0,i...>(); } };
template<int i, int... j> struct S<i,j...>
{ static constexpr A gs() { return S<i-1,i,j...>::gs(); } };
constexpr auto X = S<N-1>::gs();
int main()
{
cout << …Run Code Online (Sandbox Code Playgroud) 我知道如何选择可变参数模板的第一个参数:
template< class...Args> struct select_first;
template< class A, class ...Args> struct select_first<A,Args...>{ using type = A;};
Run Code Online (Sandbox Code Playgroud)
这很简单.但是,select_last不相似:
template< class ...Args> struct select_last;
template< class A> struct select_last<A> { using type = A; };
template< class A, class Args...> struct select_last<A,Args...>{
using type = typename select_last<Args...>::type;
};
Run Code Online (Sandbox Code Playgroud)
该解决方案需要深度递归模板即时.我尝试用以下方法解决这个问题:
template< class A, class Args...>
struct select_last< Args ... , A>{ using type = A; }; // but it's not compiled.
Run Code Online (Sandbox Code Playgroud)
问:存在更有效的方法来选择可变参数模板的最后一个参数吗?