【发布时间】:2020-01-15 11:36:47
【问题描述】:
我是C++ 的新手,在C 有背景。我很难采用的一件事是经常使用范围运算符,例如std:: 好吧,我会通过将using namespace std 放在我的源代码开头来避免使用它,但是很多人不使用这种方法,因为他们认为这可能会在将来对他们不利。
另外,visual-studio 还显示范围运算符中的错误/警告消息,例如
cannot convert from 'std::vector<int,std::allocator<_Ty>> *' to 'std::shared_ptr<std::vector<int,std::allocator<_Ty>>>'
虽然上面的消息很冗长,但读起来很痛苦(?)。我认为如果是这种形式,阅读起来会很简单
cannot convert from 'vector<int,allocator<_Ty>> *' to 'shared_ptr<vector<int,allocator<_Ty>>>'
1) 为什么每个人都在使用std::,甚至是cout、cin、endl?为什么会有人将这些标签用于其他目的?
2) 他们是 Visual Studio 中的一种解决方法,不向我显示带有前缀 std:: 的错误/消息/语法高亮吗?
【问题讨论】:
-
1) "虽然上面的信息很冗长,但读起来很痛苦(?)。" 如何/为什么? 2) Why is “using namespace std;” considered bad practice? 3) “他们在 Visual Studio 中是一种解决方法,不向我显示带有前缀
std::的错误/消息/语法高亮吗?”用户定义的类型/函数呢?在命名空间中定义?他们不应该有那个限定词吗?那么,如果不显示命名空间,您将如何区分这些名称? -
只需将命名空间视为名称的一部分。
-
另外,问问自己如果你的代码中有一些东西,在任何命名空间之外,没有
std::的错误消息将意味着什么,命名为vector。
标签: c++ visual-studio namespaces readability name-lookup