这是我连接元组的二元运算符:
template <class... Args1, class... Args2>
constexpr decltype(auto) operator+(const std::tuple<Args1...> &tup1,
const std::tuple<Args2...> &tup2) {
return std::tuple_cat(tup1, tup2);
}
它在两个元组的编译器(gcc,clang)上都能很好地工作:
template <class Arg1, class Arg2>
constexpr decltype(auto) concat_test(Arg1 &&arg1, Arg2 &&arg2) {
return arg1 + arg2;
}
但是当我尝试在折叠表达式中使用它时,如下所示:
template <class... Args>
constexpr decltype(auto) multiple_concat(Args &&... args) {
return (args + ...);
}
gcc 7.1.1编译它没有任何错误,不像clang 5.0,它产生错误输出:
error:调用函数'operator',它在模板定义中既不可见,也不是由参数依赖的查找返回(args ...);注意:在实例化函数模板特化'multiple_concat <std :: __ 1 :: tuple&,std :: __ 1 :: tuple&>'这里请求multiple_concat(tup1,tup2);注意:'operator'应该在调用站点constexpr decltype(auto)操作符之前声明(const std :: tuple&tup1,const std :: tuple&tup2)
这段代码是不正确的,究竟是什么铿锵谈论的?
2 回答
2018年8月:Xcode 9.0(大致相当于开源clang 4.0)仍然没有编译这段代码,而g正确地完成了这项工作 .
我知道's painful not to be able to use the shiny new template fold syntax, but here'是一个基于
if constexpr
的解决方法,这是我们可以使用的下一个最好的方法 .Clang愉快地编译了这段代码 .
显然,不合格的查找失败 . 这个用Clang 6编译: