我现在在C++ 11,C++ 14和C++ 1z中学习了一些关于模板和模板的知识。我正在尝试编写一个可变类模板,其内部类将会将int
与每个模板参数相关联 - 并且有一个返回其数组表示的constexpr
方法。将数组与可变模板关联
假设我确保模板不能接收两个与参数相同的类型。我在想这样做有点像这样:
template <typename... Types>
struct MyVariadicTemplate {
//we know that all types in Types... are different
template <int... Values>
struct MyInnerTemplate {
//I need to make sure that sizeof...(Values) == sizeof...(Types)
constexpr std::array<int, sizeof...(Values)> to_array() {
std::array<int, sizeof...(Values)> result = {Values...};
return result;
// this is only valid since C++14, as far as I know
}
};
};
此代码应是有效的(如果不是的话,我很想知道为什么)。现在,我想补充另一内模板:
template <typedef Type>
struct AnotherInnerTemplate {};
具有public typedef
,它代表MyInnerTemplate
一个对Type
在别处Types...
和零点的位置 - 在这里我迷路了。我不知道如何继续
如果能够做到这一点,我将不胜感激 - 如果我朝着错误的方向前进,我希望有人能给我提示如何做到这一点。
我不明白'AnotherInnerTemplate'的'typedef'能否显示一个例子 – bolov