Specialized functions in stl container types

If I have a type T, then what is a useful way to check it at compile time to see if it is an STL style container (for an arbitrary value type) or not?
(Assumption: pointers, links, etc. Already divided)

Starting code:

template<class T> // (1)
void f(T&) {} 

template<class T> // (2)
void f(std::vector<T>&) {} 

void test() 
{
    int a;
    std::vector<int> b;
    f(a);
    f(b);
}

Now this works fine, but what if I want to generalize the container (i.e. not define (3), (4), ... explicitly)?

Using SFINAE and type lists will slightly reduce the code, but is there a better way?
Or is there an idiom for concept-based specialization?
Or could I somehow use SFINAE to selectively include only the desired specializations?

, - , T .


MSalters:

template<class T>
void f(T&, ...) {
    std::cout << "flat" << std::endl; 
}

template<class Cont>
void f(Cont& c, typename Cont::iterator begin = Cont().begin(),
                typename Cont::iterator end   = Cont().end()) {
    std::cout << "container" << std::endl; 
}

( , f )

+3
3

STL typedef iterator, 2 begin() end() . . , STL. - ( )

template<typename CONTAINER>
void f(CONTAINER& c,
       typename CONTAINER::iterator begin = c.begin(),
       typename CONTAINER::iterator end = c.end())
{ }
+2

, , . , "STL". , . , ( ) std:: map, AVL R-B, , std:: map?

- hash_map, Boost, TR1, , ++ 0x. , "STL", , STL, - , , , , -.

, , , .

+2

www.cplusplus.com, , STL, :

  • operator=
  • size.

, T.

0
source

Source: https://habr.com/ru/post/1725682/


All Articles