pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 C++ 中方便地聲明編譯時字符串

Conveniently Declaring Compile-Time Strings in C++(在 C++ 中方便地聲明編譯時字符串)
本文介紹了在 C++ 中方便地聲明編譯時字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

能夠在 C++ 編譯時創建和操作字符串有幾個有用的應用.盡管可以在 C++ 中創建編譯時字符串,但該過程非常繁瑣,因為該字符串需要聲明為可變字符序列,例如

Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, as the string needs to be declared as a variadic sequence of characters, e.g.

using str = sequence<'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'>;

字符串連接、子字符串提取等操作可以輕松實現為對字符序列的操作.是否可以更方便地聲明編譯時字符串?如果沒有,是否有工作中的提案可以方便地聲明編譯時字符串?

Operations such as string concatenation, substring extraction, and many others, can easily be implemented as operations on sequences of characters. Is it possible to declare compile-time strings more conveniently? If not, is there a proposal in the works that would allow for convenient declaration of compile-time strings?

理想情況下,我們希望能夠如下聲明編譯時字符串:

Ideally, we would like to be able to declare compile-time strings as follows:

// Approach 1
using str1 = sequence<"Hello, world!">;

或者,使用用戶定義的文字,

or, using user-defined literals,

// Approach 2
constexpr auto str2 = "Hello, world!"_s;

其中 decltype(str2) 將有一個 constexpr 構造函數.可以實現方法 1 的更混亂版本,利用您可以執行以下操作的事實:

where decltype(str2) would have a constexpr constructor. A messier version of approach 1 is possible to implement, taking advantage of the fact that you can do the following:

template <unsigned Size, const char Array[Size]>
struct foo;

然而,數組需要有外部鏈接,所以要使方法 1 起作用,我們必須這樣寫:

However, the array would need to have external linkage, so to get approach 1 to work, we would have to write something like this:

/* Implementation of array to sequence goes here. */

constexpr const char str[] = "Hello, world!";

int main()
{
    using s = string<13, str>;
    return 0;
}

不用說,這很不方便.方法2實際上是不可能實現的.如果我們要聲明一個 (constexpr) 文字運算符,那么我們將如何指定返回類型?由于我們需要操作符返回一個可變字符序列,所以我們需要使用 const char* 參數來指定返回類型:

Needless to say, this is very inconvenient. Approach 2 is actually not possible to implement. If we were to declare a (constexpr) literal operator, then how would we specify the return type? Since we need the operator to return a variadic sequence of characters, so we would need to use the const char* parameter to specify the return type:

constexpr auto
operator"" _s(const char* s, size_t n) -> /* Some metafunction using `s` */

這會導致編譯錯誤,因為 s 不是 constexpr.嘗試通過執行以下操作來解決此問題并沒有多大幫助.

This results in a compile error, because s is not a constexpr. Trying to work around this by doing the following does not help much.

template <char... Ts>
constexpr sequence<Ts...> operator"" _s() { return {}; }

標準規定這種特定的文字運算符形式保留用于整數和浮點類型.雖然 123_s 會起作用,但 abc_s 不會.如果我們完全放棄用戶定義的文字,而只使用常規的 constexpr 函數會怎樣?

The standard dictates that this specific literal operator form is reserved for integer and floating-point types. While 123_s would work, abc_s would not. What if we ditch user-defined literals altogether, and just use a regular constexpr function?

template <unsigned Size>
constexpr auto
string(const char (&array)[Size]) -> /* Some metafunction using `array` */

和以前一樣,我們遇到的問題是,數組現在是 constexpr 函數的參數,它本身不再是 constexpr 類型.

As before, we run into the problem that the array, now a parameter to the constexpr function, is itself no longer a constexpr type.

我相信應該可以定義一個 C 預處理器宏,它將字符串和字符串的大小作為參數,并返回由字符串中的字符組成的序列(使用 BOOST_PP_FOR,字符串化、數組下標等).但是,我沒有時間(或足夠的興趣)來實現這樣的宏 =)

I believe it should be possible to define a C preprocessor macro that takes a string and the size of the string as arguments, and returns a sequence consisting of the characters in the string (using BOOST_PP_FOR, stringification, array subscripts, and the like). However, I do not have the time (or enough interest) to implement such a macro =)

推薦答案

我沒有看到任何東西可以與 Scott Schurr 的 str_const 在 C++ Now 2012 上發表.不過它確實需要 constexpr.

I haven't seen anything to match the elegance of Scott Schurr's str_const presented at C++ Now 2012. It does require constexpr though.

以下是您如何使用它,以及它可以做什么:

Here's how you can use it, and what it can do:

int
main()
{
    constexpr str_const my_string = "Hello, world!";
    static_assert(my_string.size() == 13, "");
    static_assert(my_string[4] == 'o', "");
    constexpr str_const my_other_string = my_string;
    static_assert(my_string == my_other_string, "");
    constexpr str_const world(my_string, 7, 5);
    static_assert(world == "world", "");
//  constexpr char x = world[5]; // Does not compile because index is out of range!
}

沒有比編譯時范圍檢查更酷的了!

It doesn't get much cooler than compile-time range checking!

無論是使用還是實現,都沒有宏.并且對字符串大小沒有人為限制.我會在這里發布實現,但我尊重 Scott 的隱含版權.實現在他的演示文稿的一張幻燈片上,鏈接到上面.

Both the use, and the implementation, is free of macros. And there is no artificial limit on string size. I'd post the implementation here, but I'm respecting Scott's implicit copyright. The implementation is on a single slide of his presentation linked to above.

這篇關于在 C++ 中方便地聲明編譯時字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 深圳活动策划公司|庆典策划|专业公关活动策划|深圳艺典文化传媒 重庆中专|职高|技校招生-重庆中专招生网 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 志高装潢官网-苏州老房旧房装修改造-二手房装修翻新 | 银川美容培训-美睫美甲培训-彩妆纹绣培训-新娘化妆-学化妆-宁夏倍莱妮职业技能培训学校有限公司 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 北京遮阳网-防尘盖土网-盖土草坪-迷彩网-防尘网生产厂家-京兴科技 | 大行程影像测量仪-探针型影像测量仪-增强型影像测量仪|首丰百科 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 槽钢冲孔机,槽钢三面冲,带钢冲孔机-山东兴田阳光智能装备股份有限公司 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | 螺旋丝杆升降机-SWL蜗轮-滚珠丝杆升降机厂家-山东明泰传动机械有限公司 | 北京公寓出租网-北京酒店式公寓出租平台| 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 膏剂灌装旋盖机-眼药水灌装生产线-西林瓶粉剂分装机-南通博琅机械科技 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 民用音响-拉杆音响-家用音响-ktv专用音响-万昌科技 | 长沙发电机-湖南发电机-柴油发电机供应厂家-长沙明邦智能科技 | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 窖井盖锯圆机_锯圆机金刚石锯片-无锡茂达金刚石有限公司 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 上海办公室装修,写字楼装修—启鸣装饰设计工程有限公司 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | led冷热冲击试验箱_LED高低温冲击试验箱_老化试验箱-爱佩百科 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | jrs高清nba(无插件)直播-jrs直播低调看直播-jrs直播nba-jrs直播 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 中开泵,中开泵厂家,双吸中开泵-山东博二泵业有限公司 | 点焊机-缝焊机-闪光对焊机-电阻焊设备生产厂家-上海骏腾发智能设备有限公司 | 全自动面膜机_面膜折叠机价格_面膜灌装机定制_高速折棉机厂家-深圳市益豪科技有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 房在线-免费房产管理系统软件-二手房中介房屋房源管理系统软件 | UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 |