为什么使用指令的范围会有所不同
Why does scope of using directive make a difference?
下面是两个简化的代码示例:第一个编译得很好,第二个发出编译错误(找不到使用左手运算符ByteVector的运算符<<…)
这两个示例之间的唯一区别是using指令的位置。
我不想知道它为什么失败(你没有足够的信息来回答这个问题),我只感兴趣的是它为什么会在我放置using
的地方产生任何的差异。
我本以为这两个例子都会有完全相同的行为。
编译无错误
ByteVector Test()
{
using Base::operator <<;
ByteVector foo;
int bar = 1;
foo << bar;
return foo;
}
编译时出错
using Base::operator <<;
ByteVector Test()
{
...same as above, without using
}
额外信息:
运算符<lt;使用的定义如下
template<typename T>
ByteVector& operator<<(ByteVector &, const T&){...};
我唯一想到的就是Visual Studio。如果是这种情况,您可能希望将using
指令放在相应的#include
之后。正如页面上的评论所说:
将using指令放在源代码文件的开头使用IntelliSense降低意外行为的可能性。
否则,应该不会有什么不同。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- GCC和Clang在与__builtin_constant_p相关的static_assert方面有所不同
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 为什么使用指令的范围会有所不同