【发布时间】:2011-11-29 23:01:37
【问题描述】:
假设我正在编写一些容器模板或其他东西。是时候专门为它专门std::swap了。作为一个好公民,我将通过执行以下操作来启用 ADL:
template <typename T>
void swap(my_template<T>& x, my_template<T>& y) {
using std::swap;
swap(x.something_that_is_a_T, y.something_that_is_a_T);
}
这非常整洁。直到我想添加一个异常规范。只要T 的交换是noexcept,我的swap 就是noexcept。所以,我会写这样的东西:
template <typename T>
void swap(my_template<T>& x, my_template<T>& y)
noexcept(noexcept(swap(std::declval<T>(), std::declval<T>())))
问题是,其中的swap 必须是ADL 发现的swap 或std::swap。我该如何处理?
【问题讨论】:
-
这看起来高度相关并包含各种信息代码sn-ps:gcc.gnu.org/bugzilla/show_bug.cgi?id=49107(只需在页面上搜索
swap) -
FWIW,如果您希望与
decltype的尾随返回类型具有相同的启用 ADL 的行为,也可能会出现此问题。 -
酷,不知道
declval。对于受我的欺骗问题影响的许多函数,这意味着我可以改用非尾随返回类型(尽管这与我的问题无关)。
标签: c++ c++11 argument-dependent-lookup noexcept