What are the negative effects of using typedefs in an implementation file to shorten signatutures names?

I am doing my first real C ++ project, which is a simple CSV parser (at the very early stage right now), and I have the following in the 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 function next, I was very annoyed to constantly enter CsvReader::before the enumerations, because he made the code so detailed. Therefore, 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 cannot name anything else Stateor Actionin the file, are there any consequences for this? Are there any better solutions?

+4
3

:

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::
}

:

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

, .


, const char& c . , const , , , . const char c.

+6

, - State Action , - ?

- . , .

?

++ 11, using typedef:

using State = CsvReader::State;

. , .

+2

Are there any better solutions?

I would not be his best decision. In any case, as mentioned in the comments on the question, if you can use C ++ 14, you can also use the return type auto.

Therefore it is:

std::tuple<CsvReader::State, CsvReader::Action, bool>
CsvReader::next(CsvReader::State s, const char& c) {
    // do whatever you want
    return std::tuple<State, Action, bool>{};
}

Becomes as follows:

auto CsvReader::next(State s, const char& c) {
    // do whatever you want
    return std::tuple<State, Action, bool>{};
}

Note also that in the body of the function you do not need to explicitly specify Stateor Action.

+1
source

Source: https://habr.com/ru/post/1662582/


All Articles