相关疑难解决方法(0)

为什么对std :: tuple实现使用递归继承不好?

这个问题上,Howard Hinnant说

std :: tuple的一些实现使用递归继承.但好的却没有.;-)

有人可以对此有所了解吗?

c++ c++11 stdtuple

23
推荐指数
1
解决办法
2880
查看次数

C++ 11:具有对数评估深度的编译时数组

应用程序图像的实现具有通过由编译器计算其指数的函数初始化它的元素,并已存储在数据部分的结果的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)

c++ c++11

17
推荐指数
1
解决办法
1178
查看次数

选择可变参数模板的最后一个参数的有效方法

我知道如何选择可变参数模板的第一个参数:

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)

问:存在更有效的方法来选择可变参数模板的最后一个参数吗?

c++ variadic-templates c++11

13
推荐指数
5
解决办法
5017
查看次数

标签 统计

c++ ×3

c++11 ×3

stdtuple ×1

variadic-templates ×1