Concat两个`const char`字符串文字 [英] Concat two `const char` string literals

查看:71
本文介绍了Concat两个`const char`字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #define nl(str)str \n 

int main()
{
std :: cout<<
nl(用法:foo)
nl(打印消息)
;

返回0;
}

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

解决方案


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


  2. 编译器不需要对静态和线程持续时间对象以外的任何对象执行常量初始化。特别是,临时对象(不是变量,并且具有小于自动存储持续时间的对象)不需要进行常量初始化,据我所知,没有编译器对数组执行此操作。有关定义块初始化的信息,请参见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.

解决方案

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

  2. 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`字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆