字符串文字中的C预处理器插入/附加标记 [英] C preprocessor insert/append token in string literal

查看:83
本文介绍了字符串文字中的C预处理器插入/附加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下宏:

#define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__)                      \
static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \
              "Expected iterator with value type #Value_type__")

在上面的宏中,我试图在 Value_type __ 标记中插入/附加作为第二个输入的字符串文字 static_assert 中的参数。

In the macro above I'm trying to insert/append the Value_type__ token in the string literal that's feed in as the second input argument in static_assert.

很明显,这不是我要实现的目标,因为如果我将宏声明为:

Obviously, this is not what I'm trying to achieve, since if I state the macro as:

ASSERT_ITERATOR_VALUE_TYPE(std::set<int>::iterator, double);

我将收到消息:

error: static assertion failed: Expected iterator with value type #Value_type__
                                                                  ^^^^^^^^^^^^^

在这里,我想接受以下消息:

where instead I would like to take the message:

error: static assertion failed: Expected iterator with value type double
                                                                  ^^^^^^

实时演示

是否存在某种预处理程序可以帮助我实现我所不想要的?

Is there some kind of preprocessor sorcery that will help me achieve what I wan't?

推荐答案

#define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__)                      \
static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \
              "Expected iterator with value type " #Value_type__)

您将宏参数扩展为字符串文字,然后依靠字符串文字串联。

You expand the macro parameter into a string literal, and then rely on string literal concatenation.

这篇关于字符串文字中的C预处理器插入/附加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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