看起来std::tuple包含一个或多个引用在构造和赋值方面具有意外行为(尤其是复制/移动构造和复制/移动分配).它与两者的行为std::reference_wrapper(更改引用的对象)和具有成员引用变量的结构(赋值运算符已删除)不同.它允许方便的std::tiepython像多个返回值,但它也允许明显不正确的代码,如下所示(链接在这里):
#include <tuple>
int main()
{
std::tuple<int&> x{std::forward_as_tuple(9)}; // OK - doesn't seem like it should be
std::forward_as_tuple(5) = x; // OK - doesn't seem like it should be
// std::get<0>(std::forward_as_tuple(5)) = std::get<0>(x); // ERROR - and should be
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该标准似乎要求或强烈暗示20.4.2.2.9最新工作草案的副本(ish)分配部分中的此行为(Ti&将折叠为左值ref):
template <class... UTypes> tuple& operator=(const tuple<UTypes...>& u);9 要求:
sizeof...(Types) == sizeof...(UTypes)并且is_assignable<Ti&, const Ui&>::value对所有人都是如此i.10 效果:将u的每个元素分配给*this的相应元素.
11 返回:*this …
更新
我发布了一份工作草案,rebind作为问题的答案.虽然我没有太多运气找到一种通用的方法来防止static_assert破坏元功能.
基本上我想检查是否T<U, Args...>可以从其他类型构造模板化类型T<V, Args...>.其中T和Args...是在这两种类型相同.问题是,T<>可能有一个static_assert完全打破我的元功能.
以下是我正在尝试做的粗略总结.
template<typename T>
struct fake_alloc {
using value_type = T;
};
template<typename T, typename Alloc = fake_alloc<T>>
struct fake_cont {
using value_type = T;
// comment the line below out, and it compiles, how can I get it to compile without commenting this out???
static_assert(std::is_same<value_type, typename Alloc::value_type>::value, "must be the same type");
};
template<typename T, typename U, typename = …Run Code Online (Sandbox Code Playgroud)