Bar*_*wek 9 metaprogramming variadic-templates c++11
我正在试用C++ 0x,我想知道如何解决出现的以下问题.我有一个可变参数模板类:
template<typename... T>
class MyLovelyClass {
template<typename SomeType>
void DoSthWithStorageOfSomeType();
private:
std::tuple<std::vector<T>...> m_storage;
};
Run Code Online (Sandbox Code Playgroud)
这里的函数假设对m_storage元组中与SomeType模板参数对应的向量进行某种操作(如果没有,则编译时失败).怎么能做到这一点?
我的想法是在参数包中找到SomeType的索引然后使用std :: get来获取适当的向量,但我不知道如何做第一部分.
这里有一些代码可以对它找到的第一个类型U进行元组的线性搜索,如果找不到U,则会发出编译时错误.注意,如果元组包含多个U,它只找到第一个.不确定这是否是您想要的政策.它将编译时索引返回到第一个U的元组中.也许你可以将它用作你的索引std::get.
免责声明:为此答案一起投掷.只是轻微测试.诸如空元组之类的边缘情况具有可以改进的令人讨厌的错误消息.等等
#include <type_traits>
#include <tuple>
template <class Tuple, class T, std::size_t Index = 0>
struct find_first;
template <std::size_t Index, bool Valid>
struct find_first_final_test
: public std::integral_constant<std::size_t, Index>
{
};
template <std::size_t Index>
struct find_first_final_test<Index, false>
{
static_assert(Index == -1, "Type not found in find_first");
};
template <class Head, class T, std::size_t Index>
struct find_first<std::tuple<Head>, T, Index>
: public find_first_final_test<Index, std::is_same<Head, T>::value>
{
};
template <class Head, class ...Rest, class T, std::size_t Index>
struct find_first<std::tuple<Head, Rest...>, T, Index>
: public std::conditional<std::is_same<Head, T>::value,
std::integral_constant<std::size_t, Index>,
find_first<std::tuple<Rest...>, T, Index+1>>::type
{
};
#include <iostream>
int main()
{
typedef std::tuple<char, int, short> T;
std::cout << find_first<T, double>::value << '\n';
}
Run Code Online (Sandbox Code Playgroud)