Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Return type of lambda function
Forum Updated to NodeBB v4.3 + New Features

Return type of lambda function

Scheduled Pinned Locked Moved Unsolved C++ Gurus
12 Posts 4 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on 14 May 2020, 14:01 last edited by Vinoth Rajendran4
    #1

    Hi All,

    auto x1 = [](int i) { return i; };
    cout << typeid(x1).name();
    

    The output for type x1 is class <lambda_8cff3db0c4e5c934a92a739779c1aa4d> . I was expecting the type deduced for x1 to be int.

    Can someone help me with the following points,

    • why the type deduced is class <lambda_8cff3db0c4e5c934a92a739779c1aa4d> ?

    • How to make lambda to return int?

    I am using MSVC Compiler.

    J 1 Reply Last reply 14 May 2020, 14:53
    0
    • V Vinoth Rajendran4
      14 May 2020, 14:01

      Hi All,

      auto x1 = [](int i) { return i; };
      cout << typeid(x1).name();
      

      The output for type x1 is class <lambda_8cff3db0c4e5c934a92a739779c1aa4d> . I was expecting the type deduced for x1 to be int.

      Can someone help me with the following points,

      • why the type deduced is class <lambda_8cff3db0c4e5c934a92a739779c1aa4d> ?

      • How to make lambda to return int?

      I am using MSVC Compiler.

      J Offline
      J Offline
      JonB
      wrote on 14 May 2020, 14:53 last edited by JonB
      #2

      @Vinoth-Rajendran4
      I just think your expectation is wrong! x1 is the lambda itself, hence the output. You are confusing that with what will be returned when the lambda is executed, which is a totally different thing. Your lambda will return an int when executed, so you're all done, what makes you think there is any issue?

      I am not a C++ expert, but if you like you can specify the lambda return type via

      auto x1 = [](int i) -> int { return i; };
      

      but I don't think you have to/will make any difference here (you have just a simple return statement in it).

      V 1 Reply Last reply 14 May 2020, 15:03
      3
      • J JonB
        14 May 2020, 14:53

        @Vinoth-Rajendran4
        I just think your expectation is wrong! x1 is the lambda itself, hence the output. You are confusing that with what will be returned when the lambda is executed, which is a totally different thing. Your lambda will return an int when executed, so you're all done, what makes you think there is any issue?

        I am not a C++ expert, but if you like you can specify the lambda return type via

        auto x1 = [](int i) -> int { return i; };
        

        but I don't think you have to/will make any difference here (you have just a simple return statement in it).

        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on 14 May 2020, 15:03 last edited by
        #3

        @JonB , Thanks for the update.. I got the point.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 14 May 2020, 15:19 last edited by
          #4

          I cannot remember everything I was doing here, but I was testing if getting the return type would invoke the lambda. I think it will not and it will give you the return type. You will have to search online the other crap I was testing here as I don't remember:

              {
                  auto func = [](int a){
                      qInfo() << "Invoked";
                      return a;
                  };
                  auto a = func(1);
                  qInfo() << typeid(a).name();
                  decltype (a) b;
                  qInfo() << typeid(b).name();
                  qInfo() << typeid(func(1)).name();
              }
          

          Types returned by typeid are compiler specific if I remember correctly. So it would not be suitable for cross platform code unless you can ensure the same compiler everywhere.

          C++ is a perfectly valid school of magic.

          K 1 Reply Last reply 14 May 2020, 22:58
          0
          • F fcarney
            14 May 2020, 15:19

            I cannot remember everything I was doing here, but I was testing if getting the return type would invoke the lambda. I think it will not and it will give you the return type. You will have to search online the other crap I was testing here as I don't remember:

                {
                    auto func = [](int a){
                        qInfo() << "Invoked";
                        return a;
                    };
                    auto a = func(1);
                    qInfo() << typeid(a).name();
                    decltype (a) b;
                    qInfo() << typeid(b).name();
                    qInfo() << typeid(func(1)).name();
                }
            

            Types returned by typeid are compiler specific if I remember correctly. So it would not be suitable for cross platform code unless you can ensure the same compiler everywhere.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 14 May 2020, 22:58 last edited by
            #5

            There's std::invoke_result for that purpose. It goes like this (more or less):

            auto x1 = [](int i) -> int { return i; };
            
            typedef typename std::invoke_result<decltype(x1), int>::type x1ret_type; //< which is int
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            4
            • F Offline
              F Offline
              fcarney
              wrote on 15 May 2020, 15:47 last edited by
              #6

              This is interesting:

                 {
                      auto x1 = [](int i) -> int {
                          qInfo() << "Invoked";
                          return i;
                      };
              
                      typedef typename std::invoke_result<decltype(x1), int>::type x1ret_type;
                      typedef decltype (x1(1)) rtype;
              
                      Q_ASSERT(typeid(rtype) == typeid(x1ret_type));
                      qInfo() << typeid(rtype).name() << typeid(x1ret_type).name();
                  }
              

              C++ is a perfectly valid school of magic.

              K 1 Reply Last reply 15 May 2020, 18:14
              0
              • F fcarney
                15 May 2020, 15:47

                This is interesting:

                   {
                        auto x1 = [](int i) -> int {
                            qInfo() << "Invoked";
                            return i;
                        };
                
                        typedef typename std::invoke_result<decltype(x1), int>::type x1ret_type;
                        typedef decltype (x1(1)) rtype;
                
                        Q_ASSERT(typeid(rtype) == typeid(x1ret_type));
                        qInfo() << typeid(rtype).name() << typeid(x1ret_type).name();
                    }
                
                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 15 May 2020, 18:14 last edited by
                #7

                @fcarney said in Return type of lambda function:

                This is interesting:

                What is?

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fcarney
                  wrote on 15 May 2020, 19:42 last edited by
                  #8

                  I didn't know if decltype would not execute the function to get the return type. I was playing with it. I knew typeid did, didn't know about decltype. So the two methods produce the same output.

                  C++ is a perfectly valid school of magic.

                  K 1 Reply Last reply 15 May 2020, 20:18
                  0
                  • F fcarney
                    15 May 2020, 19:42

                    I didn't know if decltype would not execute the function to get the return type. I was playing with it. I knew typeid did, didn't know about decltype. So the two methods produce the same output.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 15 May 2020, 20:18 last edited by
                    #9

                    @fcarney said in Return type of lambda function:

                    I didn't know if decltype would not execute the function to get the return type.

                    It would if it could but it can't so it shan't. decltype is evaluated at compile time, so there's no way it could or would execute a function (unless the function is also evaluated at compile time, which it isn't here). On the other hand typeid is a runtime-executed operator.

                    Read and abide by the Qt Code of Conduct

                    F 1 Reply Last reply 15 May 2020, 20:48
                    3
                    • K kshegunov
                      15 May 2020, 20:18

                      @fcarney said in Return type of lambda function:

                      I didn't know if decltype would not execute the function to get the return type.

                      It would if it could but it can't so it shan't. decltype is evaluated at compile time, so there's no way it could or would execute a function (unless the function is also evaluated at compile time, which it isn't here). On the other hand typeid is a runtime-executed operator.

                      F Offline
                      F Offline
                      fcarney
                      wrote on 15 May 2020, 20:48 last edited by
                      #10

                      @kshegunov Just to be clear, I think I was not clear, typeid does NOT execute the function to find the return type either. I think I said it strangely.

                      C++ is a perfectly valid school of magic.

                      K 1 Reply Last reply 15 May 2020, 20:59
                      0
                      • F fcarney
                        15 May 2020, 20:48

                        @kshegunov Just to be clear, I think I was not clear, typeid does NOT execute the function to find the return type either. I think I said it strangely.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 15 May 2020, 20:59 last edited by
                        #11

                        @fcarney said in Return type of lambda function:

                        @kshegunov Just to be clear, I think I was not clear, typeid does NOT execute the function to find the return type either. I think I said it strangely.

                        Are you sure? That'd be odd.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fcarney
                          wrote on 15 May 2020, 22:10 last edited by fcarney
                          #12
                              {
                                  auto x1 = [](int i) -> int {
                                      qInfo() << "Invoked";
                                      return i;
                                  };
                          
                                  typedef typename std::invoke_result<decltype(x1), int>::type x1ret_type;
                                  typedef decltype (x1(1)) rtype;
                          
                                  Q_ASSERT(typeid(rtype) == typeid(x1ret_type));
                                  qInfo() << typeid(rtype).name() << typeid(x1ret_type).name();
                          
                                  qInfo() << "before typeid";
                                  typeid(x1(1));
                                  qInfo() << "after typeid";
                          
                              }
                          

                          Pretty sure, I saw this behavior before (tested again):

                          i i
                          before typeid
                          after typeid
                          

                          C++ is a perfectly valid school of magic.

                          1 Reply Last reply
                          1

                          1/12

                          14 May 2020, 14:01

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved