搜索
首页后端开发C++C 模板元编程图灵完备吗?

Is C   Template Metaprogramming Turing-Complete?

C 模板图灵完备吗?

一个广泛流传的说法是 C 模板在编译时是图灵完备的。这意味着模板可用于表示和执行任何可计算函数。

计算示例

这是用 C 实现的图灵机的重要示例11 使用模板:

#include <iostream>

template<bool c typename a b>
struct Conditional {
    typedef A type;
};

template<typename a typename b>
struct Conditional<false a b> {
    typedef B type;
};

template<typename...>
struct ParameterPack;

template<bool c typename="void">
struct EnableIf { };

template<typename type>
struct EnableIf<true type> {
    typedef Type type;
};

template<typename t>
struct Identity {
    typedef T type;
};

// define a type list 
template<typename...>
struct TypeList;

template<typename t typename... tt>
struct TypeList<t tt...>  {
    typedef T type;
    typedef TypeList<tt...> tail;
};

template
struct TypeList> {

};

template<typename list>
struct GetSize;

template<typename... items>
struct GetSize<typelist>> {
    enum { value = sizeof...(Items) };
};

template<typename... t>
struct ConcatList;

template<typename... first typename... second tail>
struct ConcatList<typelist>, TypeList<second...>, Tail...> {
    typedef typename ConcatList<typelist second...>, 
                                Tail...>::type type;
};

template<typename t>
struct ConcatList<t> {
    typedef T type;
};

template<typename newitem typename list>
struct AppendItem;

template<typename newitem typename...items>
struct AppendItem<newitem typelist>> {
    typedef TypeList<items... newitem> type;
};

template<typename newitem typename list>
struct PrependItem;

template<typename newitem typename...items>
struct PrependItem<newitem typelist>> {
    typedef TypeList<newitem items...> type;
};

template<typename list int n typename="void">
struct GetItem {
    static_assert(N > 0, "index cannot be negative");
    static_assert(GetSize<list>::value > 0, "index too high");
    typedef typename GetItem<typename list::tail n-1>::type type;
};

template<typename list>
struct GetItem<list> {
    static_assert(GetSize<list>::value > 0, "index too high");
    typedef typename List::type type;
};

template<typename list template typename...> class Matcher, typename... Keys>
struct FindItem {
    static_assert(GetSize<list>::value > 0, "Could not match any item.");
    typedef typename List::type current_type;
    typedef typename Conditional<matcher keys...>::value, 
                                 Identity<current_type>, 
                                 FindItem<typename list::tail matcher keys...>>
        ::type::type type;
};

template<typename list int i typename newitem>
struct ReplaceItem {
    static_assert(I > 0, "index cannot be negative");
    static_assert(GetSize<list>::value > 0, "index too high");
    typedef typename PrependItem<typename list::type typename replaceitem list::tail i-1 newitem>::type>
        ::type type;
};

template<typename newitem typename type typename... t>
struct ReplaceItem<typelist t...>, 0, NewItem> {
    typedef TypeList<newitem t...> type;
};

enum Direction {
    Left = -1,
    Right = 1
};

template<typename oldstate typename input newstate output direction move>
struct Rule {
    typedef OldState old_state;
    typedef Input input;
    typedef NewState new_state;
    typedef Output output;
    static Direction const direction = Move;
};

template<typename a typename b>
struct IsSame {
    enum { value = false }; 
};

template<typename a>
struct IsSame<a a> {
    enum { value = true };
};

template<typename input typename state int position>
struct Configuration {
    typedef Input input;
    typedef State state;
    enum { position = Position };
};

template<int a int b>
struct Max {
    enum { value = A > B ? A : B };
};

template<int n>
struct State {
    enum { value = n };
    static char const * name;
};

template<int n>
char const* State<n>::name = "unnamed";

struct QAccept {
    enum { value = -1 };
    static char const* name;
};

struct QReject {
    enum { value = -2 };
    static char const* name; 
};

#define DEF_STATE(ID, NAME) \
    typedef State<id> NAME ; \
    NAME :: name = #NAME ;

template<int n>
struct Input {
    enum { value = n };
    static char const * name;

    template<int... i>
    struct Generate {
        typedef TypeList<input>...> type;
    };
};

template<int n>
char const* Input<n>::name = "unnamed";

typedef Input InputBlank;

#define DEF_INPUT(ID, NAME) \
    typedef Input<id> NAME ; \
    NAME :: name = #NAME ;

template<typename config typename transitions> 
struct Controller {
    typedef Config config;
    enum { position = config::position };

    typedef typename Conditional(GetSize<typename config::input>::value) 
            (position),
        AppendItem<inputblank typename config::input>,
        Identity<typename config::input>>::type::type input;
    typedef typename config::state state;

    typedef typename GetItem<input position>::type cell;

    template<typename item typename state cell>
    struct Matcher {
        typedef typename Item::old_state checking_state;
        typedef typename Item::input checking_input;
        enum { value = IsSame<state checking_state>::value && 
                       IsSame<cell checking_input>::value
        };
    };
    typedef typename FindItem<transitions matcher state cell>::type rule;

    typedef typename ReplaceItem<input position typename rule::output>::type new_input;
    typedef typename rule::new_state new_state;
    typedef Configuration<new_input new_state max rule::direction>::value> new_config;

    typedef Controller<new_config transitions> next_step;
    typedef typename next_step::end_config end_config;
    typedef typename next_step::end_input end_input;
    typedef typename next_step::end_state end_state;
    enum { end_position = next_step::position };
};

template<typename input typename state int position transitions>
struct Controller<configuration state position>, Transitions, 
                  typename EnableIf<issame qaccept>::value || 
                                    IsSame<state qreject>::value>::type> {
    typedef Configuration<input state position> config;
    enum { position = config::position };
    typedef typename Conditional(GetSize<typename config::input>::value) 
            (position),
        AppendItem<inputblank typename config::input>,
        Identity<typename config::input>>::type::type input;
    typedef typename config::state state;

    typedef config end_config;
    typedef input end_input;
    typedef state end_state;
    enum { end_position = position };
};

template<typename input typename transitions startstate>
struct TuringMachine {
    typedef Input input;
    typedef Transitions transitions;
    typedef StartState start_state;
</typename></typename></inputblank></typename></state></issame></configuration></typename></new_config></new_input></transitions></cell></state></typename></typename></inputblank></typename></typename></id></n></int></int...></int></id></n></int></int></int></typename></a></typename></typename></typename></newitem></typelist></typename></typename></list></typename></typename></current_type></matcher></list></typename></list></list></typename></typename></list></typename></newitem></newitem></typename></typename></items...></newitem></typename></typename></t></typename></typelist></second...></typelist></typename...></typename...></typelist></typename...></typename></tt...></t></typename></typename...></typename></true></typename></bool></typename...></false></typename></bool></iostream>

以上是C 模板元编程图灵完备吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在C中掌握多态性:深度潜水在C中掌握多态性:深度潜水May 14, 2025 am 12:13 AM

掌握C 中的多态性可以显着提高代码的灵活性和可维护性。 1)多态性允许不同类型的对象被视为同一基础类型的对象。 2)通过继承和虚拟函数实现运行时多态性。 3)多态性支持代码扩展而不修改现有类。 4)使用CRTP实现编译时多态性可提升性能。 5)智能指针有助于资源管理。 6)基类应有虚拟析构函数。 7)性能优化需先进行代码分析。

C Destructors vs垃圾收集器:有什么区别?C Destructors vs垃圾收集器:有什么区别?May 13, 2025 pm 03:25 PM

C DestructorSprovidePreciseControloverResourCemangement,whergarBageCollectorSautomateMoryManagementbutintroduceunPredicational.c Destructors:1)允许CustomCleanUpactionsWhenObextionsWhenObextSaredSaredEstRoyed,2)RorreasereSouresResiorSouresiorSourseResiorMeymemsmedwhenEbegtsGoOutofScop

C和XML:在项目中集成数据C和XML:在项目中集成数据May 10, 2025 am 12:18 AM

在C 项目中集成XML可以通过以下步骤实现:1)使用pugixml或TinyXML库解析和生成XML文件,2)选择DOM或SAX方法进行解析,3)处理嵌套节点和多级属性,4)使用调试技巧和最佳实践优化性能。

在C中使用XML:库和工具指南在C中使用XML:库和工具指南May 09, 2025 am 12:16 AM

在C 中使用XML是因为它提供了结构化数据的便捷方式,尤其在配置文件、数据存储和网络通信中不可或缺。1)选择合适的库,如TinyXML、pugixml、RapidXML,根据项目需求决定。2)了解XML解析和生成的两种方式:DOM适合频繁访问和修改,SAX适用于大文件或流数据。3)优化性能时,TinyXML适合小文件,pugixml在内存和速度上表现好,RapidXML处理大文件优异。

C#和C:探索不同的范例C#和C:探索不同的范例May 08, 2025 am 12:06 AM

C#和C 的主要区别在于内存管理、多态性实现和性能优化。1)C#使用垃圾回收器自动管理内存,C 则需要手动管理。2)C#通过接口和虚方法实现多态性,C 使用虚函数和纯虚函数。3)C#的性能优化依赖于结构体和并行编程,C 则通过内联函数和多线程实现。

C XML解析:技术和最佳实践C XML解析:技术和最佳实践May 07, 2025 am 12:06 AM

C 中解析XML数据可以使用DOM和SAX方法。1)DOM解析将XML加载到内存,适合小文件,但可能占用大量内存。2)SAX解析基于事件驱动,适用于大文件,但无法随机访问。选择合适的方法并优化代码可提高效率。

c在特定领域:探索其据点c在特定领域:探索其据点May 06, 2025 am 12:08 AM

C 在游戏开发、嵌入式系统、金融交易和科学计算等领域中的应用广泛,原因在于其高性能和灵活性。1)在游戏开发中,C 用于高效图形渲染和实时计算。2)嵌入式系统中,C 的内存管理和硬件控制能力使其成为首选。3)金融交易领域,C 的高性能满足实时计算需求。4)科学计算中,C 的高效算法实现和数据处理能力得到充分体现。

揭穿神话:C真的是一种死语吗?揭穿神话:C真的是一种死语吗?May 05, 2025 am 12:11 AM

C 没有死,反而在许多关键领域蓬勃发展:1)游戏开发,2)系统编程,3)高性能计算,4)浏览器和网络应用,C 依然是主流选择,展现了其强大的生命力和应用场景。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具