类型在可变参数模板参数包中的位置
我正在尝试 C++0x,我想知道如何解决出现的以下问题。 我有一个可变参数模板类:
template<typename... T>
class MyLovelyClass {
template<typename SomeType>
void DoSthWithStorageOfSomeType();
private:
std::tuple<std::vector<T>...> m_storage;
};
该函数应该对 m_storage 元组中对应于 SomeType 模板参数的向量进行某种操作(如果不这样做,则编译时失败)。 怎样才能做到这一点呢?
我的想法是在参数包中找到 SomeType 的索引,然后使用 std::get 获取适当的向量,但我不知道如何执行第一部分。
I'm giving C++0x a try and I was wondering how to solve the following problem that came up.
I've got a variadic template class:
template<typename... T>
class MyLovelyClass {
template<typename SomeType>
void DoSthWithStorageOfSomeType();
private:
std::tuple<std::vector<T>...> m_storage;
};
The function there is suppose to do some sort of manipulation on the vector in m_storage tuple that corresponds to the SomeType template argument (or compile time fail if it doesn't).
How one can do this?
My idea was to find the index of SomeType in parameter pack and then use std::get to obtain the appropriate vector, but I don't know how to to do the first part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一些代码,用于对元组中找到的第一个类型 U 进行线性搜索,如果找不到 U,则会给出编译时错误。请注意,如果元组包含多个 U,则它只会找到第一个。不确定这是否是您想要的政策。它将编译时索引返回到第一个 U 的元组中。也许您可以将其用作
std::get
的索引。免责声明:为这个答案而拼凑在一起。仅进行了轻微测试。诸如空元组之类的边缘情况会出现令人讨厌的错误消息,但可以改进。 ETC。
Here's some code to do a linear search of a tuple for the first type U it finds, and gives a compile-time error if it can't find U. Note if the tuple contains multiple U's it only finds the first one. Not sure if that is the policy you want or not. It returns the compile-time index into the tuple of the first U. Perhaps you could use it as the index into your
std::get
.Disclaimer: Thrown together for this answer. Only lightly tested. Edge cases such as an empty tuple have a nasty error message that could be improved. etc.
C++14 解决方案:
使用:
C++14 solution:
Use:
我通过返回 SIZE_MAX 向 Elazar 的解决方案添加了对“未找到类型”情况的支持:
编辑:我转而使用参数包的大小作为“未找到”索引值。这就像 STL 中“最后一次”索引或迭代器的用法,是一个更优雅的解决方案:
I added support for the "type not found" case to Elazar's solution, by returning SIZE_MAX:
EDIT: I switched to using the size of the parameter pack as the "not found" index value. This is like the STL usage of a "one past the end" index or iterator and makes a more elegant solution: