- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下简单(就模板问题而言)示例:
#include <iostream>
template <typename T>
struct identity;
template <>
struct identity<int> {
using type = int;
};
template<typename T> void bar(T, T ) { std::cout << "a\n"; }
template<typename T> void bar(T, typename identity<T>::type) { std::cout << "b\n"; }
int main ()
{
bar(0, 0);
}
+---+-------------------------------+-------------------------------------------+
| | Parameters | Arguments |
+---+-------------------------------+-------------------------------------------+
| a | T, typename identity<T>::type | UniqueA, UniqueA |
| b | T, T | UniqueB, typename identity<UniqueB>::type |
+---+-------------------------------+-------------------------------------------+
typename identity<UniqueB>::type
视为类型,并且不对其进行求值。也就是说,这将被合成为:
+---+-------------------------------+--------------------+
| | Parameters | Arguments |
+---+-------------------------------+--------------------+
| a | T, typename identity<T>::type | UniqueA, UniqueA |
| b | T, T | UniqueB, UniqueB_2 |
+---+-------------------------------+--------------------+
T
推论给它们两者。
A
的推论应该失败。对于第一个参数,您将匹配
T == UniqueA
。第二个参数是非推论上下文-如果
UniqueA
可转换为
identity<UniqueA>::type
,推论成功吗?后者是替代失败,因此我也看不到这种推论如何成功。
最佳答案
正如评论中所讨论的那样,我相信功能模板部分排序算法的某些方面在标准中尚不清楚或根本没有指定,这在您的示例中得以展示。
为了使事情变得更加有趣,MSVC(我测试了12和14)拒绝了该电话的通话。我认为标准中没有什么可以结论性地证明哪个编译器是正确的,但是我想我可能会知道差异的根源。下面有一个注释。
您的问题(和this one)挑战了我对事情的工作方式进行更多调查。我决定写这个答案并不是因为我认为它是权威的,而是将我发现的信息组织在一个地方(它不适合放在注释中)。我希望它会有用。
首先,为issue 1391提出的解决方案。我们在评论和聊天中进行了广泛的讨论。我认为,尽管确实提供了一些说明,但同时也引入了一些问题。它将[14.8.2.4p4]更改为(新文本以粗体显示):
Each type nominated above from the parameter template and the corresponding type from the argument template are used as the types of
P
andA
. If a particularP
contains no template-parameters that participate in template argument deduction, thatP
is not used to determine the ordering.
P
是非依赖性的,则它根本不包含任何模板参数,因此也不包含任何参与参数推导的参数,这将使粗体语句适用于该参数。但是,这会使template<class T> f(T, int)
和template<class T, class U> f(T, U)
无序,这没有任何意义。这可以说是措辞的解释问题,但可能引起困惑。 template<class T> void f(T)
和template<class T> void f(typename A<T>::a)
处于无序状态(推论从第一个到第二个成功,因为T
并未根据新规则用于部分排序的类型中使用,因此可以保持不带任何值)。目前,我测试过的所有编译器都报告第二种更为专业。 #2
比#1
更专业:#include <iostream>
template<class T> struct A { using a = T; };
struct D { };
template<class T> struct B { B() = default; B(D) { } };
template<class T> struct C { C() = default; C(D) { } };
template<class T> void f(T, B<T>) { std::cout << "#1\n"; } // #1
template<class T> void f(T, C<typename A<T>::a>) { std::cout << "#2\n"; } // #2
int main()
{
f<int>(1, D());
}
#2
的第二个参数未用于部分排序,因此推论成功地从#1
转换为#2
,反之则不行)。当前,该 call 是模棱两可的,可以说应该保持不变。 For a
P
/A
pair:
- If
P
is non-dependent, deduction is considered successful if and only ifP
andA
are the same type.- Substitution of deduced template parameters into the non-deduced contexts appearing in
P
is not performed and does not affect the outcome of the deduction process.- If template argument values are successfully deduced for all template parameters of
P
except the ones that appear only in non-deduced contexts, then deduction is considered successful (even if some parameters used inP
remain without a value at the end of the deduction process for that particularP
/A
pair).
P
)后与A
兼容,从而使A
成为可能。这可能导致对部分订购期间实际发生的事情感到困惑;没有替代。 P
具有类似A<T, typename U::b>
的形式的情况,这些问题未包含在第1391期的措辞中。Function template
F
is at least as specialized as function templateG
if and only if:
- for each pair of types used to determine the ordering, the type from
F
is at least as specialized as the type fromG
, and,- when performing deduction using the transformed
F
as the argument template andG
as the parameter template, after deduction is done for all pairs of types, all template parameters used in the types fromG
that are used to determine the ordering have values, and those values are consistent across all pairs of types.
F
is more specialized thanG
ifF
is at least as specialized asG
andG
is not at least as specialized asF
.
P
中的所有模板参数都需要在推导过程中接收该特定
P
/
A
对的值,以便推论成功地从
A
转换为
P
。我认为这可能是导致您的示例与其他示例之间实现差异的原因,但是我至少看到一种情况,上述情况似乎并不适用,因此我不确定该相信什么。
template<typename T> void bar(T, T)
更改为
template<typename T, typename U> void bar(T, U)
会交换结果:在Clang和GCC中,调用是模棱两可的,但在MSVC中,解析为
b
。
#include <iostream>
template<class T> struct A { using a = T; };
template<class, class> struct B { };
template<class T, class U> void f(B<U, T>) { std::cout << "#1\n"; }
template<class T, class U> void f(B<U, typename A<T>::a>) { std::cout << "#2\n"; }
int main()
{
f<int>(B<int, int>());
}
#2
,但是MSVC拒绝该调用是模棱两可的。不知道为什么。
A
类型的形式,因为通常无法确定替换形式可以解析为哪些具体类型。合成的类型是未知的,这使其与模板参数非常相似。
P
时,Clang的模板参数推导算法只是通过为该特定步骤返回“成功”来跳过它。这不仅在部分排序期间发生,而且在所有类型的推导中不仅发生在函数参数列表的顶级,而且在遇到复合类型形式的非推导上下文时都以递归方式发生。由于某种原因,我第一次见到它时感到很惊讶。考虑一下,它当然是有道理的,并且符合标准([14.8.2.5p4]中不参与类型推导)。
关于c++ - 模板部分排序-为什么部分推理在这里成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394260/
现在,我正在使用 MALLET 包中的 LDA 主题建模工具对我的文档进行一些主题检测。最初一切都很好,我从中得到了 20 个主题。但是,当我尝试使用该模型推断新文档时,结果有点莫名其妙。 例如,我故
我正在使用 Jersey 在 Scala 中开发 REST web 服务JAX-RS 引用实现,我收到一个奇怪的错误。 我正在尝试创建一个 ContentDisposition对象使用 Content
以下两个用于计算斐波那契数列第 n 项的 Haskell 程序具有截然不同的性能特征: fib1 n = case n of 0 -> 1 1 -> 1 x -> (fib
所以在来自 another question 的评论中,我刚刚看到了这个计算字符串中 L 数量的例子: "hello".count('l'==) 而且够疯狂……它有效。 从完全扩展的版本开始,我们有:
我在 android 上运行训练有素的 yolov2 网络时遇到问题。我正在使用这个项目进行测试 https://github.com/szaza/android-yolo-v2 . 提供的网络工作正
我目前在我的 iOS 应用程序中使用 Tensorflow 的 Swift 版本。我的模型工作正常,但我无法将数据复制到第一个张量中,因此我可以使用神经网络来检测东西。 我咨询了the testsui
我有一个 SSD tflite 检测模型,正在台式计算机上使用 Python 运行。就目前而言,我的下面的脚本将单个图像作为推理的输入,并且运行良好: # Load TFLite model
我所拥有的:在 Tensorflow 中经过训练的递归神经网络。 我想要的:一个可以尽可能快地运行这个网络的移动应用程序(只有推理模式,没有训练)。 我相信有多种方法可以实现我的目标,但我希望您能提供
**我得到了一些让我的函数成为纯通用函数的建议,这可行,但我更愿意将函数限制为仅接受 Base 及其子项。 在创建可以接受可变模板类基类型参数的函数时遇到问题,而该函数实际上将使用从 Base 派生的
我想使用 TF 2.0 在我的 GPU 集群上运行分布式预测。我使用 MirroredStrategy 训练了一个用 Keras 制作的 CNN 并保存了它。我可以加载模型并在其上使用 .predic
实现一个 C++ 代码来加载一个已经训练好的模型然后获取它而不是使用 Python 真的值得吗? 我想知道这一点,因为据我所知,用于 python 的 Tensorflow 是幕后的 C++(对于 n
我将在网站上提供 pytorch 模型(resnet18)。 然而,在 cpu(amd3600) 中进行推理需要 70% 的 cpu 资源。 我不认为服务器(heroku)可以处理这个计算。 有什么方
为了充分利用 CPU/GPU,我运行了多个对不同数据集进行 DNN 推理(前馈)的进程。由于进程在前馈期间分配了 CUDA 内存,因此我收到了 CUDA 内存不足错误。为了缓解这种情况,我添加了 to
你知道用 1 个 GPU tensorflow 对 2 个 python 进程进行推理的优雅方法吗? 假设我有 2 个进程,第一个是分类猫/狗,第二个是分类鸟/飞机,每个进程运行不同的 tensorf
我是 Scala 的初学者,不明白这里发生了什么: 给定: val reverse:Option[MyObject] = ... 并且myObject.isNaire返回 bool 值。 如果我这样做
我正在尝试通过用我常用的语言 Clojure 实现算法 W 来自学 Hindley-Milner 类型推理。我遇到了 let 推理的问题,我不确定我是否做错了什么,或者我期望的结果是否需要算法之外的东
我正在尝试通过用我常用的语言 Clojure 实现算法 W 来自学 Hindley-Milner 类型推理。我遇到了 let 推理的问题,我不确定我是否做错了什么,或者我期望的结果是否需要算法之外的东
我做了一个项目,基本上使用带有 tensorflow 的 googles object detection api。 我所做的只是使用预训练模型进行推理:这意味着实时对象检测,其中输入是网络摄像头的视
我有一台带有多个 GPU 的服务器,我想在 Java 应用程序内的模型推理期间充分利用它们。默认情况下,tensorflow 占用所有可用的 GPU,但仅使用第一个。 我可以想到三个选项来解决这个问题
这个预测时间190ms,应该是cpu版本 昨天修改了个OpenCV DNN支持部署YOLOv5,6.1版本的Python代码,今天重新转换为C 代码了!貌似帧率比之前涨了点!说明C的确是比Python
我是一名优秀的程序员,十分优秀!