[英]Why doesn't sizeof… work with this alias template?
我想将可变参数函数限制为一定数量的输入 - 比方说,两个。 为此,这在我的环境中工作正常(VS2017,C ++ 17):
#include <type_traits>
template<typename... T>
auto f(T...) -> typename std::enable_if<sizeof...(T) == 2>::type {
// no-op
}
int main() {
// f(1); // should fail
f(1,2);
// f(1,2,3); // should fail
}
但是,如果我引入别名模板,它就不会。
#include <type_traits>
template<typename... T>
using two_params = typename std::enable_if<sizeof...(T) == 2>::type;
template<typename... T>
auto f(T...) -> two_params<T...> { // failed to specialize alias template
}
int main() {
// f(1); // should fail
f(1,2);
// f(1,2,3); // should fail
}
有趣的是,如果我将条件更改为1
或实际所需的输入数量,则替换成功。
// This works, except that it permits a single argument even when it shouldn't.
// Both conditions ||'d together seems to be needed in the general case.
template<typename... T>
using two_params = typename std::enable_if<sizeof...(T) == 1 || sizeof...(T) == 2>::type;
似乎f(1,2)
生成两个sizeof...(T)
。 到底发生了什么?
我看过一些参考文献:
来自微软的Jonathan Emmett刚刚确认这是一个编译器错误:
谢谢你的报告。 我可以确认这是一个带有别名模板和包扩展的编译器错误。 我们目前正在为别名专业化进行一系列主要修复工作,目前这项工作将包含在VS 2017 15.9版本中。 我也可以确认这个错误是作为返工的一部分修复的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.