вторник, 1 сентября 2015 г.

Специализация одной функции в шаблоне класса

template <class T>
class A {
public:
    void foo() { cout << "foo1\n";}
    void bar() { cout << "bar1\n";}
   
};

template<>
void A<int>::foo() {cout << "foo2\n";}

среда, 19 августа 2015 г.

среда, 5 августа 2015 г.

Шаблон с переменным количеством параметров

#include <iostream>
using namespace std;

template <class T>
void print(T t) {
    cout << t;   
}

template <class T,class... Args>
void print(T t,Args... args) {
    cout << t << " ";
    print(args...);
   
}

int main() {
   
    print(5,"333",5.2);
    return 0;
}

c++

Обзор с++11 от Страуструпа: ссыль

generate(vec.begin(),vec.end(),[](){srand(clock());return rand()%10;}); remove_copy_if(vec.begin(),vec.end(),back_inserter(res),[](int i){return i>2;}); copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));
copy(res.begin(),res.end(),ostream_iterator<int>(cout," "));

среда, 13 мая 2015 г.

Вложенность произвольных контейнеров

#include <iostream>
#include <vector>
#include <list>
#include <deque>
using namespace std;

template <class T>
struct inCount{
    enum {result = 0};  
};

template<class T,template <class TT,class alloc = allocator<TT> > class CON>
struct inCount<CON<T> > {
    enum {result = inCount<T>::result +1};
};

template <class T>
int foo(T const & t) {
    return inCount<T>::result;
}

int main() {
    // your code goes here
    vector< vector< list<vector <list<deque<int> > > > > > v;
  
    cout << foo(v);
    return 0;
}

Узнать вложенность вектора

#include <iostream>
#include <vector>
using namespace std;

template <class T>
struct inCount{
    enum {result = 0};   
};

template<class T>
struct inCount<vector<T> > {
    enum {result = inCount<T>::result +1};
};

template <class T>
int foo(T const & t) {
    return inCount<T>::result;
}

int main() {
    // your code goes here
    vector< vector< vector<int> > > v;
   
    cout << foo(v);
    return 0;
}

вторник, 24 марта 2015 г.

Проверить, является ли тип абстрактным

#include <iostream>
using namespace std;

template <class T>
class is_abstr {

typedef char yes[1];
typedef char no[2];

template <class C> static no& test(C (*)[1]);
template <class> static yes& test(...);
   
public:   
    static const bool value = sizeof(test<T>(0 )) == sizeof(yes);

};

class A {
public:
    virtual int foo() = 0;
   
};

class B : public A {
public:
    int foo() {cout << "foo";}

};


int main() {
    cout.setf(ios::boolalpha);
    cout << is_abstr<B>::value;
    return 0;
}