账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    c++ primer中auto与decltype
    50
    0

    看c++ primer,对auto和decltype有些疑问,搜索看到geekforgeeks上面的一篇文章中的例子:

    // C++ program to demonstrate use of decltype in functions 
    #include <bits/stdc++.h> 
    using namespace std; 
    
    // A generic function which finds minimum of two values 
    // return type is type of variable which is minimum 
    template <class A, class B> 
    auto findMin(A a, B b) -> decltype(a < b ? a : b) 
    { 
        return (a < b) ? a : b; 
    } 
    
    // driver function to test various inference 
    int main() 
    { 
        // This call returns 3.44 of doubale type 
        cout << findMin(4, 3.44) << endl; 
    
        // This call returns 3 of double type 
        cout << findMin(5.4, 3) << endl; 
    
        return 0; 
    } 
    

    这个例子中的return type,去掉->decltype(a<b?a:b)编译运行,结果与添加这个是一致的。这是为何,在哪种情况下,不加这段代码会有问题?

    补充answer from fefe

    // C++ program to demonstrate use of decltype in functions
    #include <bits/stdc++.h>
    using namespace std;
    
    // A generic function which finds minimum of two values
    // return type is type of variable which is minimum
    template <class A, class B>
    auto findMin(A a, B b)->decltype(a<b ? a:b)
    {
        if(a > 4)
           return 1;
        else if(a >5)
           return 2.0;
        else
           return 3;
    }
    
    // driver function to test various inference
    int main()
    {
        // This call returns 3.44 of doubale type
        cout << findMin(4, 3.44) << endl;
    
        // This call returns 3 of double type
        cout << findMin(5.4, 3) << endl;
    
        return 0;
    }

    这种情况下,如果去掉decltype会报错。

    维基百科中的一个例子:

    int& foo(int& i);
    float foo(float& f);
    
    template <class T> auto transparent_forwarder(T& t) −> decltype(foo(t)) {
      return foo(t);
    }

    如果foo(t)返回的是个引用,此时用auto也会有问题。
    auto需要手动添加&当处理引用时。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 嶶笶吥繻崾、 普通会员 1楼

      在C++ Primer中,autodecltype是两个重要的关键字,用于声明和操作变量的类型信息。

      auto 是一个特殊的关键字,用于声明动态类型变量(也称为变量提升自动类型)。它的语法如下:

      cpp auto variable_name = value;

      在这个例子中,variable_nameauto关键字声明的一个变量,其类型为int,因为我们没有指定具体的类型。这意味着variable_name可以是任何类型的变量,但必须是引用类型,如auto &auto const &auto const *

      decltype 是一个特殊的关键字,用于声明函数返回类型。它的语法如下:

      cpp decltype(expression) return_type;

      在这个例子中,expression是一个表达式,它返回一个类型。decltype(expression)会返回这个表达式的结果类型。例如,如果expression是一个表达式int a = 5;, decltype(expression)将会返回int类型。

      举个例子,如果有一个名为calculate_average的函数,它的返回类型是double,我们可以这样声明它:

      cpp double calculate_average(double a, double b) { return (a + b) / 2; }

      在这个例子中,calculate_average函数的返回类型是double,因为我们没有指定具体的类型。calculate_average会计算两个输入值ab的平均值,并返回这个结果。

      注意,autodecltype关键字的作用是将变量的类型信息提升到函数的调用点。这意味着在函数内部,我们可以直接使用变量的类型,而无需显式声明它的类型。例如:

      cpp auto sum = calculate_average(3, 5);

      在这个例子中,calculate_average函数会计算两个输入值的和,并返回这个和,这就在函数内部将变量sum的类型信息提升到了calculate_average函数的调用点,从而无需显式声明sum的类型。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部