basic_istream problem to create cin and wcin template

I am trying to code OpenCin function that will open cin ili cwin in the function whether we deal with chars or w_chars.

template <class T>
basic_istream<T, char_traits<T> > *is;
template <class T>
inline std::basic_istream<char,char_traits<char> > * cin_assign(char dummy){
return &cin;
}
template<class T>
inline std::basic_istream<wchar_t,char_traits<wchar_t> > * cin_assign(wchar_t dummy) {
return &wcin;
}
template<class T>
void OpenCin(T dummy) {
is = cin_assign(T dummy);
}

int man ()
{
char cTest = 'a';
OpenCin(cTest);

return 0;

}

The error on Linux (with c11) is:
testcin.cpp:15:5: error: missing template arguments before ‘=’ token
is = cin_assign(T dummy);
^
testcin.cpp:15:20: error: expected primary-expression before ‘dummy’
is = cin_assign(T dummy);
^~~~~

What I have done wrong?
Last edited on
std::cin and std::wcin are objects of two different types.

Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

constexpr std::istream& get_cin(char) { return std::cin ; }
constexpr std::wistream& get_cin(wchar_t) { return std::wcin ; }

constexpr std::ostream& get_cout(char) { return std::cout ; }
constexpr std::wostream& get_cout(wchar_t) { return std::wcout ; }

template < typename CHAR_TYPE > constexpr auto& get_cin() { return get_cin( CHAR_TYPE{} ) ; }
template < typename CHAR_TYPE > constexpr auto& get_cout() { return get_cout( CHAR_TYPE{} ) ; }

template < typename CHAR_TYPE >
std::basic_string<CHAR_TYPE> test_it( const CHAR_TYPE* cstr )
{
    get_cout<CHAR_TYPE>() << cstr ; // option one: template argument

    std::basic_string<CHAR_TYPE> str ;
    std::getline( get_cin( CHAR_TYPE{} ), str ) ; // option two: function argument
    return str ;
}

int main()
{
    std::cout << test_it( "enter a narrow character string: " ) << '\n' ;
    std::wcout << test_it( L"enter a wide character string: " ) << L'\n' ;
}
May I ask what is the meaning CHAR_TYPE{} ? I mean {}....
CHAR_TYPE is the character type. CHAR_TYPE{} is an anonymous temporary value initialised object of type CHAR_TYPE
http://en.cppreference.com/w/cpp/language/value_initialization

In this case, value initialisation is zero initialisation (initialised with a literal 0).
We could have written this instead with equivalent effect:
1
2
template < typename CHAR_TYPE > constexpr auto& get_cin() { return get_cin( CHAR_TYPE(0) ) ; }
template < typename CHAR_TYPE > constexpr auto& get_cout() { return get_cout( CHAR_TYPE(0) ) ; }
Topic archived. No new replies allowed.