- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我想写的代码:
template <typename T1, typename ... tail>
class record : public record<tail...>
{
using baseT = record<tail...>;
T1 elem;
public:
record(T1 first, tail... rest)
: elem(first), baseT(rest...)
{}
template <int index>
inline constexpr T1& get()
{
// line 83:
return baseT::get<index-1>();
}
// line 85:
template <>
inline constexpr T1& get<0>()
{
return elem;
}
};
我得到的编译器输出:(GCC 4.8.2 with -std=c++11
)
record.hpp:85:15: error: explicit specialization in non-namespace scope 'class base::record<T1, tail>'
template <>
^
record.hpp:86:33: error: template-id 'get<0>' in declaration of primary template
inline constexpr T1& get<0>()
^
record.hpp: In member function 'constexpr T1& base::record<T1, tail>::get() const':
record.hpp:83:32: error: expected primary-expression before ')' token
return baseT::get<index-1>();
^
好的,我的意思很明确:类似于 std::tuple
. get()
旨在提供元素访问。
请帮助我让这个骨架正常工作;我所追求的是正确实现这种结构所需的理解;不过有 2 个具体问题:
get<0>()
?typename... tail
是,当编译器到达层次结构的根部时?最佳答案
如果你想显式特化一个函数(成员函数,成员函数模板,..),那么你必须在命名空间范围内这样做:
template <typename T1, typename ... tail>
class record : public record<tail...>
{
using baseT = record<tail...>;
T1 elem;
public:
record(T1 first, tail... rest) // you should use perfect forwarding here
: elem(first), baseT(rest...)
{}
template <int index>
inline constexpr T1& get() // the `inline` is redundant here
{
// line 83:
return baseT::get<index-1>();
}
};
template<typename T1, typename ... tail>
template<>
inline constexpr T1& record<T1, tail...>::template get<0>()
{ return elem; }
但这是不允许的:您不能显式特化一个未显式特化的类模板的成员。在这里,record<T1, tail...>
没有明确专门化;因此你可能没有明确地特化 get
.
还有两个问题:
get
的返回类型必须依赖于索引。tail
中删除一个元素, 所以 tail
最终是空的。然后,record<tail...>
将失败,因为第一个模板参数 T1
tail
时无法设置是空的。因此,你需要专攻record
让它工作的一种方法是使用重载:
#include <type_traits>
#include <utility>
template<int N>
using int_const = std::integral_constant<int, N>;
template <typename T1, typename ... tail>
class record : public record<tail...>
{
using baseT = record<tail...>;
T1 elem;
protected:
using baseT::get_impl; // "unhide" the base class overloads
constexpr T1 const& get_impl(int_const<sizeof...(tail)>) const
{
return elem;
}
public:
template<typename T1_, typename ... tail_>
record(T1_&& first, tail_&&... rest)
: baseT(std::forward<tail_>(rest)...), elem(std::forward<T1_>(first))
{}
template <int index>
constexpr auto get() const
-> decltype( this->get_impl( int_const<sizeof...(tail) - index>{} ) )
{
static_assert(1+sizeof...(tail) > index, "out of bounds");
return this->get_impl( int_const<sizeof...(tail) - index>{} );
}
};
template <typename T1>
class record<T1>
{
T1 elem;
protected:
constexpr T1 const& get_impl(int_const<0>) const
{
return elem;
}
public:
template<typename T1_>
record(T1_&& first)
: elem(first)
{}
template <int index>
constexpr auto get() const
-> decltype( get_impl( int_const<index>{} ) )
{
static_assert(0 == index, "out of bounds");
return this->get_impl( int_const<index>{} );
}
};
#include <iostream>
int main()
{
record<int, double, char, bool> r{42, 1.2, 'c', false};
std::cout << r.get<1>() << '\n';
std::cout << r.get<0>() << '\n';
}
这是一个使用不同继承技术的例子:
#include <type_traits>
#include <utility>
template<int N>
using int_const = std::integral_constant<int, N>;
template<int N, class... Ts>
struct record_impl
{
struct out_of_bounds {};
template<int I>
constexpr out_of_bounds get(int_const<I>) const
{
static_assert(I < N, "out of bounds");
return {};
}
};
template<int N, class T, class... Ts>
struct record_impl<N, T, Ts...> : record_impl<N+1, Ts...>
{
using base = record_impl<N+1, Ts...>;
T mem;
template<class Arg, class... Args>
record_impl(Arg&& arg, Args&&... args)
: base(std::forward<Args>(args)...), mem(std::forward<Arg>(arg))
{}
using base::get;
constexpr T const& get(int_const<N>) const
{ return mem; }
};
template<class... Ts>
using record = record_impl<0, Ts...>;
#include <iostream>
int main()
{
record<int, double, char, bool> r{42, 1.2, 'c', false};
std::cout << r.get(int_const<0>{}) << '\n';
std::cout << r.get(int_const<3>{}) << '\n';
}
record_impl
的用法允许摆脱额外的 get_impl
.它还提供了放置 static_assert
的好机会在主模板的 get
中成员函数。
关于c++ - 可变递归模板 mem 有趣的特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728949/
文档:https://www.xfyun.cn/doc/spark/PPTGeneration.html 价格方面提供了免费1000点的额度,生成一次是10点,正好10
我有一个工具按钮和一个分配给它的操作。但该操作没有点击事件,也没有点击代码。因此该按钮被禁用,如何将其更改为启用? 谢谢! 最佳答案 当某个操作没有 OnExecute 处理程序时,该操作始终处于禁用
我有一个 C 程序,它初始化两个结构并尝试打印它们的值。请参阅下面的代码。 #include #include typedef struct node{ int data; struc
我在回答 this question当我想到这个例子时: #include void func(int i); void func(); int main (){ func(); r
这些bug可能够你喝一壶的。 1、被断言(assert)包含的代码 常发生在切换到release版本时,执行结果乖乖的,最终查找结果是assert括号里的代码在release下是不执行的。
我正在谈论一个独立的对象,稍后我想使用 saveOrUpdate() 进行更新。 我们如何同时设置 null 和clear()引用的集合? A[1:1]B[1:M]C B 到 C 的引用可以为空,这意
我最近完成了 Marjin Haverbeke 的书“Eloquent Javascript, Second Edition”中的一项挑战。 One had to create此控制台输出使用 for
我写了一个 HtmlHelper 表达式,我花了很多时间将标题标签放入我的下拉列表中,如下所示: public static HtmlString SelectFor( thi
你知道有哪些开放的软件项目有特别有趣/写得很好的单元测试吗? 编写单元测试对我来说通常感觉很奇怪,因为它似乎太随机、太密集、太稀疏,……阅读一些现实世界的例子(而不是书籍、图书馆示例代码等)会很棒。
我有一个我目前正在使用的 Silverlight 应用程序,它为其 MVVM 框架实现了 Caliburn.Micro。一切正常,但我注意到一些绑定(bind)中的一些有趣之处。我拥有的是处理应用程序
我通过 swscale 运行 png 图像数据以获取 YUV420P 数据,然后使用 MSMPEG4V1 编解码器对该帧进行编码,从而在 iPhone 上对一些视频进行编码。在 api 文档中,avc
我写了一个页面,发现 addEventListener("loadedmetadata",fun) 在 firefox 上运行不正确 我正在尝试修复一个旧软件的错误。在加载视频和页面时,该软件尝试在页
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一名优秀的程序员,十分优秀!