I know that we can specify the size of streambuf as below in some function
boost::asio::streambuf bufferstrm(512);
but in class how can we do the same thing
class test{
public:
boost::asio::streambuf bufferstrm;
void func1(){
//statement
}
void func2(){
//statement
}
};
So my question is if we are having boost::asio::streambuf bufferstrm; declared in above class then how can we specify the size of bufferstrm so that it can be available to all the functions of the class.
I tried below code
class test{
public:
boost::asio::streambuf bufferstrm(1024); // specified the size
void func1(){
//statement
}
void func2(){
//statement
}
};
But its giving error as cant initialize at the declaration point.
You can use NSMI with the correct syntax in C++14 and up:
In fact, this is nothing but a shorthand for a constructor initializer list item, in c++03 style:
Subclass
You can also consider creating a derived type that adds the initialization.
UPDATE Subclass idea
It turns out not so simple to make a subclass actually work with API's consuming
streambufas all the API's perform type-deduction on the buffer argument. Type deduction doesn't consider the conversion-to-base, so it fails to recognizes classes derived fromstreambuf. Also, you need those overloads asstreambufis the only buffer type that is taken by reference.I can see two solutions:
basic_streambuf_ref<>basic_streambuf<myalloc>for an artificial allocator type just to add the initialization semantics.Conversion to
basic_streambuf_ref<>You'll have to call it explicitly, but that's perhaps not the worst:
Used as:
See it on Compiler Explorer
Hacked allocator
Which can then be used as
See it Compiler Explorer