lundi 8 août 2022

Can a C++ function return without explicit giving type instantiation / with empty initializer list

Assuming a function with complicated return type, can we simplify the return statement?(no need to specify detailed return type)

std::vector<std::map<int/*id*/, std::string/*data*/>> GetSomeData(int type) {
  if (type == DataType::Invalid) {
    return std::vector<std::map<int, std::string>>(); // this works
    return {}; // can we make this happen?
  }
}

I know there is another way doing that:

std::vector<std::map<int/*id*/, std::string/*data*/>> GetSomeData(int type) {
  std::vector<std::map<int, std::string>> ret;
  if (type == DataType::TypeA) {
    ....
    // filling ret here
  } else if (....) {
    ....
  }
  return ret;
}

But I'm still curious about this question.

Aucun commentaire:

Enregistrer un commentaire