I might be completely stupid, but:
class Lexer {
private:
std::string::iterator start;
std::string::iterator current;
std::string source;
int line;
std::unordered_map<std::string, TokenType> keyword_table;
public:
Lexer(std::string program){
this->source = program;
this->start = program.begin();
this->current = program.begin();
this->line = 1;
std::cout << std::distance(this->start, this->source.begin()) << std::endl;
}
Token scan();
};
When I print the distance between this->start and this->source.begin(), i get some wild number. Depending on the string that I've fed into the class, I've gotten 40 and even 448. I'm very confused on why this is.
I've tried changing variable names, switching between const_iterator and iterator, and printing this->source, program, this->start, etc. I'm very confused about why this problem is occuring. Btw the language is c++. Am I using std::distance wrong?
This line of code:
Makes variable
sourcecontain copy of data of variableprogram. The fact that both variables contain the same string after that does not mean that their iterators would be related at all. Your code could be simplified as this:Which is invalid and leads to UB as you cannot calculate distance btw unrelated iterators as stated in documentation:
(Despite the fact that you store dangled iterator in
startandcurrentafter you ctor terminates which is a different issue)