mercredi 23 septembre 2015

c++ return type of function to be a function of it's input

I am learning c++ I am wondering if there is a way to define a template where the return type would actually be a function of the input of the function.

For example:

calling fun(1) would return me an int
calling fun(2) would return me a float

I guess this could be done using some kind of map?

1 <> int
2 <> float

The problem I was trying to solve is, for example, if I have an object called room I wanted to have a function called get_contents on which I would pass an enum to define the return type. For example:

std::vector<Table> = room.get_contents(Room::TABLE);
std::vector<Chair> = room.get_contents(Room::CHAIR);

The first question probably isn't the best solution to this problem, nevertheless I wanted to know if it possible. Also, what is the best pattern to do what I want?

Aucun commentaire:

Enregistrer un commentaire