auto Function with if Statement won't return a value

前端 未结 3 1884
醉话见心
醉话见心 2021-01-19 18:11

I made a template and an auto function that compare 2 values and return the smallest one. This is my code:

#include 

        
3条回答
  •  轮回少年
    2021-01-19 18:57

    Until yesterday (2017-12-06) this was not compiling under MSVC. But after VS 15.5 update it does.

        auto msvc_does_compile = [](auto _string)
         {
          using string_type = decltype(_string);
          return std::vector{};
         };
       /*
        OK since VS 2017 15.5 update
       auto vec1 = msvc_does_compile( std::string{} );
       */
    

    Adding explicit return type will choke MSVC , but not gcc/clang as usual:

    auto msvc_does_not_compile = [](auto _string)
        // explicit return type makes msvc not to compile
        -> std::vector< decltype(_string) >
      {
        using string_type = decltype(_string);
        return std::vector{};
      };
    

    And something of the same but simpler will be stopped even at the IDE stage:

        auto msvc_ide_does_not_allow = []( bool wide )
        {
           if (wide)
            return std::vector();
    
            return std::vector();
        };
    

    Yes, again that troublesome pair gcc/clang has no problems with the above. Try which ever online ide you prefer to convince yourself...

提交回复
热议问题