Variadic模板在g ++下不起作用,但在MSVC ++下工作

时间:2014-09-14 11:28:04

标签: c++ c++11 g++ variadic-templates

我写了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++

0 个答案:

没有答案