Исходник:
//////////////////////////////////////////////////////////////////////////////////////////
//
// _container.h
//
//////////////////////////////////////////////////////////////////////////////////////////
#ifndef __CONSTRUCTOR__DATA_STRUCT__CONTAINER_H__
#define __CONSTRUCTOR__DATA_STRUCT__CONTAINER_H__
#pragma once
#include "..\_type_def.h"
namespace Constructor {
namespace DataStruct {
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
#pragma warning(push)
#pragma warning(disable : 4786)
namespace Container {
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
template< typename ElementType, typename Container >
struct base
{
typedef Container container;
typedef ElementType element_type;
element_type m;
void BaseFunc(void) { m = 10; }
void ConcFunc(void) { m = 20; }
void ImplFunc(void) { m = 30; }
void SomeFunc(void) { m = 40; }
};
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
template< typename Container >
struct impl : public Container
{
public:
typedef Container base;
typedef typename base::container container;
typedef typename base::element_type element_type;
void BaseFunc(void) { m = 50; }
void ImplFunc(void) { reinterpret_cast<base*>(this)->BaseFunc(); }
void ConcFunc(void) { reinterpret_cast<container*>(this)->BaseFunc(); }
};
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
} // End of namespace Container
//////////////////////////////////////////////////////////////////////////////////////////
template< typename ElementType >
struct container
: public Container::impl < Container::base< ElementType, container > >
{
void BaseFunc(void) { m = 60; }
//void ImplFunc(void) { m = 70; }
void SomeFunc(void) { m = 80; }
};
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
} // End of namespace DataStruct
} // End of namespace Constructor
#endif // __CONSTRUCTOR__DATA_STRUCT__CONTAINER_H__
Результат:
typedef Constructor::DataStruct::container<dword> container;
container* pContainer = new container();
pContainer->BaseFunc(); // pContainer->m = 60
pContainer->ImplFunc(); // pContainer->m = 10
pContainer->ConcFunc(); // pContainer->m = 60
Чем-то задним чувствую, что не все так радужно и красиво...
Есть соображения по возможным проблемам в поддержке и расширении библиотеки, написанной с применением подобной конструкции?
Мой компьютер прогоняет бесконечный цикл за 9 секунд, но, мне кажется, он мог бы сделать это быстрее...