简体   繁体   English

没有任何变量类型的decltype

[英]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.

相关问题 可以在不使用decltype的情况下推断先前定义的extern变量的类型 - Can the type of a previously defined extern variable be inferred without using decltype 如何在不假设任何构造函数的情况下获取构造函数和取消引用运算符(decltype)的类型? - How to get the type of constructor and dereference operator (decltype) without assuming any constructor? enable_if 在返回类型中没有 decltype 失败 - enable_if fails without decltype in return type 带花括号的引用类型变量上的 decltype - decltype on the variable of reference type with curly braces 用decltype声明的变量类型(具有函数作为表达式) - Type of variable declared with decltype (having function as an expression) decltype(auto)foo()返回本地引用,没有任何警告 - decltype(auto) foo() returns local reference without any warning 我可以使用没有任何实例变量的decltype吗? - Can I use decltype without any instance variables? 在没有decltype的情况下声明时,函数定义中decltype中的从属类型或自变量无法编译 - Dependent type or argument in decltype in function definition fails to compile when declared without decltype decltype是否可能导致内存泄漏(new any_type())? - Are memory leaks possible with decltype(new any_type())? 使用 decltype 推断其类型时,局部变量是左值吗? - Is a local variable an lvalue when deducing its type using decltype?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM