Hardik
Hardik

Reputation: 185

Template default argument missing with basic_istream class

I was checking the implementation of the basic_istream class. I found the implementation at https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/api/a00113_source.html#l00095. Let me add a snippet.

enter image description here

I have to pass two template arguments when creating an object of the basic_istream class since the default argument is not declared in the basic_istream class declaration.

I can still build an object with just one template argument, as shown in the example below.

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    basic_stringbuf<char>string("Basic ios",ios_base::in| ios_base::out);
    basic_istream<char> bis(&string);
    
    return 0;
}

Can somebody clarify how it works for me?

Upvotes: 0

Views: 79

Answers (1)

Alan
Alan

Reputation: 1

tldr; CTAD from c++17 can be used to deduce the two template parameters.


I have to pass two template arguments when creating an object of the basic_istream class since the default argument is not declared in the basic_istream class declaration.

No, that is a wrong assumption. std::baisc_stream has a constructor that can deduce the template arguments from the passed constructor argument. The constructor is:

explicit basic_istream( std::basic_streambuf<CharT, Traits>* sb );   (1) 

This constructor will be used in your example, to deduce the two template parameters. See Class template argument deduction.

This is the reason why your code will compile in c++17 but not in c++14 or earlier. In particular, because CTAD is available from C++17 onwards.

Upvotes: 0

Related Questions