预处理器可以将字符串文字转换为字符吗? [英] can the pre-processor convert string literals into chars?

查看:53
本文介绍了预处理器可以将字符串文字转换为字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




感谢您抽出宝贵时间阅读本文。


我正在寻找预处理器命令允许我在编译时解析

const

字符串到const char文字。


查看下面的代码,我可以取5个字符并在编译时创建一个const

uint64_t值。

然后我可以创建一个具有这些const值的switch语句



各种情况。

由于每个案例在编译时解析为整数常量值

时间,

这个switch语句有效。


const uint64_t val1 =(static_cast< uint64_t>(''S'')<< 32)| // S $ / $
(static_cast< uint64_t>(''T'')<< 24)| // T

(static_cast< uint64_t>(''A'')<< 16)| // A

(static_cast< uint64_t>(''R'')<< 8)| // R

(static_cast< uint64_t>(''T'')); // T


const char c1 =''S'',c2 =''T'',c3 =''O'',c4 =''P'';

const uint64_t val2 =(static_cast< uint64_t>(c1)<< 32)| // S $ / $
(static_cast< uint64_t>(c2)<< 24)| // T $ / $
(static_cast< uint64_t>(c3)<< 16)| // O

(static_cast< uint64_t>(c4)<< 8)| // P

(static_cast< uint64_t>(0));


uint64_t input = 0;

开关(输入)

{

案例val1:printf(" input = val1 \ n"); break;

case val2:printf(" input = val2 \ n");休息;

默认:休息;

}

// ----------------- -------------------------------------------------- -------


现在我尝试重复上面的代码,而不是使用文字

字符

值,我使用const字符串文字。

gcc返回以下错误:


错误:case标签不会减少为整数常量


所以我猜编译器无法在编译时将索引解析为char

数组。

const char * const str1 = START;

const uint64_t val1 =(static_cast< uint64_t>(str1 [0])<< 32)| //

S

(static_cast< uint64_t>(str1 [1])<< 24)| //

T

(static_cast< uint64_t>(str1 [2])<< 16)| //

A

(static_cast< uint64_t>(str1 [3])<< 8)| //

R

(static_cast< uint64_t>(str1 [4])); //

T


const char * const str2 =" STOP";

const uint64_t val2 =(static_cast< uint64_t> ;(str2 [0])<< 32)| //

S

(static_cast< uint64_t>(str2 [1])<< 24)| //

T

(static_cast< uint64_t>(str2 [2])<< 16)| //

O

(static_cast< uint64_t>(str2 [3])<< 8)| //

P

(static_cast< uint64_t>(str2 [4]));


uint64_t input = 0;

开关(输入)

{

案例val1:printf(" input = val1 \ n"); break;

case val2:printf(" input = val2 \ n");休息;

默认:休息;

}

// ----------------- -------------------------------------------------- -------


我实际拥有的是一个包含许多独特输入的文本文件

字符串,

每个最多5个字符。

这些输入字符串中的每一个都有一个相应的名称和一个映射到它的
的值。我希望能够为每个

可能的输入字符串创建一个带有

的switch语句,它返回相关的值。

在运行时,我可以从任何5个字符输入创建一个uint64_t值

字符串并通过我的case语句运行该值以获得返回

值。显然性能明显远远好于:

if(strncmp(...))

返回...

else

if(strncmp(...))

返回...

其他

ad无限

请参阅下面的示例数据:


<< input_values.txt>>

MACRO1(DO_START," START",enum_val_start)

MACRO1(DO_STOP," STOP",enum_val_stop)
< br $>
//创建const值

#undef MACRO1

#define MACRO1(名称,输入,值)\

const uint64_t val _ ## NAME =(static_cast< uint64_t>(INPUT [0])

<< 32)| \

(static_cast< uint64_t>(INPUT [1])

<< 24)| \

(static_cast< uint64_t>(INPUT [2])

<< 16)| \

(static_cast< uint64_t>(INPUT [3])

<< 8)| \

(static_cast< uint64_t>(INPUT [4]));

#include" input_values.txt"


//创建包含上述const值的switch语句

#undef MACRO1

#define MACRO1(NAME,INPUT,VALUE)\

case val _ ## NAME:返回VALUE;


switch(val)

{

#include" input_value.txt"

默认值:break;

}

// -------------------- -------------------------------------------------- ----


TIA为您提供帮助

Hi

Thanks for taking the time to read this.

I''m looking for a pre-processor command that will allow me to resolve
const
strings into const char literals at compile time.

Looking at the code below, I can take 5 characters and create a const
uint64_t value at compile time.
I can then create a switch statement that has these const values as
the
various cases.
Since each case resolves to an integer constant value at compile
time,
this switch statement is valid.

const uint64_t val1 = (static_cast<uint64_t>(''S'') << 32) | // S
(static_cast<uint64_t>(''T'') << 24) | // T
(static_cast<uint64_t>(''A'') << 16) | // A
(static_cast<uint64_t>(''R'') << 8) | // R
(static_cast<uint64_t>(''T'')); // T

const char c1 = ''S'', c2 = ''T'', c3 = ''O'', c4 = ''P'';
const uint64_t val2 = (static_cast<uint64_t>(c1) << 32) | // S
(static_cast<uint64_t>(c2) << 24) | // T
(static_cast<uint64_t>(c3) << 16) | // O
(static_cast<uint64_t>(c4) << 8) | // P
(static_cast<uint64_t>(0));

uint64_t input = 0;
switch (input)
{
case val1: printf("input=val1\n"); break;
case val2: printf("input=val2\n"); break;
default: break;
}
//--------------------------------------------------------------------------

Now I attempt to repeat the above code, but instead of using literal
character
values, I use a const string literal.
gcc returns the following error:

error: case label does not reduce to an integer constant

So I guess the compiler is not able to resolve the index into the char
array at compile time.
const char* const str1 = "START";
const uint64_t val1 = (static_cast<uint64_t>(str1[0]) << 32) | //
S
(static_cast<uint64_t>(str1[1]) << 24) | //
T
(static_cast<uint64_t>(str1[2]) << 16) | //
A
(static_cast<uint64_t>(str1[3]) << 8) | //
R
(static_cast<uint64_t>(str1[4])); //
T

const char* const str2 = "STOP";
const uint64_t val2 = (static_cast<uint64_t>(str2[0]) << 32) | //
S
(static_cast<uint64_t>(str2[1]) << 24) | //
T
(static_cast<uint64_t>(str2[2]) << 16) | //
O
(static_cast<uint64_t>(str2[3]) << 8) | //
P
(static_cast<uint64_t>(str2[4]));

uint64_t input = 0;
switch (input)
{
case val1: printf("input=val1\n"); break;
case val2: printf("input=val2\n"); break;
default: break;
}
//--------------------------------------------------------------------------

What I actually have is a text file containing many unique input
strings,
each a maximum of 5 characters long.
Each of these input strings has a corresponding name and a value that
it maps to. I''d like to be able to create a switch statement with a
case for each of the
possible input strings which returns the associated value.
At run-time, I can create a uint64_t value from any 5 character input
string and run the value through my case statement to get a return
value. Obviously performance wise this is far better than:
if (strncmp(...))
return ...
else
if (strncmp(...))
return ...
else
ad infinitum
Please see my example data below:

<< input_values.txt >>
MACRO1(DO_START, "START", enum_val_start)
MACRO1(DO_STOP, "STOP", enum_val_stop)

// create const values
#undef MACRO1
#define MACRO1(NAME, INPUT, VALUE) \
const uint64_t val_##NAME = (static_cast<uint64_t>(INPUT[0])
<< 32) | \
(static_cast<uint64_t>(INPUT[1])
<< 24) | \
(static_cast<uint64_t>(INPUT[2])
<< 16) | \
(static_cast<uint64_t>(INPUT[3])
<< 8) | \
(static_cast<uint64_t>(INPUT[4]));
#include "input_values.txt"

// create switch statement containing above const values
#undef MACRO1
#define MACRO1(NAME, INPUT, VALUE) \
case val_##NAME: return VALUE;

switch (val)
{
#include "input_value.txt"
default: break;
}
//--------------------------------------------------------------------------

TIA for your help

推荐答案

2007年3月29日07 :06:38-0700, st *********** @ gmail.com 写在

comp.lang.c ++:
On 29 Mar 2007 07:06:38 -0700, st***********@gmail.com wrote in
comp.lang.c++:




感谢您的参与是时候读这篇文章了。
Hi

Thanks for taking the time to read this.



我会感谢你多次发帖,但是哎呀!,

你做了。

然后你浪费时间和带宽与某人讨论

对你做出回应因为每微秒计数,你抱怨

你可以'' t测试你对相对效率的要求,因为它太难以按照你想要的方式手动构建代码了。


你想要这样的东西:

I would have thanked you for not posting it multiple times, but oops!,
you did.

And then you waste time and bandwidth arguing with someone who
responds to you because "every microsecond counts", and you complain
you can''t test your claims about relative efficiency because it''s too
hard to manually build code the way you want to.

You want something like this:


const uint64_t val1 =(static_cast< uint64_t>(''S'')<< 32)| // S $ / $
(static_cast< uint64_t>(''T'')<< 24)| // T

(static_cast< uint64_t>(''A'')<< 16)| // A

(static_cast< uint64_t>(''R'')<< 8)| // R

(static_cast< uint64_t>(''T'')); // T
const uint64_t val1 = (static_cast<uint64_t>(''S'') << 32) | // S
(static_cast<uint64_t>(''T'') << 24) | // T
(static_cast<uint64_t>(''A'') << 16) | // A
(static_cast<uint64_t>(''R'') << 8) | // R
(static_cast<uint64_t>(''T'')); // T



[snip]


来自:

[snip]

From this:


现在我尝试重复上面的代码,但不是使用文字

字符

值,而是使用const字符串文字。

gcc返回以下错误:


错误:case标签没有减少到整数常量


所以我猜编译器不能在编译时将索引解析为char

数组。

const char * const str1 =" START";

const uint64_t val1 =(static_cast< uint64_t>(str1 [0])<< 32)| //

S

(static_cast< uint64_t>(str1 [1])<< 24)| //

T

(static_cast< uint64_t>(str1 [2])<< 16)| //

A

(static_cast< uint64_t>(str1 [3])<< 8)| //

R

(static_cast< uint64_t>(str1 [4])); //

T
Now I attempt to repeat the above code, but instead of using literal
character
values, I use a const string literal.
gcc returns the following error:

error: case label does not reduce to an integer constant

So I guess the compiler is not able to resolve the index into the char
array at compile time.

const char* const str1 = "START";
const uint64_t val1 = (static_cast<uint64_t>(str1[0]) << 32) | //
S
(static_cast<uint64_t>(str1[1]) << 24) | //
T
(static_cast<uint64_t>(str1[2]) << 16) | //
A
(static_cast<uint64_t>(str1[3]) << 8) | //
R
(static_cast<uint64_t>(str1[4])); //
T



你想折磨和滥用预处理器,总是一个坏主意,

使上面的第二种情况像第一种情况一样工作,因为你的b
$只有太多的文字字符串需要手工编写,差你就好了。


嗯你是一个程序员还是只是一个唠叨的人?


写一个程序,读取一个文本文件,包含一行单词

每行五个字符或更少,并写入另一个带有

名称的文本文件,例如my_constants.h。


写入START的代码有多难;并且

写出与上面第一个引用部分相同的输出文本?


编写一个小实用程序几乎总是比
$ b更好的选择$ b折磨预处理器。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang的常见问题解答.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++ -faq-lite /

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

You want to torture and abuse the preprocessor, always a bad idea, to
make the second case, above, work like the first case, because you
have just too many literal strings to code by hand, poor you.

Well are you a programmer or just a whiner?

Write a program that reads a text file consisting of a single word of
five characters or less per line, and writes another text file with a
name like "my_constants.h".

How hard can it possible be to write code that reads "START" and
writes output text identical to the first quoted portion above?

Writing a small utility program is almost always a better choice than
torturing the preprocessor.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


Jack Klein写道:
Jack Klein wrote:

2007年3月29日07:06:38 -0700,取值t***********@gmail.com 写在

comp.lang.c ++:
On 29 Mar 2007 07:06:38 -0700, st***********@gmail.com wrote in
comp.lang.c++:



[...]

[...]


编写一个小的实用程序几乎总是比对b / b
折磨预处理器更好的选择。
Writing a small utility program is almost always a better choice than
torturing the preprocessor.



或使用不可读的模板:-)。 (使用模板的解决方案

元编程是合理的,当您只需要信息时可以在编译器中使用
,例如关于类型。)


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

Or using unreadable templates:-). (A solution using template
meta-programming is justifiable when you need information only
available within the compiler, e.g. concerning types.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于预处理器可以将字符串文字转换为字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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