简体   繁体   中英

What type of value should it be returned by getters that return size of vector?

What type of value should be returned by getters that return size of vector?

For example I have many getters in my project of the following type and I need the (int) cast to remove the compile warnings:

int getNumberOfBuildings() const { return (int)buildings.size(); }

The C++03 way:

std::vector<Building>::size_type getNumberOfBuildings() const
{ return buildings.size(); }

The C++11 way:

auto getNumberOfBuildings() const -> decltype(buildings.size())
{ return buildings.size(); }

The C++14 way:

auto getNumberOfBuildings() const
{ return buildings.size(); }

You should return a std::size_t . std::size_t is the return type of vector<T>::size() when you use the default allocator. vector<T>::size() returns vector<T>::size_type which is just a typedef for std::size_t when you use the default allocator (which you are most likely using).

std::vector provides a typedef, std::vector<>::size_type , that indicates which is the type used as the result of vector::size :

std::vector<TYPE>::size_type getNumberOfBuildings() const { return buildings.size(); }

This, however, is usually size_t , so you can go ahead and use the latter directly:

#include <cstddef>

std::size_t getNumberOfBuildings() const { return buildings.size(); }

The formal answer is indeed std::vector<Building>::size_type , as stated in other answers.

The real answer depends on whether you already have a type in your program that you use to represent the number of buildings. It could be int , it could be unsigned or it could be some typedef name TBuildingCount . This is the type you should use in this case. It might require a cast to suppress compiler warnings, but that's the way it usually is.

I don't see the rest of your code, so I can't say it for sure, but I'd guess that the fact that the buildings are stored in some vector somewhere (or in any other container) is just an implementation detail, which should not influence your choice of type for counting these buildings. There's no need to expose the existence of that containber (or rely on it) even in such indirect way as using its size_type to return the count.

In other words, if you already decided to use int for counting buildings, then the implementation in your question is the one you should stick with.

In contrast to many other answers, the return value of your function actually much more depends on your interface than what you use under the hood. Are you returning the size of a vector and want everybody to know it? Well, then go ahead and use std::vector::size_type . But are you actually returning some abstract counting variable, like the number of buildings, then I don't think a std::vector::size_type belongs into your interface, since that is just an implementation detail.

If it's a size of something that you return (but not neccessarily known to be a vector under the hood), I'd use good old std::size_t , but if it's a count of something, then an unsigned int is conceptually much more appropriate. Of course nobody guarantees that an unsigned int or std::size_t can hold the size of an arbitrary std::vector , but then again it's not an arbitrary std::vector , it's an array of buildings and you yourself should know if there can ever be more than std::numeric_limits<unsigned int>::max() buildings anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM