Concat两个`const char`字符串文字

编程入门 行业动态 更新时间:2024-10-24 02:37:25
本文介绍了Concat两个`const char`字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以使用 constexpr 连接两个字符串文字?或者换句话说,可以消除代码中的宏吗?

#define nl(str)str \n int main() { std :: cout<< nl(用法:foo) nl(打印消息); 返回0; }

更新:使用 \n ,但是我想知道是否可以使用 constexpr 替换那些类型的宏。 / p>

解决方案

  • 是的,完全可以创建编译时常量字符串并进行操作它们带有constexpr函数甚至运算符。但是,

  • 编译器不需要对静态和线程持续时间对象以外的任何对象执行常量初始化。特别是,临时对象(不是变量,并且具有小于自动存储持续时间的对象)不需要进行常量初始化,据我所知,没有编译器对数组执行此操作。有关定义块初始化的信息,请参见3.6.2 / 2-3;有关块级静态持续时间变量的更多措辞,请参见6.7.4。这些都不适用于临时对象,其寿命在12.2 / 3及以下版本中定义。

  • 因此,您可以实现所需的编译时级联:

    static const auto conc =<一些聪明的constexpr somethingy> std :: cout<<浓

    但您不能使其与以下项一起使用:

    std :: cout<< <一些聪明的constexpr东西> ;;

    更新:

    但是您可以使其与以下项一起使用:

    std :: cout<< * []()-> const { static constexpr auto s = / * constexpr call * /; return& s;}()<< 更多的文字;

    但是,标点符号过于丑陋,无法比一个有趣的小技巧更重要。 p>

    (免责声明:IANALL,尽管有时我喜欢在互联网上玩一个。因此,该标准可能有些尘土飞扬

    (尽管免责声明,并由@DyP推动,但我还是添加了一些语言上合法的引文。)

    Is it possible to concat two string literals using a constexpr? Or put differently, can one eliminate macros in code like:

    #define nl(str) str "\n" int main() { std::cout << nl("usage: foo") nl("print a message") ; return 0; }

    Update: There is nothing wrong with using "\n", however I would like to know whether one can use constexpr to replace those type of macros.

    解决方案

  • Yes, it is entirely possible to create compile-time constant strings, and manipulate them with constexpr functions and even operators. However,

  • The compiler is not required to perform constant initialization of any object other than static- and thread-duration objects. In particular, temporary objects (which are not variables, and have something less than automatic storage duration) are not required to be constant initialized, and as far as I know no compiler does that for arrays. See 3.6.2/2-3, which define constant initialization, and 6.7.4 for some more wording with respect to block-level static duration variables. Neither of these apply to temporaries, whose lifetime is defined in 12.2/3 and following.

  • So you could achieve the desired compile-time concatenation with:

    static const auto conc = <some clever constexpr thingy>; std::cout << conc;

    but you can't make it work with:

    std::cout << <some clever constexpr thingy>;

    Update:

    But you can make it work with:

    std::cout << *[]()-> const { static constexpr auto s = /* constexpr call */; return &s;}() << " some more text";

    But the boilerplate punctuation is way too ugly to make it any more than an interesting little hack.

    (Disclaimer: IANALL, although sometimes I like to play one on the internet. So there might be some dusty corners of the standard which contradicts the above.)

    (Despite the disclaimer, and pushed by @DyP, I added some more language-lawyerly citations.)

    更多推荐

    Concat两个`const char`字符串文字

    本文发布于:2023-07-28 14:10:21,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1231643.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   两个   文字   Concat   char

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!