5

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(); }

5 Answers 5

25

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(); }
8
  • I would rather stay with my int conversion rather than any of these :). They are all ugly except the last one but we are not there yet, c++14... Jul 6, 2013 at 20:42
  • 3
    @user2381422, But int causes trouble everywhere. In comparisons, you get warnings, you might get truncation, and you have to do pointless work in the callsite if you want the proper type.
    – chris
    Jul 6, 2013 at 20:45
  • 2
    @user2381422: Most of the time you are safe returning an std::size_t, but in theory that is not portable, since the standard does not specify std::size_t should be the same as std::vector<T>::size_type for any T. Also, with GCC 4.8.1 and Clang 3.3 you can use the last form if you compile with the -std=c++1y flag
    – Andy Prowl
    Jul 6, 2013 at 20:46
  • 1
    @user2381422, For whatever reason, I like using size_type over decltype, but they both give the same thing, so it doesn't matter too much.
    – chris
    Jul 6, 2013 at 20:49
  • 1
    @AndyProwl: actually I think the standard does say that std::allocator<T>::size_type is size_t, and it follows that std::vector<T>::size_type is size_t. What the standard doesn't say is that std::vector<T,Alloc>::size_type is size_t for any Alloc, so you're right that there can be vectors with other size types. Jul 7, 2013 at 2:45
5

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).

4
  • 4
    Technically, the return type is vector::size_type, which is a typedef for the allocator's size_type, which in turn is a typedef for size_t. If you have a custom allocator, it might be something different. Jul 6, 2013 at 20:40
  • What do you guys mean by default vs custom allocator? Jul 6, 2013 at 20:44
  • 1
    An allocator is an object that manages how the vector gets memory to store what you put in it. You can make your own custom allocators, but the default one (std::allocator) is good enough in most cases.
    – JKor
    Jul 6, 2013 at 20:45
  • +1 for the one most simple type that will never fail to represent the answer correctly. No matter, how many objects of whatever type you have, you will always be able to count them using size_t, simply because you also have to store them. Jul 30, 2013 at 13:02
3

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(); }
2
  • and then in my code I should also use size_t correct? for example: size_t temp = myObject.getNumberOfBuildings(); Jul 6, 2013 at 20:45
  • 1
    @user2381422 yes. If you're using C++11, you could use the auto keyword: auto temp = myObject.getNumberOfBuildings();.
    – mfontanini
    Jul 6, 2013 at 20:46
3

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.

1
  • I agree with this, but I can't resist quibbling with the last assertion that the questioner "should" stick with a C-style cast. There are alternatives that might be preferable, such as a static_cast (to aid certain kinds of code searching) or a boost::numeric_cast (to assert the precondition / class invariant that the current value must fit in an int). Jul 7, 2013 at 2:52
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.