【发布时间】:2018-03-20 08:53:52
【问题描述】:
运行时:
template <typename T>
struct CodeByType
{
static const int32_t Value = 7;
};
template <>
struct CodeByType<int>
{
static const int32_t Value = 1;
};
template <typename Arg, typename... Args>
int32_t Sum()
{
// The compiler complains on this line
return Sum<Arg>() + Sum<Args...>();
}
template <typename Arg>
int32_t Sum()
{
return CodeByType<Arg>::Value;
}
int main()
{
auto sum = Sum<int, char, double>();
}
我明白了:
错误 C2668 'Sum':对重载函数的模糊调用
谁能解释一下为什么以及如何克服它?
这看起来与下面的代码非常相似,它确实可以编译,所以我想这与Sum 不接受任何实际参数有关。
template <typename T>
T adder(T first) {
return first;
}
template<typename T, typename... Args>
T adder(T first, Args... rest) {
return first + adder(rest...);
}
int main()
{
auto sum = adder(1, 7);
}
【问题讨论】:
标签: c++ c++11 variadic-templates