[英]decltype without any variable type
I am not so experienced with decltype usage in C++. 我对C ++中的decltype用法不太熟悉。 However below is code which I finally arrived for my project purpose: 但是下面是我最终出于项目目的而到达的代码:
#include <iostream>
#include <inttypes.h>
#define SA(obj) ((obj)->u)
struct A
{
A()
{
std::cout << "Called" << std::flush << std::endl;
}
uint32_t u;
};
int main()
{
struct A a2;
decltype(A().u) p;
a2.u = 99;
p = a2.u;
if(a2.u != SA(&a2) )
std::cout << "Not Same" << std::flush << std::endl;
else
std::cout << "Same" << std::flush << std::endl;
}
I can see that the A's constructor is called only once cause of below statement: 我可以看到A的构造函数仅被调用一次,原因如下:
struct A a2;
In same concern what does the construct in decltype means - will it not be creating a temporary instance of the structure - 出于同样的考虑,decltype中的构造意味着什么-它将不会创建该结构的临时实例-
decltype(A().u) p;
as the below declaration gives compilation error: 如以下声明给出了编译错误:
decltype(A.u) p;
c++ -std=c++11 try5.cpp
try5.cpp: In function 'int main()':
try5.cpp:18:17: error: invalid type in declaration before ';' token
decltype(A.u) p;
The expression inside decltype
's parentheses is not evaluated . 不评估 decltype
括号内的表达式。 It is just analyzed by the compiler to discover its type, but never translated into actual executable code. 编译器仅对其进行分析以发现其类型,但从未将其转换为实际的可执行代码。
Au
fails the analyzing stage, because you can't use .
Au
无法通过分析阶段,因为您不能使用.
after a type name. 在类型名称之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.