为什么我需要写“std :: string”但不是“std :: getline()”? [英] Why do I need to write "std::string" but not "std::getline()"?

查看:286
本文介绍了为什么我需要写“std :: string”但不是“std :: getline()”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这段代码:

  #include< iostream> 
#include< string>

int main()
{
std :: string str;
std :: cout<< 输入字符串:\\\
;
getline(std :: cin,str);
}

为什么我必须使用 std :: string cin cout 但不是 getline() getline()不在标准库中吗?我实际上有点困惑为什么我不能写使用命名空间std; ,而不必 #include 任何东西在标准库中。提前感谢!

解决方案

重新启用魔法自动命名空间限定。



这是Andrew Koenig的 * 错误。



他考虑了为用户定义类型提供操作符的问题。并提出了根据其参数解析函数的想法。例如。使用命名空间 std 中定义的类型的参数,编译器将在命名空间 std 中查找(也)。



这叫做 Koenig查询 ADL 参数依赖查找的缩写。






使用命名空间std;



对于短的探索性程序,您不仅可以,你应该这样做。



但是,标准库定义了一些标识符,例如 distance 非常可能会与您使用的名称发生冲突。



因此,对于更严重的代码,请考虑是否采用其他避免冗长的方法。许多程序员甚至认为您不应在严重的代码中使用 using namespace std; 。一些挪威公司把这作为一个编码指南(我不同意这样绝对的观点,但你应该知道它在那里,并可能是多数人的意见)。



无论如何:



     不要使用命名空间std; distance ,因此在全局命名空间中



为什么使用

/ code>不允许访问。



许多编程语言,如Java和C#,Ada和Modula-2和UCSD Pascal一些,对单独编译的模块有语言支持,模块包括函数,变量,类型,常量,或许更多。



语言模块可以称为模块(Modula-2),包(Ada,Java),单元(Pascal),类(Eiffel)等。



C ++有一个更原始的单独编译系统,其中编译器本质上知道关于模块的。预处理器可以从headers中拖动文本,这提供了一种获取一致性声明的方式。



但是这一切,当前的原始文本包含系统有大量的问题,通过各种编译器的支持最明显可见的所谓的precompiled headers(不是C ++标准的一部分!)。



David Vandevoorde曾在一个 模块提议



很遗憾,它还没有为C ++ 11做好准备,但也许以后会出现。



*: href =http://stackoverflow.com/users/36565/david-rodriguez-dribeas> David Rodriguez 补充说,这不是Andrew的错。他只打算为运营商ADL,委员会扩展到所有功能。


Consider this bit of code:

#include <iostream>                                                   
#include <string>

int main()
{
  std::string str;
  std::cout << "Enter a string: \n";
  getline(std::cin, str);
}

Why must I use std:: for string, cin and cout, but not getline()? Is getline() not in the standard library? I'm actually somewhat confused why I can't just write using namespace std; and not have to #include anything in the standard library, too. Thanks in advance!

解决方案

Re the magic automatic namespace qualification.

It's Andrew Koenig's* fault.

He considered the problem of providing operators for user defined types. And came up with the idea of resolving the function depending on its arguments. E.g. with an argument of type defined in namespace std, the compiler looks (also) in namespace std.

That's called either Koenig lookup or ADL, short for Argument Dependent Lookup.


Re using namespace std;.

For short exploratory programs you not only can, but to save work you should, do this.

However, the standard library defines some identifiers such as distance that are very likely to collide with names you use.

Hence, for more serious code consider whether other means of avoiding verbosity, might be better. Many programmers even maintain that you should not use using namespace std; in serious code. Some Norwegian firms have this as a coding guideline (I don't agree with so absolute a view, but you should know that it's there, and may be a majority view).

At any rate:

    Never put a using namespace std; in the global namespace in a header

because that makes name collisions like distance very likely for some code that includes the header.


Why using doesn't make things accessible.

Many programming languages, such as Java and C# and Ada and Modula-2 and UCSD Pascal, just to name some, have language support for separately compiled modules, where a module consists of functions, variables, types, constants, maybe more.

Depending on the language a module may be called a "module" (Modula-2), a "package" (Ada, Java), a "unit" (Pascal), a "class" (Eiffel), so on.

C++ has a much more primitive system of separate compilation, where essentially the compiler proper knows nothing about modules. The preprocessor can drag in text from "headers", which provides a way to get consistent declarations of things. And to easily get those declarations.

But that's all, and the current primitive text inclusion system has a large number problems, most manifestly visible via various compilers' support for so called "precompiled headers" (not part of the C++ standard!).

David Vandevoorde has worked on a module proposal for C++.

Sadly, it wasn't ready for C++11, but maybe it will come later.

*: In a comment David Rodriguez adds that “it is not really Andrew's fault. He only intended ADL for operators, the committee extended that to all functions”.

这篇关于为什么我需要写“std :: string”但不是“std :: getline()”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆