0

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

Community
  • 1
  • 1
onqtam
  • 4,356
  • 2
  • 28
  • 50
  • What's your definition of "container" ? – M.M Mar 04 '16 at 03:22
  • I don't recall if SFINAE basics were included in C++98. If so, it should be possible to concoct something together, by hand, instead of using the canned C++1x language support templates. Dollars-to-doughnuts this is MS-Windows related. gcc on modern Linux has had excellent C++1x support for a long time. – Sam Varshavchik Mar 04 '16 at 03:22
  • @M.M not sure... maybe something with a ```begin()``` and ```end()``` and forward iterators? – onqtam Mar 04 '16 at 03:24
  • @SamVarshavchik even for gcc I want my library to work with ```-std=c++98``` – onqtam Mar 04 '16 at 03:25
  • 2
    Usually the only distinction of interest is raw array versus container. And that's easy to check for via specialization. – Cheers and hth. - Alf Mar 04 '16 at 03:27
  • 1
    Why do you want to make this determination? Your problem may have a different solution. – GManNickG Mar 04 '16 at 03:28
  • @GManNickG I want to provide a specialization of a template for containers that iterates over them - and the user will have to write specializations only for his types and not for the common containers – onqtam Mar 04 '16 at 03:30
  • Is there a reason you wouldn't just have the user pass begin/end iterators like standard algorithms, and if it's not a standard container, it's up to the user to provide iterators? – Jerry Coffin Mar 04 '16 at 03:42
  • @JerryCoffin I'm doing expression decomposition (like [here](https://github.com/martinmoene/lest/blob/master/include/lest/lest_decompose.hpp#L286)) and all I have is the types of the left and right side of an expression (and I'm writing something similar to the ```to_string``` that dude has already done for containers) – onqtam Mar 04 '16 at 03:46
  • @onqtam: That doesn't really answer the question. Is there any part of what you need/want to do that can't be done with iterators, or some other specific reason to deal directly with containers instead of using iterators? – Jerry Coffin Mar 04 '16 at 04:15
  • `std::enable_if` doesn't rely on C++11 features, so you can just implement it yourself for C++03. – Cheers and hth. - Alf Mar 04 '16 at 04:26
  • @JerryCoffin actually yes - iterators work fine for me - after having looked at the pretty printing of c++ stl containers question. But the user will not supply the iterators - I will detect if T has begin and end like [here](https://github.com/louisdx/cxx-prettyprint/blob/master/prettyprint98.hpp) – onqtam Mar 04 '16 at 04:29

2 Answers2

1

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

onqtam
  • 4,356
  • 2
  • 28
  • 50
0

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.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • so there is no way without listing them? no way to specialize on types with begin() and end()? this means boost containers (for example) won't work out of the box – onqtam Mar 04 '16 at 03:48
  • You can check for presence of a function in the template parameter and decide that any class that has `begin()`, `end()` is container. Not very precise. User classes, that require specializations may be recognized as containers. – Kirill Kobelev Mar 04 '16 at 03:53
  • well c++98 is mandatory in my case - thanks for the effort though – onqtam Mar 04 '16 at 04:07