- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
考虑:
#include <cassert>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
int main() {
auto range = boost::irange(1, 4);
assert(boost::find(range, 4) == end(range));
}
main.cpp:8:37: error: use of undeclared identifier 'end'
using boost::end;
它
works just fine ,这意味着
boost::end
可见:
boost::end
在表达式
end(range)
中?如果是故意的,其背后的理由是什么?
std::find_if
和不合格
end(vec)
.
最佳答案
历史背景
此 closed Boost ticket 中讨论了根本原因
With the following code, compiler will complain that no begin/end is found for "
range_2
" which is integer range. I guess that integer range is missing ADL compatibility ?
#include <vector>
#include <boost/range/iterator_range.hpp>
#include <boost/range/irange.hpp>
int main() {
std::vector<int> v;
auto range_1 = boost::make_iterator_range(v);
auto range_2 = boost::irange(0, 1);
begin(range_1); // found by ADL
end(range_1); // found by ADL
begin(range_2); // not found by ADL
end(range_2); // not found by ADL
return 0;
}
boost::begin()
andboost::end()
are not meant to be found by ADL. In fact, Boost.Range specifically takes precautions to preventboost::begin()
andboost::end()
from being found by ADL, by declaring them in thenamespace boost::range_adl_barrier
and then exporting them into thenamespace boost
from there. (This technique is called an "ADL barrier").In the case of your
range_1
, the reason unqualifiedbegin()
andend()
calls work is because ADL looks not only at the namespace a template was declared in, but the namespaces the template arguments were declared in as well. In this case, the type ofrange_1
isboost::iterator_range<std::vector<int>::iterator>
. The template argument is innamespace std
(on most implementations), so ADL findsstd::begin()
andstd::end()
(which, unlikeboost::begin()
andboost::end()
, do not use an ADL barrier to prevent being found by ADL).To get your code to compile, simply add "
using boost::begin;
" and "using boost::end;
", or explicitly qualify yourbegin()/end()
calls with "boost::
".
begin
和
end
有两个方面:
begin(x)
, 如果 x
有(可能是默认的!)模板参数,或在其实现中隐藏的基类,模板参数及其基类的关联命名空间也被 ADL 考虑。每个关联的命名空间都可能导致 begin
的许多重载和 end
在参数相关查找期间被拉入。 namespace std
, begin
和 end
函数模板不会为每个容器单独重载,或者以其他方式受限于所提供容器的签名。当另一个命名空间(例如 boost
)也提供类似的不受约束的函数模板时,重载决议将考虑两者相等匹配,并发生错误。 std::iterator
的迭代器。 ,以及通用和不受约束的函数模板
begin
和
end
.
#include <iostream>
#include <iterator>
namespace C {
template<class T, int N>
struct Container
{
T data[N];
using value_type = T;
struct Iterator : public std::iterator<std::forward_iterator_tag, T>
{
T* value;
Iterator(T* v) : value{v} {}
operator T*() { return value; }
auto& operator++() { ++value; return *this; }
};
auto begin() { return Iterator{data}; }
auto end() { return Iterator{data+N}; }
};
template<class Cont>
auto begin(Cont& c) -> decltype(c.begin()) { return c.begin(); }
template<class Cont>
auto end(Cont& c) -> decltype(c.end()) { return c.end(); }
} // C
begin
和
end
.
namespace R {
template<class It>
struct IteratorRange
{
It first, second;
auto begin() { return first; }
auto end() { return second; }
};
template<class It>
auto make_range(It first, It last)
-> IteratorRange<It>
{
return { first, last };
}
template<class Rng>
auto begin(Rng& rng) -> decltype(rng.begin()) { return rng.begin(); }
template<class Rng>
auto end(Rng& rng) -> decltype(rng.end()) { return rng.end(); }
} // R
begin
进行迭代时,麻烦就开始了。和
end
:
int main()
{
C::Container<int, 4> arr = {{ 1, 2, 3, 4 }};
auto rng = R::make_range(arr.begin(), arr.end());
for (auto it = begin(rng), e = end(rng); it != e; ++it)
std::cout << *it;
}
rng
将为 begin
找到 3 个重载和 end
:来自
namespace R
(因为
rng
住在那里),来自
namespace C
(因为
rng
模板参数
Container<int, 4>::Iterator
存在于那里),并且来自
namespace std
(因为迭代器派生自
std::iterator
)。
重载解析然后将所有 3 个重载视为相等匹配 这会导致硬错误。
boost::begin
解决了这个问题和
boost::end
在内部命名空间中并将它们拉入封闭的
boost
命名空间通过使用指令。另一种方式,也是 IMO 更直接的方式,是
ADL 保护类型 (不是函数),所以在这种情况下,
Container
和
IteratorRange
类模板。
Container
和
IteratorRange
- 在这种特殊情况下 - 足以让上述代码运行而不会出错,因为
std::begin
和
std::end
会被调用,因为
std::iterator
不受 ADL 保护。
这是非常令人惊讶和脆弱的 .例如。如果执行
C::Container::Iterator
不再源自
std::iterator
,代码将停止编译。因此最好使用合格的调用
R::begin
和
R::end
在从
namespace R
的任何范围内为了免受这种不正当的名称劫持。
std
作为关联的命名空间进行 ADL)。这在
N3257 中讨论过这导致 range-for 的语义变化。当前 range-for 首先查找成员函数
begin
和
end
,所以
std::begin
和
std::end
不会被考虑,无论 ADL 障碍和继承自
std::iterator
.
int main()
{
C::Container<int, 4> arr = {{ 1, 2, 3, 4 }};
auto rng = R::make_range(arr.begin(), arr.end());
for (auto e : rng)
std::cout << e;
}
关于c++ - 为什么 ADL 不适用于 Boost.Range?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503793/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!