2

I am starting learning C++ a little while ago. Came to know about header files and preprocessor statements.

I know that std::cin and std::cout are the objects/Function is declared in standard library IOSTREAM. But when taking input as a string and to read the whole line. We have to getline() function or at least the instructor is using it in the video.

Now I have checked on the internet and most of the sites are showing that getline() is defined under STRING file/Library. But the thing is my program is working perfectly fine even without including the string file. So what I am missing.? or doing something wrong. and if you can please also explain how getline function is working. and also please what's the actual difference between using namespace std, and using std:: Thank you

#include <iostream> 

int main() 
{ 
    std::string str; 
  
    std::cout << "Please enter your name: \n"; 
    getline(std::cin, str); 
    std::cout << "Hello, " << str 
         << " welcome to GfG !\n"; 
  
    return 0; 
} 
Pete Becker
  • 74,985
  • 8
  • 76
  • 165

1 Answers1

1

You have to include the header <string> to use the function std::getline. It is implementation defined whether the header <iostream> includes the header <string>.

In this call using unqualified name getline

getline(std::cin, str);

the compiler applies the argument dependent lookup ADL (the first argument std::cin is defined in the namespace std::) and finds the corresponding name std::getline in the namespace std::.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • So you are saying string header implementation is defined under IOSTREAM?. – Ashish Agarwal Sep 04 '20 at 12:16
  • But isn't the namespace is IOSTREAM in this case? – Ashish Agarwal Sep 04 '20 at 12:18
  • @AshishAgarwal There is no such a namespace like iostream. It is a name of a header and of a corresponding class There is the standard namespace std. – Vlad from Moscow Sep 04 '20 at 12:20
  • 1
    @AshishAgarwal The header iostream may include the header string. It is an implementation defined detail. Other compilers can issue an error that the header string is not included that is that the name getline is not declared. – Vlad from Moscow Sep 04 '20 at 12:22
  • Ahha.! Got you. SO this is dependant upon the compiler. Can I know if we are using functions like cin,cout, or getline and each has their corresponding files from which are coming from, then what the use of namespace? – Ashish Agarwal Sep 04 '20 at 12:27
  • @AshishAgarwal Standard declaration are place in the namepsace std. – Vlad from Moscow Sep 04 '20 at 12:28
  • 1
    @AshishAgarwal • Learning which `std` identifiers reside in what standard C++ header files is part of the C++ learning curve. Resources like [CppReference](https://en.cppreference.com/w/cpp/string/basic_string/getline) are invaluable for helping find things. – Eljay Sep 04 '20 at 12:37
  • It is **not** implementation defined whether `` includes ``. It is specific to the implementation; a header file is allowed to define names that aren't required by the standard to be in that header, but the implementation is not required to document those details. Implementation-defined means that the implementation must document its behavior. – Pete Becker Sep 04 '20 at 13:17
  • @PeteBecker "Specific to the implementation" means implementation-defined. Headers are themselves documentations. You can look through headers. – Vlad from Moscow Sep 04 '20 at 15:14
  • That's not the point. The C++ standard says that some things are implementation defined, and it says that, for a conforming implementation, things that are implementation define must be documented. The fact that you can hunt through headers and perhaps figure out what the behavior of something is does not make that behavior implementation defined. It's implementation defined if the standard says that it is. – Pete Becker Sep 04 '20 at 16:57