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中文网其他相关文章!

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C#和C 在面向对象编程(OOP)中的实现方式和特性上有显着差异。 1)C#的类定义和语法更为简洁,支持如LINQ等高级特性。 2)C 提供更细粒度的控制,适用于系统编程和高性能需求。两者各有优势,选择应基于具体应用场景。

从XML转换到C 并进行数据操作可以通过以下步骤实现:1)使用tinyxml2库解析XML文件,2)将数据映射到C 的数据结构中,3)使用C 标准库如std::vector进行数据操作。通过这些步骤,可以高效地处理和操作从XML转换过来的数据。

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。

C 在现代编程中仍然具有重要相关性。1)高性能和硬件直接操作能力使其在游戏开发、嵌入式系统和高性能计算等领域占据首选地位。2)丰富的编程范式和现代特性如智能指针和模板编程增强了其灵活性和效率,尽管学习曲线陡峭,但其强大功能使其在今天的编程生态中依然重要。

C 学习者和开发者可以从StackOverflow、Reddit的r/cpp社区、Coursera和edX的课程、GitHub上的开源项目、专业咨询服务以及CppCon等会议中获得资源和支持。1.StackOverflow提供技术问题的解答;2.Reddit的r/cpp社区分享最新资讯;3.Coursera和edX提供正式的C 课程;4.GitHub上的开源项目如LLVM和Boost提升技能;5.专业咨询服务如JetBrains和Perforce提供技术支持;6.CppCon等会议有助于职业

C#适合需要高开发效率和跨平台支持的项目,而C 适用于需要高性能和底层控制的应用。1)C#简化开发,提供垃圾回收和丰富类库,适合企业级应用。2)C 允许直接内存操作,适用于游戏开发和高性能计算。

C 持续使用的理由包括其高性能、广泛应用和不断演进的特性。1)高效性能:通过直接操作内存和硬件,C 在系统编程和高性能计算中表现出色。2)广泛应用:在游戏开发、嵌入式系统等领域大放异彩。3)不断演进:自1983年发布以来,C 持续增加新特性,保持其竞争力。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

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

Dreamweaver Mac版
视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。