我写了this answer。为什么这个代码在VC ++下编译时不能编译?+ 如何使代码可移植?
我的代码:
template<typename T>
inline T sum(T t){
return t;
}
template<typename T, typename... Ts>
inline auto sum(T t, Ts... ts)->decltype(t+sum(ts...)){
return t+sum(ts...);
}
#include<iostream>
int main(){
std::cout<<sum(2.5, 2)<<'\n' //works
<<sum(2, 2.5)<<'\n'; //works
std::cout<<sum(1u, 2.5, 3.f, '0')<<'\n';//doesn't work, don't know why
}
See it in online compiler under g++
See it in online compiler under MSVC++