PacktPublishing / Template-Metaprogramming-with-CPP

Template Metaprogramming with C++, published by Packt
MIT License
82 stars 25 forks source link

Example n303 won't compile using MSVC but works with Clang & GCC #3

Closed dwhaynes closed 1 year ago

dwhaynes commented 1 year ago
namespace n303
{
   template <typename T>
   T min(T a, T b)
   {
      return a < b ? a : b;
   }

   template <typename T, typename... Args>
   T min(T a, Args... args)
   {
      return min(a, min(args...));
   }
}

When I run this code like cout << min(1, 2, 3, 4) << endl;

I get the following errors

Build started... 1>------ Build started: Project: templates, Configuration: Debug x64 ------ 1>templates.cpp 1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.32919\include\utility(90,19): error C2064: term does not evaluate to a function taking 2 arguments 1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.32919\include\utility(90,19): message : the template instantiation context (the oldest one first) is 1>C:\Users\filepath\source\repos\templates\templates.cpp(56,13): message : see reference to function template instantiation 'T min<int,int,int,int>(T,int,int,int)' being compiled 1> with 1> [ 1> T=int 1> ] 1>C:\Users\filepath\source\repos\templates\templates.cpp(42,19): message : see reference to function template instantiation 'const _Ty &std::min<int,int>(const _Ty &,const _Ty &,_Pr) noexcept()' being compiled 1> with 1> [ 1> _Ty=int, 1> _Pr=int 1> ] 1>C:\Users\filepath\source\repos\templates\templates.cpp(42,12): error C2668: 'min': ambiguous call to overloaded function 1>C:\Users\filepath\source\repos\templates\templates.cpp(35,3): message : could be 'T min(T,T)' 1> with 1> [ 1> T=int 1> ] 1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.32919\include\utility(99,6): message : or 'const _Ty &std::min(const _Ty &,const _Ty &) noexcept()' 1> with 1> [ 1> _Ty=int, 1> T=int 1> ] 1>C:\Users\filepath\source\repos\templates\templates.cpp(42,12): message : while trying to match the argument list '(T, const _Ty)' 1> with 1> [ 1> T=int 1> ] 1> and 1> [ 1> _Ty=int 1> ] 1>Done building project "templates.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== ========== Build started at 10:44 PM and took 01.242 seconds ==========

dwhaynes commented 1 year ago

Issue was using namespace std

Apparently std::min is used somewhere in iostream