onqtam
onqtam

Reputation: 4528

Determine if a type is a container at compile time in c++98

Is there a way to determine if a type is some sort of a container (probably with begin() and end() and forward iterators) in c++98? I saw this but it uses decltype from c++11.

Also making specializations for every container (list, vector, etc) is not that appealing - perhaps something more general?

Probably a duplicate... I'd imagine this used to be a common question

EDIT:

I think this is almost what I need and I should remove tuple stuff from it and replace the use of std::enable_if with c++98 code. What should I do with this question now? keep it open? or? If someone does that work for me I would be happy to accept his answer :D

Upvotes: 0

Views: 135

Answers (2)

onqtam
onqtam

Reputation: 4528

I found this - cxx-prettyprint and will adapt it to my needs.

Upvotes: 1

Kirill Kobelev
Kirill Kobelev

Reputation: 10557

You can write specializations for all common containers that you want to support and fail compilation in the generic (non specialized) template.

There are not that many containers in the STL. Just about 15.

In this case your specializations will be picked up for common containers just by the regular mechanism and users will still be able use their own containers provided that they write required specializations.

There is other point about C++98/C++11. Compilers have not implemented all C++11 features in one step. For example constexpr was implemented by Microsoft only in VisualStudio 2015. If you try to run your code on a random compiler expect to see that some C++11 features are there, some features not.

It might be not a big mistake to use some of the features of C++11 provided that you know they were implemented early enough in the compiler that you are interested in.

Upvotes: 0

Related Questions