Reputation: 1
This is a beginner for C++ programming. (and also in English, sorry for my poor English... ToT)
I searched topics related to my question, but I couldn't find them. I hope that this article is not duplicated one.
I'd like to write some class wraps an ostream object to manipulate output for my purpose.
For my studying, I'm trying to design generalized output stream class so that I made a child class inherits the basic_ostream class template.
As you know, the basic_ostream class requires two template variables(In fact, only the first one is mandatory and another has the default value as char_traits<_CharT>).
But, I stuck at making a constructor for my class, to make the compiler deduce the template variables automatically from an argument of the constructor.
Here is a part of my class header:
#include <ostream>
using namespace std;
template <typename _CharT, typename _Traits = char_traits<_CharT>>
class testOStream : public basic_ostream<_CharT, _Traits> {
public:
using typename basic_ostream<_CharT,_Traits>::__ostream_type;
explicit testOStream(__ostream_type& in)
:__ostream_type (in.rdbuf()), nowStream(in){};
private:
__ostream_type &nowStream;
};
I can instantiate above class by explicit designation of template variables as follows.
testOStream<ostream::char_type,ostream::traits_type> test1(std::cout);
However, the following line omits a compile error which is that the template variable deduction has been failed. (with c++17 standard)
testOStream test2(std::cout);
Actually, there are no problems practically. However, I want to deduce the variables automatically. Is there any way to achieve my goal and is it possible to compile the above code with the c++11 standard? I am able to work around this problem by giving up using the basic_ostream class as the parent class. But I want to know a solution and a reason for this error for my studying.
I am looking for your kind answer and if there is a solved question for this problem on the Stack Overflow, I am very sorry for that.
Thank you!
Upvotes: 0
Views: 99
Reputation: 3047
in c++17 you can use class deduction guide
template<typename T>
testOStream(T&) -> testOStream<typename T::char_type, typename T::traits_type>;
but this will deduce not only for std::cout
but for any std::basic_ostream
Upvotes: 1
Reputation: 409196
As mentioned in a comment, class template deduction exists in C++17. But without any deduction guides then (just like for functions) it has to be a direct deduction, not indirect as is the case with std::cout
(or any std::basic_ostream
derived object).
If you had a constructor taking a _CharT
(plain, pointer or reference) argument then it would be possible to deduce the template type, but not now.
Upvotes: 0