LINUX.ORG.RU

#include <iostream>

template <typename org, int x, int y>
struct neighbors
{
	static const int count =
		(org::template cell<x-1, y-1>::alive ? 1 : 0) +
		(org::template cell<x-1, y  >::alive ? 1 : 0) +
		(org::template cell<x-1, y+1>::alive ? 1 : 0) +
		(org::template cell<x  , y-1>::alive ? 1 : 0) +
		(org::template cell<x  , y+1>::alive ? 1 : 0) +
		(org::template cell<x+1, y-1>::alive ? 1 : 0) +
		(org::template cell<x+1, y  >::alive ? 1 : 0) +
		(org::template cell<x+1, y+1>::alive ? 1 : 0);

	template <int x1, int y1>
	struct with
	{
		static const int dx = x - x1;
		static const int dy = y - y1;

		static const bool are =
			(dx || dy) &&
			(dx >= -1 && dx <= 1) &&
			(dy >= -1 && dy <= 1);
	};
};


template <typename org>
struct evolved
{
	template <int x, int y>
	struct cell
	{
		static const bool alive =
			(neighbors<org, x, y>::count == 3) ||
			(org::template cell<x, y>::alive && neighbors<org, x, y>::count == 2);
	};
};


template <typename org, int x, int y>
struct cell_printer
{
	static void print(std::ostream& out)
	{
		out << (org::template cell<x, y>::alive ? "()" : "--");
	}
};


template <typename org, int x1, int y1, int x2, int y2, int x = x1, int y = y1>
struct org_printer
{
	static void print(std::ostream& out)
	{
		cell_printer<org, x, y>::print(out);
		org_printer<org, x1, y1, x2, y2, x+1, y>::print(out);
	}
};


template <typename org, int x1, int y1, int x2, int y2, int y>
struct org_printer<org, x1, y1, x2, y2, x2, y>
{
	static void print(std::ostream& out)
	{
		cell_printer<org, x2, y>::print(out);
		out << std::endl;
		org_printer<org, x1, y1, x2, y2, x1, y+1>::print(out);
	}
};


template <typename org, int x1, int y1, int x2, int y2>
struct org_printer<org, x1, y1, x2, y2, x2, y2>
{
	static void print(std::ostream& out)
	{
		cell_printer<org, x2, y2>::print(out);
		out << std::endl;
	}
};


template <typename org, int x1, int y1, int x2, int y2, int steps, int n = 0>
struct life_printer
{
	static void print(std::ostream& out)
	{
		org_printer<org, x1, y1, x2, y2>::print(out);
		out << std::endl;
		life_printer<evolved<org>, x1, y1, x2, y2, steps, n+1>::print(out);
	}
};


template <typename org, int x1, int y1, int x2, int y2, int steps>
struct life_printer<org, x1, y1, x2, y2, steps, steps>
{
	static void print(std::ostream& out)
	{
	}
};


namespace glider
{
	template <int x, int y>
	struct cell { const static bool alive = false; };

	template <> struct cell<0, 0> { const static bool alive = true; };
	template <> struct cell<1, 0> { const static bool alive = true; };
	template <> struct cell<2, 0> { const static bool alive = true; };
	template <> struct cell<2, 1> { const static bool alive = true; };
	template <> struct cell<1, 2> { const static bool alive = true; };

	struct org
	{
		template <int x, int y>
		struct cell: glider::cell<x, y> {  };
	};
};

int main()
{
	life_printer<glider::org, -10, -10, +10, +10, 5>::print(std::cout);
	return 0;
}

anonymous
()
Ответ на: комментарий от anonymous

А (c) кто будет ставить? Это работа int19h!

anonymous
()
Ответ на: комментарий от anonymous

E:\users\Fedor\1>cl /GX /MD zzz.cpp /Fetest_vc.exe
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

zzz.cpp
zzz.cpp(7) : error C2903: 'cell' : symbol is neither a class template nor a function template
        zzz.cpp(27) : see reference to class template instantiation 'neighbors<org,x,y>' being compi
led
zzz.cpp(7) : error C2143: syntax error : missing ')' before '<'
        zzz.cpp(27) : see reference to class template instantiation 'neighbors<org,x,y>' being compi
led
zzz.cpp(7) : error C2039: 'alive' : is not a member of '`global namespace''
        zzz.cpp(27) : see reference to class template instantiation 'neighbors<org,x,y>' being compi
led
zzz.cpp(7) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
        zzz.cpp(27) : see reference to class template instantiation 'neighbors<org,x,y>' being compi
led
zzz.cpp(37) : error C2143: syntax error : missing ')' before '=='
        zzz.cpp(35) : see reference to class template instantiation 'evolved<org>::cell<`template-pa
rameter513',`template-parameter514'>' being compiled
        zzz.cpp(40) : see reference to class template instantiation 'evolved<org>' being compiled
zzz.cpp(37) : error C2059: syntax error : ')'
        zzz.cpp(35) : see reference to class template instantiation 'evolved<org>::cell<`template-pa
rameter513',`template-parameter514'>' being compiled
        zzz.cpp(40) : see reference to class template instantiation 'evolved<org>' being compiled
zzz.cpp(38) : fatal error C1903: unable to recover from previous error(s); stopping compilation

E:\users\Fedor\1>bcc32 zzz.cpp -etest_bcc.exe
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
zzz.cpp:
Assertion failed zzz.cpp(53) : ! isFormalParam(sym->symAddr.symPAddr) at c:\helena\bcc\indep\build.c
(384)

E:\users\Fedor\1>g++ zzz.cpp -o zzz.exe -s

E:\users\Fedor\1>g++ --version
g++.exe (GCC) 3.2.3 (mingw special 20030504-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


:))))))))))))))

anonymous
()
Ответ на: комментарий от anonymous

> Microsoft (R) 32-bit C/C++ Optimizing Compiler

Да уш, этой бедой токмо осиновые баклуши компилить, кто бы сомневался :(

bugmaker ★★★★☆
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.