2021年1月25日星期一

Compiling Kevlin Henney's lambda Fizzbuzz with Visual Studio 2017 using C++17's compiler

I was following Kevlin Henney's Youtube video on Lambdas in programming. At about 20:30 - 20:40 in his video he gives this code snippet:

string fizzbuzz(int n)  {      auto fizz = [=](function<string(string)> f)      {          return n % 3 == 0 ? [=](auto) return "Fizz" + f("");} : f;      };      auto fizz = [=](function<string(string)> f)      {          return n % 5 == 0 ? [=](auto) return "Buzz" + f("");} : f;      };      auto id = [](auto s) { return s; };      return fizz(buzz(id))(to_string(n));  }  

Here is my actual code within my IDE:

#include <functional>  #include <iostream>  #include <string>    std::string fizzbuzz(int n) {      auto fizz = [=](std::function<std::string(std::string)> f) {          return n % 3 == 0 ? [=](auto) { return "Fizz" + f(""); } : f;      };      auto buzz = [=](std::function<std::string(std::string)> f) {          return n % 5 == 0 ? [=](auto) { return "Buzz" + f(""); } : f;      };      auto id = [](auto s) { return s; };      return fizz(buzz(id))(std::to_string(n));  }    int main() {      for (int i = 1; i <= 100; i++)          std::cout << fizzbuzz(i) << '\n';      return 0;  }  

However, when I try to compile this Visual Studio is generating these compiler errors:

1>------ Build started: Project: Data Structure Samples, Configuration: Debug x64 ------  1>main.cpp  1>c:\...\main.cpp(62): error C2445: result type of conditional expression is ambiguous: types 'fizzbuzz::<lambda_9027e592dd51e6f4c5342b61ff8c23f0>::()::<lambda_2463463a8046fa170a40e78d59e9f461>' and 'std::function<std::string (std::string)>' can be converted to multiple common types  1>c:\...\main.cpp(62): note: could be 'fizzbuzz::<lambda_9027e592dd51e6f4c5342b61ff8c23f0>::()::<lambda_2463463a8046fa170a40e78d59e9f461>'  1>c:\...\main.cpp(62): note: or       'std::function<std::string (std::string)>'  1>c:\...\main.cpp(65): error C2445: result type of conditional expression is ambiguous: types 'fizzbuzz::<lambda_c18a2fee5ba13240be9b86f815911a7c>::()::<lambda_2774da13f447e3dfb583778d4ea6d5bd>' and 'std::function<std::string (std::string)>' can be converted to multiple common types  1>c:\...\main.cpp(65): note: could be 'fizzbuzz::<lambda_c18a2fee5ba13240be9b86f815911a7c>::()::<lambda_2774da13f447e3dfb583778d4ea6d5bd>'  1>c:\...\main.cpp(65): note: or       'std::function<std::string (std::string)>'  1>c:\...\main.cpp(68): error C2664: 'void fizzbuzz::<lambda_9027e592dd51e6f4c5342b61ff8c23f0>::operator ()(std::function<std::string (std::string)>) const': cannot convert argument 1 from 'void' to 'std::function<std::string (std::string)>'  1>c:\...\main.cpp(68): note: Expressions of type void cannot be converted to other types  1>Done building project "Data Structure Samples.vcxproj" -- FAILED.  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  

However, when I check this against CompilerExplorer found here, This code compiles with all three of the major compilers: GCC, Clang, and MSVC...

I know that C++17 supports both lambdas and std::function<T>, but is this type of implementation or usage specific to a newer version of the compiler? Meaning is this technic or usage only available with say C++20 and later? If so, what can be done to this code snippet so that it can be compiled under Visual Studio 2017 using C++17 that will provide the same semantics and behavior?

https://stackoverflow.com/questions/65895753/compiling-kevlin-henneys-lambda-fizzbuzz-with-visual-studio-2017-using-c17s January 26, 2021 at 12:05PM

没有评论:

发表评论