【发布时间】:2018-01-16 02:31:08
【问题描述】:
这是我连接元组的二元运算符:
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,它会产生错误输出:
错误:调用函数“operator+”在模板定义中既不可见,也不通过参数相关查找找到
return (args + ...);
注意:在此处请求的函数模板特化 'multiple_concat ' 的实例化中
multiple_concat(tup1, tup2);
注意:'operator+' 应该在调用站点之前声明
constexpr decltype(auto) 运算符+(const std::tuple &tup1, const std::tuple &tup2)
这段代码格式不正确,clang 到底在说什么?
【问题讨论】:
-
clang 4.0.1 编译它,clang 6.0 也是如此。这很可能是编译器错误。
-
@Rakete1111 my clang 4.0.1 (tags/RELEASE_401/final) 没有编译这个,还有 clang 6.0 Example
-
我使用的是 xcode clang 9.0.0(大致相当于开源的 clang 4.0),它也无法编译。我自己在不同的环境中遇到了这个问题,这导致了我这篇文章。
-
这能回答你的问题吗? fold expression and function name lookup
标签: c++ templates variadic-templates c++17