- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习 C++ 中的模板。我不明白的是:给定一个类型 X
,并创建一个对象 X a;
,如果我定义对 a
的引用通过 X& b = a;
, 然后 std::is_reference<decltype(b)>::value
返回真。但是,如果我将 b 作为模板函数的参数,推导的类型不是引用。
#include <iostream>
class X {};
template<typename T>
void
F(T t)
{
if (std::is_reference<T>::value)
std::cout << "T is a reference" << std::endl;
}
int
main()
{
X a;
X &b = a;
std::cout << std::is_reference<decltype(b)>::value << std::endl; // return true
F(b); //return false
}
我的问题是为什么代码会这样工作。
最佳答案
b
的类型作为表达式 是X
由于[expr.type]/1 :
If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis.
所以T
被推导为X
。但是 decltype(b)
不仅仅是 b
的类型作为表达式。根据[dcl.type.simple]/4 (不相关的部分被我删掉了):
For an expression
e
, the type denoted bydecltype(e)
is defined as follows:
...
otherwise, if e is an unparenthesized id-expression or an unparenthesized class member access,
decltype(e)
is the type of the entity named bye
. ......
请注意,它是由b
(即X&
)命名的实体的类型,而不是表达式b
的类型。
关于c++ - 模板推导和 decltype(T) 如何作用于引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50821862/
我想要一个“父”节点,它生成数据并将其传递给工作节点,工作节点将对数据进行必要的计算。 我希望父节点坐下来“监听”工作节点何时告诉他他已准备好接收数据。 实现这一目标的最佳方法是什么?我应该为此使用哪
我想从 c# 写一些 Html(html 是一个例子,这可能是其他语言..) 例如: string div = @" Mon text
由于某种原因,在使用我放置在 XML 中的任何新 View 时,我收到了 nullPointerException。 View 类型(TextView、EditText 等)并不重要。我最初工作的任何
就像在 hibernate 中一样,在 EclipseLink 中,我们有注释 @AdditionalCriteria,它允许我们在数据上添加过滤器。在 hibernate 中,它作为@Filter,
file_get_contents() [function.file-get-contents]: SSL: Connection reset by peer in 上 file_get_conten
我有一个简单的 从某些 JS 中隐藏的标签。 ( display:none )我查看了页面源代码,发现它没有隐藏,但是检查器将其显示为显示:无(内联样式) 为了隔离隐藏 的代码部分,在 JS 代码中
我在 GUI 中工作,我有多种类型的事件可能会导致按钮被禁用。我已经尝试了几种方法来组合这两个 Observable,但我发现的每个解决方案都需要两个 Observable 在产生结果之前发布一个事件
我正在尝试让 jquery 在使用 GWT 时为我做一些 ui 影响。我有添加到页面的通知,点击后应该会消失。由于可能有多个相同类型的通知(警告、错误等),我试图仅在通过 GWT 单击它们时才动态添加
我有一组具有以下结构的复选框: A1 A2 ... 复选框仅用于 UI 控制(不用于表单提交)。我有一个关联的 html 元素,它有一个 onclick
作用于 UDP 套接字时,什么会导致 sendto() 发送的字节数少于请求的字节数? 提出这个问题的动机是找出我需要采取的预防措施,以确保我 始终 在一次调用 sendto() 时收到完整的消息,并
我正在编写一个 postgresql View ,它使用一些复杂的逻辑来生成数据行。它要求我对从查询两个不同表的结果派生的两组数据执行相同的一组步骤。是否可以编写一个概括这些步骤的函数? 例如: CR
我想在按下后更改每个图标的颜色。但是 Expandable Container 中的所有图标在按下其中一个后都会发生变化。 class _ExpandableListViewState extends
我正在动态提取 HTML 内容以在我的站点页面中显示注释。问题是我导入的 HTML 内容在样式标签下嵌入了 CSS,所以它基本上扰乱了我的页面样式。有没有什么办法可以阻止传入的样式作用于我的页面? 例
当我将 stdin FD 状态标志设置为 O_NONBLOCK_fileno, F_GETFL); flag = fcntl(stderr->_fileno, F_GETFL); if(-1
我是一名优秀的程序员,十分优秀!