jeudi 1 décembre 2016

What are the negative consequences of using typedefs in an implementation file to shorten type signatutures?

I'm doing my first real project in C++, which is a simple CSV parser (in the very early stages right now), and I have the following in a header file:

class CsvReader {
public:
  // Actions to commit on each iteration of the CSV parser
  enum Action { ADD_CHAR, ADD_FIELD, NONE };

  // The possible states for each cell of a CSV
  enum State { START, IN_FIELD, IN_QUOTED_FIELD, IN_QUOTED_QUOTE };

  // Create the reader from a file
  explicit CsvReader(File& f);

  // Get a row from the CSV
  std::vector<std::string> get_row();
private:
  State m_state;
  LineReader m_lr;
  std::tuple<State, Action, bool> next(State s, const char& c);
};

When I want to implement the next function, I found it really annoying to constantly have to type out CsvReader:: before the enums because it made the code so verbose. So instead of having something like this in the implementation

std::tuple<CsvReader::State, CsvReader::Action, bool> next(CsvReader::State s, const char& c) {
  // more usage of CsvReader::
}

I did

typedef CsvReader::State State;
typedef CsvReader::Action Action;
std::tuple<State, Action, bool> CsvReader::next(State s, const char& c) {
  // function signature is much shorter and don't need to use CsvReader::
}

Except for the fact that I can't call anything else State or Action in the file are there any consequences to doing this? Are there any better solutions?

Aucun commentaire:

Enregistrer un commentaire