Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to write "std::string" but not "std::getline()"?

Tags:

c++

include

std

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!

like image 339
bcf Avatar asked Jan 02 '14 03:01

bcf


People also ask

Why CIN Getline is not working in C++?

The getline() function in C++ is used to read a string or a line from the input stream. The getline() function does not ignore leading white space characters. So special care should be taken care of about using getline() after cin because cin ignores white space characters and leaves it in the stream as garbage.

Can you use Getline on a string?

getline (string) in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.

What can I use instead of Getline in C++?

Use the std::ws manipulator in the std::getline . Like: getline(cin >> ws, title); . This will eat potential leading whitespaces, including the newline.

What does getline () do in C++?

The C++ getline() is an in-built function defined in the <string. h> header file that allows accepting and reading single and multiple line strings from the input stream. In C++, the cin object also allows input from the user, but not multi-word or multi-line input.


1 Answers

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”.

like image 112
Cheers and hth. - Alf Avatar answered Sep 19 '22 14:09

Cheers and hth. - Alf