This is my custom string class (xstring.hpp):
#include <vector>
#include <sstream>
namespace
{
using std::vector;
using std::istringstream;
template <class strT>
class xstring_base
{
private:
strT str;
public:
operator strT&() {return str;}
vector<strT>* tokenize();
// constructors
xstring_base<strT>(strT);
};
}
#include "xstring.cpp"
#include <string>
typedef xstring_base<std::string> xstring;
I’ve put the operator strT&() to mimic the Standard Library’s string behavior wherever needed, and this class works absolutely fine when I initialize it with a C-style string, even containing non-ASCII code, for example arabic, but std::getline complains that xstring is not supported.
How can I use getline to input from cin to this custom string class of mine?
(I use g++ on Kubuntu 11.10. Gives tens of lines of complain about template mixture mismatches…)
Thanks so much!
std::getlineis a function template that looks like this:When you call
std::getline(std::cin, x), you don’t provide template arguments. This means that the types will have to be deduced from the arguments.However, the type deduction algorithm does not take user-defined conversions into account. So your conversion operator is not used. If you wrote
std::getline<char, std::char_traits<char>, std::allocator<char>>(std::cin, x), no type deduction is needed, so the conversion operator would be considered.