- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下编译按预期运行和执行:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <type_traits>
class Freaky {
public:
template<
typename UNSIGNED_TYPE,
typename std::enable_if<(sizeof(UNSIGNED_TYPE)>=sizeof(int)),int>::type X = 0
>
static UNSIGNED_TYPE copyThing(int x) ;
};
template<
typename UNSIGNED_TYPE,
typename std::enable_if<(sizeof(UNSIGNED_TYPE)>=sizeof(int)),int>::type X
>
UNSIGNED_TYPE Freaky::copyThing(int x) {
UNSIGNED_TYPE r(0);
std::memcpy(&r,&x,sizeof(int));//Please ignore. Not the point of the question...
return r;
}
int main(int argc, char*argv[]) {
std::cout << "The answer is ... " <<
Freaky::copyThing<unsigned long>(10)<<std::endl;
return EXIT_SUCCESS;
}
样本输出(实际输出可能取决于字节顺序和整数大小):
The answer is .... 10
以下不会编译并提示 copyThing()
的实现原型(prototype)与类中声明的不匹配。
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <type_traits>
class Freaky {
public:
template<
typename UNSIGNED_TYPE,
typename std::enable_if<(sizeof(UNSIGNED_TYPE)>=sizeof(int)),int>::type X = 0
>
static UNSIGNED_TYPE copyThing(int x) ;
};
template<
typename UNSIGNED_TYPE,
typename std::enable_if<(sizeof(int)<=sizeof(UNSIGNED_TYPE)),int>::type X
>
UNSIGNED_TYPE Freaky::copyThing(int x) {
UNSIGNED_TYPE r(0);
std::memcpy(&r,&x,sizeof(int));//Please ignore. Not the point of the question...
return r;
}
int main(int argc, char*argv[]) {
std::cout << "The answer is ... " <<
Freaky::copyThing<unsigned long>(10)<<std::endl;
return EXIT_SUCCESS;
}
两者之间唯一的区别是sizeof(UNSIGNED_TYPE)>=sizeof(int)
已替换为 sizeof(int)<=sizeof(UNSIGNED_TYPE)
在该实现中。
显然这两个语句在语义上是等价的。在哪里可以找到关于如何确定模板原型(prototype)相等的正式定义?
这显然是某种程度的词汇对等,而不是语义对等。
最佳答案
我找不到标准的任何部分明确指定类模板(或成员的外联定义中的类模板及其说明符)的重新声明何时相同。
编译器实际上遵循 C++11 14.5.6.1/5+6 中指定的函数模板重新声明规则:
5 Two expressions involving template parameters are considered equivalent if two function definitions containing the expressions would satisfy the one definition rule (3.2), except [template parameter renaming]. 6 Two function template are equivalent if they are declared in the same scope, have the same name, have identical template parameter lists, and have return types and parameter lists that are equivalent using the rules described above to compare expressions involving template parameters.
不过,我找不到任何规则使它适用于非类型模板参数类型中的表达式,以重新声明类模板。
关于原型(prototype)的 C++ 模板等价性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30917253/
是: x -= y; 相当于: x = x - y; 最佳答案 不,它们并不等同于您表达它们的方式。 short x = 0, y = 0; x -= y; // This compiles f
这个问题在这里已经有了答案: What is the rationale for all comparisons returning false for IEEE754 NaN values? (1
我在哪里可以找到与 Python maketrans 和 translate 等效的 C# 代码?谢谢! 最佳答案 这应该带你到那里的大部分方式: public class MakeTrans {
我正在 SwiftUI 中构建一个应用程序其中包含很多组件,包括 Text包含长字符串的 View 。 当我在 iPhone 11 上启动该应用程序时,一切正常,但当我在较小的设备(例如 iPhone
这个问题已经有答案了: What is the equivalent lambda expression for System.out::println (2 个回答) Function pointe
我最近在使用 postgres,我必须做一些计算。然而我一直没能模仿Excel的HOUR()函数,我看了official information但这对我帮助不大。 函数接收一个小数,并得到小数的时、分
如果在 cython 中定义了一个指针 vector ,那么与 python 中的 enumerate 类似的函数或过程是什么,用于遍历指针数组中元素的索引和值在 C 声明类型函数内的循环中? 测试.
要选择: select user_id, max(case when value > 0 then timestamp else 0 end) as max_timestamp_whe
如果没有例子,这个问题很难问,所以这里是: #include struct O { }; struct C { template void function1(void (C::*call
我得到了这个结构的实现: struct NodoQ { Etype elem; NodoQ *sig; }; 下面是这段代码吗, typedef NodoQ *PtrNodoQ; PtrNod
我有一些宏需要访问当前类的类型,目前我通过违反 DRY 的模式解决了这个问题: struct ThisScruct{ int a; double b; //example st
我想知道 TensorFlow 的 softmax_cross_entropy_with_logits 是否有等效的 PyTorch 损失函数? 最佳答案 is there an equivalent
我找到了一个 trie 的 java 实现,并希望在 J2ME 中有一个类似的实现。这是代码。 节点类 import java.util.Collections; import java.util.L
我刚刚学习了 GraphQL,我想找到用户 id=2 OR 用户 id=3 现在我将如何进行 GraphQL 查询,我正在使用以下查询获取整个集合 { users() {
假设我有两个 Web 服务:A 和 B。两者都在 Apache 上运行。我希望它们可以从我的主机的不同端口访问:A 来自端口 88,B 来自端口 89。 我可以手动完成(首先创建图像,然后使用“doc
我一直在 excel 中使用一个非常简单的数组公式来处理一些数据集,但是它们变得太大并且在我更新计算时完全破坏了我的计算机性能。 excel表格和MySQL数据库布局如下: +-Timestamp-+
我有一个类,其实例要通过不同于它们携带的数据值的标识来区分。在我的代码中,我打算使用 == 来表示两个实例在它们的数据方面是等价的,并且 is 表示两个变量引用同一个实例,也就是说,他们是相同的。根据
我正在 Windows 中使用 WinSock 2.0 开发代理服务器。如果我想在阻塞模型中开发它,select() 是等待客户端或远程服务器从中接收数据的方法。是否有任何适用的方法可以使用 I/O
我正在将我制作的 Android 应用移植到 iOS。 Android 有一个 Yield() 函数可以将线程从运行中移到线程队列的后面(?)。这很有用,这样该线程就不会占用过多的 CPU 并使其他一
这是否保证始终为真: std::numeric_limits::max() == INT_MAX C++ 标准对此有何规定?我在标准中找不到任何明确说明这一点的引用资料,但我一直在阅读这些内容应该是等
我是一名优秀的程序员,十分优秀!