gcc 未给出的 Clang 错误"attempted to construct a reference element in a tuple with an rvalue"
Clang error "attempted to construct a reference element in a tuple with an rvalue" not given by gcc
我已经编写了以下(相对)简单的std::tuple
zip函数实现(类似于Python的zip
),具有完美的转发:
template <size_t I, size_t N>
struct tuple_zip_helper {
template <typename... Tuples>
constexpr auto operator()(Tuples&&... tuples) const {
return tuple_cat(
make_tuple( forward_as_tuple(get<I>(forward<Tuples>(tuples))...) ),
tuple_zip_helper<I+1, N>()(forward<Tuples>(tuples)...)
);
}
};
template <size_t N>
struct tuple_zip_helper<N, N> {
template <typename... Tuples>
constexpr auto operator()(Tuples&&...) const {
return forward_as_tuple();
}
};
namespace std {
// Extend min to handle single argument case, for generality
template <typename T>
constexpr decltype(auto) min(T&& val) { return forward<T>(val); }
}
template <typename... Tuples>
auto tuple_zip(Tuples&&... tuples) {
static constexpr size_t min_size = min(tuple_size<decay_t<Tuples>>::value...);
return tuple_zip_helper<0, min_size>()( forward<Tuples>(tuples)... );
}
这似乎适用于两个或更多元组,即使在混合左值和右值时,甚至当我使用BlabberMouth
类检查虚假副本和移动时:
template <typename Tuple>
void f(Tuple&& tup) {
cout << get<0>(get<0>(tup)).data << endl;
}
struct Blabbermouth {
Blabbermouth(string const& str) : data(str) { }
Blabbermouth(Blabbermouth const& other) : data(other.data) { cout << data << " copied" << endl; }
Blabbermouth(Blabbermouth&& other) : data(move(other.data)) { cout << data << " moved" << endl; }
string data;
};
int main(int argc, char** argv) {
Blabbermouth x("hello ");
// prints "hello"
f(tuple_zip(
forward_as_tuple(x, 2),
forward_as_tuple(Blabbermouth("world"), 3)
));
}
当我只给它一个tuple
,而不混合左值和右值时,它也很好(clang-3.9,clang的早期版本也会阻塞它):
f(tuple_zip( forward_as_tuple(Blabbermouth("world"), 3) )); // prints "world"
然而,当我混合左值和右值时,和只给出一个元组,clang
会对noexecpt
规范中的某些内容感到奇怪(但gcc很好,甚至可以正确运行):
auto x = BlabberMouth("hello");
f(tuple_zip( forward_as_tuple(x, 3) )); // clang freaks out, gcc okay
实时演示
我做错了什么(如果有的话)?gcc应该抱怨,还是clang不应该抱怨?我的代码是否有任何我只是"幸运"的悬挂引用,这就是clang反对的原因?我应该采取不同的做法吗?如果clang是这里的错误,有人能提出一个解决办法吗?(和/或将我链接到错误报告?)
更新
@Oktalist提供了一个更简单的例子来说明同样的问题:
struct foo {};
int main(int argc, char** argv)
{
foo f;
std::tuple<foo&> t(f);
std::tuple_cat(std::make_tuple(t), std::make_tuple());
}
(我也考虑过让我的例子变得更简单,但我不确定我所做的是否与此完全类似,主要是因为我不完全理解完美转发如何与auto
/decltype(auto)
返回值、返回值优化(RVO)、std::get
和std::make_tuple
相互作用,所以我想确保我没有做其他愚蠢的事情。)
该错误是由对tuple_cat
的调用引起的(尽管不完全在其中;它似乎在libc++的tuple
的构造函数和SFINAE条件的拜占庭迷宫中有些混乱),因此解决方法是避免使用它。
无论如何,做递归tuple_cat
s是没有意义的;一个包扩展(或两个)就可以了。
template<size_t I, typename... Tuples>
constexpr auto tuple_zip_one(Tuples&&... tuples) {
return forward_as_tuple(get<I>(forward<Tuples>(tuples))...);
}
template<size_t...Is, typename... Tuples>
constexpr auto tuple_zip_helper(index_sequence<Is...>, Tuples&&... tuples) {
return make_tuple(tuple_zip_one<Is>(forward<Tuples>(tuples)...)...);
}
template <typename... Tuples>
auto tuple_zip(Tuples&&... tuples) {
static constexpr size_t min_size = min({tuple_size<decay_t<Tuples>>::value...});
return tuple_zip_helper( make_index_sequence<min_size>(), forward<Tuples>(tuples)... );
}
我随意删除了导致min
过载的UB,并简单地使用标准的initializer_list
版本。
演示。
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 不断"Attempting to reference a deleted function"
- std::iterator::reference 必须是引用吗?
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- C++返回类型 T(&)[] 与使用 reference = T(&)[] 作为返回类型
- const auto & 和 auto & if reference 对象之间的区别是 const
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 以前的'namespace reference { }'声明
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- gcc 未给出的 Clang 错误"attempted to construct a reference element in a tuple with an rvalue"
- 如果 x 是对 unique_ptr 的引用,则 *x 是"reference to element pointed to" ?