恼人的常见问题 [英] Annoying const problem
问题描述
我有一个总是占用16个字节数据的函数并且没有修改它/ b $ b它:
void func(字节const(* data) )[16]);
但是,如果我尝试使用非const数据调用它,编译器将无法执行转换:
static const byte bar1 [16] = {0};
int foo()
{
byte bar2 [16] = {0};
func(& bar1); / * OK * /
func(& bar2); / *错误* /
}
除了定义const和
之外,还有其他解决方法吗? func的nonconst版本,或者将指针传递给第一个元素
数据,从而丢失编译时长度检查?
Old Wolf写道:
>
我有一个总是占用16个字节数据的函数没有
修改它:
void func(字节常量(*数据)[16]);
但是,如果我尝试使用非const数据调用它,编译器将无法执行转换:
static const byte bar1 [16] = {0};
int foo()
{
byte bar2 [16] = {0} ;
func(& bar1); / * OK * /
func(& bar2); / *错误* /
}
除了定义const和
之外,还有其他解决方法吗?非对象版本的func,或者将指针传递给第一个元素
的数据,从而失去编译时长度检查?
可能用C编译器编译就足够了。看起来
就像你在使用C ++。
-
Chuck F(cinefalconer at maineline dot net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>
>
在文章< 11 ********************** @ n67g2000cwd.googlegroups .com> ;,
Old Wolf< ; ol ***** @ inspire.net.nzwrote:
> void func(byte const(* data)[16]);
[推测字节是typedefed。]
>但是,如果我尝试调用它非常数据,编译器无法执行转换:
你必须进行明确的演员:
func((字节const(*)[16])& bar2);
当然,这会丢失类型检查,但仅限于
你施放它的情况。
- 理查德
-
"考虑应该在一些字母表中需要多达32个字符
- X3.4,1963。
CBFalconer写道:
Old Wolf写道:
< blockquote class =post_quotes>
void func(byte const(* data)[16]);
static const byte bar1 [16] = {0};
int foo()
{
byte bar2 [16] = {0};
func(& bar1); / * OK * /
func(& bar2); / *错误* /
}
可能用C编译器进行编译就足够了。看起来
就像你在使用C ++一样。
其实我不是。谢谢你的猜测。什么C编译器
您使用它编译此代码而不发出诊断?
typedef unsigned char byte;
void func (byte const(* data)[16]){}
int main()
{
byte bar [16] = {0 };
func(& bar);
}
I have a function that always takes 16 bytes of data and doesn''t modify
it:
void func( byte const (*data)[16] );
However, if I try to call it with non-const data, the compiler is
unable to perform the conversion:
static const byte bar1[16] = { 0 };
int foo()
{
byte bar2[16] = { 0 };
func( &bar1 ); /* OK */
func( &bar2 ); /* Error */
}
Is there any work-around to this, other than defining a const and a
nonconst version of func, or passing a pointer to the first element
of data and thereby losing the compile-time length check?
Old Wolf wrote:>
I have a function that always takes 16 bytes of data and doesn''t
modify it:
void func( byte const (*data)[16] );
However, if I try to call it with non-const data, the compiler is
unable to perform the conversion:
static const byte bar1[16] = { 0 };
int foo()
{
byte bar2[16] = { 0 };
func( &bar1 ); /* OK */
func( &bar2 ); /* Error */
}
Is there any work-around to this, other than defining a const and a
nonconst version of func, or passing a pointer to the first element
of data and thereby losing the compile-time length check?Probably it will be enough to compile with a C compiler. Looks
like you are using C++.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
In article <11**********************@n67g2000cwd.googlegroups .com>,
Old Wolf <ol*****@inspire.net.nzwrote:
>void func( byte const (*data)[16] );[Presumably byte is typedefed.]
>However, if I try to call it with non-const data, the compiler is
unable to perform the conversion:You''ll have to put in an explicit cast:
func( (byte const (*)[16]) &bar2 );
Of course, this loses the type checking, but only for the cases where
you cast it.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
CBFalconer wrote:Old Wolf wrote:void func( byte const (*data)[16] );
static const byte bar1[16] = { 0 };
int foo()
{
byte bar2[16] = { 0 };
func( &bar1 ); /* OK */
func( &bar2 ); /* Error */
}
Probably it will be enough to compile with a C compiler. Looks
like you are using C++.Actually I''m not. Thanks for guessing though. What C compiler are
you using that compiles this code without issuing a diagnostic?
typedef unsigned char byte;
void func( byte const (*data)[16] ) {}
int main()
{
byte bar[16] = { 0 };
func( &bar );
}
这篇关于恼人的常见问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!