- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图理解为什么一个模板元编程是 不是 产生无限递归。我试图尽可能减少测试用例,但仍然涉及一些设置,所以请耐心等待:)
设置如下。我有一个通用函数 foo(T)
它将实现委托(delegate)给一个名为 foo_impl
的通用仿函数通过其调用运算符,如下所示:
template <typename T, typename = void>
struct foo_impl {};
template <typename T>
inline auto foo(T x) -> decltype(foo_impl<T>{}(x))
{
return foo_impl<T>{}(x);
}
foo()
出于 SFINAE 目的使用 decltype 尾随返回类型。
foo_impl
的默认实现没有定义任何调用运算符(operator)。接下来,我有一个类型特征来检测
foo()
可以使用
T
类型的参数调用:
template <typename T>
struct has_foo
{
struct yes {};
struct no {};
template <typename T1>
static auto test(T1 x) -> decltype(foo(x),void(),yes{});
static no test(...);
static const bool value = std::is_same<yes,decltype(test(std::declval<T>()))>::value;
};
has_foo<T>::value
如果有效
foo_impl
则为真
T
存在特化,否则为假。最后,我对整数类型和浮点类型的实现仿函数有两个特化:
template <typename T>
struct foo_impl<T,typename std::enable_if<std::is_integral<T>::value>::type>
{
void operator()(T) {}
};
template <typename T>
struct foo_impl<T,typename std::enable_if<has_foo<unsigned>::value && std::is_floating_point<T>::value>::type>
{
void operator()(T) {}
};
foo_impl
专门化,浮点类型的一种,我添加了额外的条件
foo()
必须可用于类型
unsigned
(
has_foo<unsigned>::value
)。
int main()
{
foo(1.23);
}
foo(1.23)
被称为以下应该发生:
foo_impl
的特化for 整数类型被丢弃,因为 1.23
不是整数,所以只有 foo_impl
的第二个特化被认为; foo_impl
的第二个特化的启用条件包含 has_foo<unsigned>::value
,即编译器需要检查 foo()
可以在类型 unsigned
上调用; foo()
可以在类型 unsigned
上调用,编译器需要再次选择 foo_impl
的特化在可用的两个中; foo_impl
的第二个特化的启用条件编译器再次遇到条件 has_foo<unsigned>::value
. 最佳答案
has_foo<unsigned>::value
是一个非依赖表达式,因此它立即触发 has_foo<unsigned>
的实例化(即使从未使用过相应的特化)。
相关规则为[temp.point]/1:
For a function template specialization, a member function template specialization, or a specialization for a member function or static data member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization and the context from which it is referenced depends on a template parameter, the point of instantiation of the specialization is the point of instantiation of the enclosing specialization. Otherwise, the point of instantiation for such a specialization immediately follows the namespace scope declaration or definition that refers to the specialization.
The program is ill-formed, no diagnostic required, if:
- [...]
- a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter, or
- the interpretation of such a construct in the hypothetical instantiation is different from the interpretation of the corresponding construct in any actual instantiation of the template.
has_foo<unsigned>
的自由。在上面示例中出现的位置,并赋予它与在那里实例化相同的语义。 (请注意,这里的规则实际上是错误的:另一个实体的声明所引用的实体的实例化点实际上必须紧接在该实体之前,而不是紧随其后。这已被报告为核心问题,但它不在问题列表尚未更新,因为该列表已经有一段时间没有更新了。)
has_foo
的实例化点在浮点部分特化内发生在该特化的声明点之前,即
>
之后。根据 [basic.scope.pdecl]/3 的部分特化:
The point of declaration for a class or class template first declared by a class-specifier is immediately after the identifier or simple-template-id (if any) in its class-head (Clause 9).
foo
来自
has_foo<unsigned>
查找
foo_impl
的部分特化,它根本找不到浮点特化。
void
逗号运算符:
static auto test(T1 x) -> decltype(foo(x),void(),yes{});
operator,
仍然会为逗号运算符执行查找,其中它的操作数之一是类或枚举类型(即使它永远不会成功)。这可能会导致执行 ADL [允许实现但不需要跳过此操作],这会触发返回类型为 foo 的所有关联类的实例化(特别是,如果
foo
返回
unique_ptr<X<T>>
,这可能会触发
X<T>
的实例化,并且如果该实例化在此翻译单元中不起作用,则可能导致程序格式错误)。您应该更愿意将用户定义类型的逗号运算符的所有操作数转换为
void
:
static auto test(T1 x) -> decltype(void(foo(x)),yes{});
template <typename T1>
static auto test(T1 x) -> decltype(void(foo(x)),yes{});
static no test(...);
static const bool value = std::is_same<yes,decltype(test(std::declval<T>()))>::value;
T
是不能作为参数传递的类型,例如 void
, 你会触发一个硬错误而不是 value
评估为 false
符合预期 T
是无法形成引用的类型,您再次触发硬错误 foo
可应用于 remove_reference<T>
类型的左值即使T
是一个右值引用 yes
test
的版本而不是拆分
declval
部分到
value
:
template <typename T1>
static auto test(int) -> decltype(void(foo(std::declval<T1>())),yes{});
template <typename>
static no test(...);
static const bool value = std::is_same<yes,decltype(test<T>(0))>::value;
// elsewhere
template<int N> struct rank : rank<N-1> {};
template<> struct rank<0> {};
template <typename T1>
static no test(rank<2>, std::enable_if_t<std::is_same<T1, double>::value>* = nullptr);
template <typename T1>
static yes test(rank<1>, decltype(foo(std::declval<T1>()))* = nullptr);
template <typename T1>
static no test(rank<0>);
static const bool value = std::is_same<yes,decltype(test<T>(rank<2>()))>::value;
test
的声明,您的类型特征将在编译时更快地计算并使用更少的内存。
has_foo
的定义之外(可能进入一些辅助类或命名空间);这样,它们不需要为每次使用
has_foo
重复实例化一次。 .
关于templates - 期望无限递归模板实例化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059511/
template struct List { }; template class> struct ListHelper; template struct ListHelper> { };
最近,我注意到 html/template.Template 的 Templates() 与 text/template.Template 的工作方式不同。 // go1.12 func main()
我正在尝试使用 polymer 1.0 实现一个网站。我有一个自定义元素 my-greeting,里面有一些模板重复。 我想做的是获取一个名为 TARGET 的字符串,但我不知道该怎么做: /cons
(是的,由于我糟糕的英语,标题很奇怪;我希望有人能改进它。) 接听this question ,我发现这段代码有效: template class A { }; template class U>
这个问题在这里已经有了答案: How to import and use different packages of the same name (2 个答案) 关闭 4 年前。 我正在使用 Go
我的想法是这是不可能的,或者我缺少一个额外的步骤。无论哪种方式,我都被卡住了,无法弄清楚。 使用内联模板的原因是能够使用 Laravel Blade 语法并结合 Vue Js 的强大功能。似乎是两者中
我已经尝试实现一个“模板模板模板”——模板类来满足我的需求(我对使用模板元编程很陌生)。不幸的是,我发现以下主题为时已晚: Template Template Parameters 不过,我需要实现如
Helm _helpers.tpl? Helm 允许使用 Go templating在 Kubernetes 的资源文件中。 一个名为 _helpers.tpl 的文件通常用于定义 Go 模板助手,语
{{template "base"}} 和 {{template "base".}} 有什么区别? 我用的是go-gin,两者都可以正常运行。我在文档中找不到关于此的任何描述。 最佳答案 来自 god
我有一个本质上充当查找表的函数: function lookup(a::Int64, x::Float64, y::Float64) if a == 1 z = 2*x + y else if a =
当 out 成员函数(来自模板和特化)都需要模板时,为什么 c++ 需要模板参数,因为我没有得到它,谷歌也没有帮助。必须是c++11但和c++1z有同样的错误。 我正在使用 g++ 7.3.0 收到此
我正在寻找简单的方法来将带有 ${myvar} 的简单模板转换为带有 {{ myvar }} 的 GO 模板。 是否有任何库可以实现这一点? 最佳答案 使用正则表达式查找 \${([a-z0-9\_\
我有这个模板可以将 slice 的多个项目解析到页面上。它确实做得很好。 但是,我现在想使用完全相同的模板来根据范围索引解析 slice 的单个值。该 slice 在多个文件中使用,所以我不能像 Sl
要清理模板文件夹,我想将常用模板保存在子文件夹中。目前我有以下文件结构: main.go templates/index.tpl # Main template for the
最近我设计了元类型和允许编译时类型连接的可能操作: #include template typename T> struct MetaTypeTag {}; /*variable template
准备模板时发生错误。谁能告诉你怎么修? 如有必要,还可以编辑变量。 vars: AllСountry: - "name1" - "name2"
我在使用新的匿名模板引擎时遇到问题。它不能使用嵌套模板。我收到错误消息:“此模板引擎不支持嵌套在其模板中的匿名模板”。 我的问题:我如何强制 knockout JS 使用jquery 模板引擎,而不是
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 8
我在 C++ 中使用带有模板的集合: template class OMSSVDisk : public OMSSObjProperties{ set memberPDs; }; 如上面代码中
因为我喜欢分离接口(interface)和实现,而不是只在头文件中实现模板类,我将它分成 .h 和 .tpp(.tpp 这样它就不会用 *.cpp 编译)。然后我将 tpp 包含在头文件的末尾,就在
我是一名优秀的程序员,十分优秀!