• C++ Decltype 关键字


    12.1.9 C++ Decltype 关键字

    12.1.9.1 问题描述
    template<class T1, class T2>
    void ft(T1 x, T2 y)
    {
        ...
        ?type? xpy = x + y;//C++98 can't explain...,xpy的类型无法描述
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    12.1.9.2 Decltype 关键字(C++11)

    The C++11 solution is a new keyword: decltype.可以这样更改函数:

    template<class T1, class T2>
    void ft(T1 x, T2 y)
    {
        ...
        decltype(x + y) xpy = x + y;
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    但是decltype是如何评估是什么类型的呢,分为四个步骤,假设有如下声明:

    decltype (expression) var;

    Stage 1: 如果expresion是一个没有用括号括起的标识符,则var的类型与该标识符的类型相同,包括const等限定符。

    double x = 5.5;
    double y = 7.9;
    double &rx = x;
    const double * pd;
    decltype(x) w; // w is type double
    decltype(rx) u = y; // u is type double &
    decltype(pd) v; // v is type const double *
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Stage 2: 如果expression是一个函数调用,则var的类型与函数的返回值相同。

    long indeed(int);
    decltype (indeed(3)) m; // m is type long
    //The call expression isn’t evaluated. In this case, the compiler examines the prototype to
    get the return type; there’s no need to actually call the function.
    
    • 1
    • 2
    • 3
    • 4

    Stage 3:如果expression是一个左值,则var为指向其类型的引用。前提是expression是用括号括起的标识符。

    double xx = 4.4;
    decltype ((xx)) r2 = xx; // r2 is double &
    decltype(xx) w = xx; // w is double (Stage 1 match)
    //Incidentally, parentheses don’t change the value or lvaluedness of an expression. For
    example, the following two statements have the same effect:
    xx = 98.6;
    (xx) = 98.6; // () don't affect use of xx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Stage 4: 如果前面的条件都不满足,则var的类型与expression的类型相同。

    int j = 3;
    int &k = j
    int &n = j;
    decltype(j+6) i1; // i1 type int
    decltype(100L) i2; // i2 type long
    decltype(k+n) i3; // i3 type int;
    //Note that although k and n are references, the expression k+n is not a reference; it’s just
    the sum of two ints, hence an int.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    12.1.9.3 后置返回值类型

    如何确定返回值的类型:

    template<class T1, class T2>
    ?type? gt(T1 x, T2 y)
    {
    ...
        return x + y;
    }
    //如果继续使用decltype方式的话,当函数返回值时,x,y的存储空间已经收回了,
    //所以,不知道应该是什么类型
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    C++11定义了新的语法:

    auto h(int x, float y) -> double;
    //-> double成为后置返回类型. 其中auto是一个占位符,表示后置返回类型提供的类型。
    
    • 1
    • 2

    结合->和decltype,可以得出如下解决方案:

    template<class T1, class T2>
    auto gt(T1 x, T2 y) -> decltype(x + y)
    {
        ...
        return x + y;
    }
    //decltype在函数声明后面,因此x和y位于作用域内,可以使用他们。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

  • 相关阅读:
    Python 潮流周刊第 46 期(摘要)+ 赠书 7 本
    OSPF的防止环路的机制
    OPPO的关键一步
    SQLServer快速入门
    EFLAGS寄存器与JCC指令
    点餐小程序实战教程03-用户注册
    Java - HashMap原理分析
    amr文件苹果手机怎么打开?四个方法教会你!
    信息学奥赛一本通:1003:对齐输出
    SimpleDateFormat类的parse和format方法的线程安全问题
  • 原文地址:https://blog.csdn.net/weixin_44410704/article/details/127983155