不正确的做法 [英] const-incorrect practice

查看:61
本文介绍了不正确的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些软件提供了const错误的编程接口。我猜想,如果考虑一些基本的设计规则和模式,可以从

开始构建const-correct API / SDK。

对于初学者来说,准备好并习惯这种编程风格是多么困难?

你需要多少钱?的const_cast"因为关键词

" const" API在重要案例中被遗忘为类型说明者

设计师?


这种技术似乎存在强烈的不同意见

成为检测安全漏洞的宝贵工具,以及整体源代码质量的改进。为了达到常识,经验是否会随着时间的推移而变化?

开发人员和程序员是否需要像编译器这样的关键/成功案例

优化和错误避免给予正确性尝试和

让它成为一种常见的做法?

你想表明维护工作是否值得

实现方式不可变数据结构?
http://en.wikipedia.org/wiki / Const_correctness


问候,

Markus

A couple of software provides const-incorrect programming interfaces. I
guess that it is possible to develop const-correct APIs/SDKs from the
beginning if a few basic design rules and patterns would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?
How much do you need to fiddle with "const_cast" because the key word
"const" was forgotten for type specifiers in important cases by an API
designer?

It seems that there exist strong different opinions when this technique
becomes a valuable tool for the detection of security flaws and the
improvement of the overall source code quality. Does the experience
change over time to achieve a common sense?
Do developers and programmers need key/success stories like compiler
optimisations and error avoidance to give "correctness" a try and to
let it become a common practice?
Do you want to show that the maintenance effort is worth for the
implementation way of immutable data structures?
http://en.wikipedia.org/wiki/Const_correctness

Regards,
Markus

推荐答案

Ma ************ @ web.de 写道:

[...]
Ma************@web.de writes:
[...]
你需要多少才能摆弄const_cast?因为关键词
const API
设计师在重要案例中忘记了类型说明符?
How much do you need to fiddle with "const_cast" because the key word
"const" was forgotten for type specifiers in important cases by an API
designer?




C没有const_cast。后续重定向。


-

Keith Thompson(The_Other_Keith) ks ** *@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



C has no "const_cast". Followups redirected.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Ma * ***********@web.de 写道:
一些软件提供了const错误的编程接口。我猜想如果考虑一些基本的设计规则和模式,可以从
开始开发const-correct API / SDK。
对于初学者来说,制作它有多困难正确并习惯了这种编程风格?


一点都不困难:

只要定义,除非需要所有成员函数都是const,

,并且除了极少数例外,不允许使用const_cast

和/或可变。


有几种情况如何违反const正确性,

所有这些都从const函数返回非常量引用或指针

。例如,


class Class {

//构造函数,副本,分配,析构函数省略。

public:

int& getInt()const

{

返回* i_;

}

私人:

int * i_;

};


void foo(const Class& c)

{

c.getInt()= 5;

}


将在大多数编译器上愉快地编译。

多少钱你需要摆弄const_cast因为关键词
const API
设计师在重要案例中忘记了类型说明符?


我不记得因为一个

API而需要const_cast的任何情况。

似乎那里当这种技术成为检测安全漏洞和提高整体源代码质量的有价值的工具时,存在着强烈的不同意见。经验是否随着时间的推移而变化以实现常识?
开发人员和程序员是否需要关键/成功案例,如编译器优化和错误避免,以提供正确性。尝试并让它成为一种常见的做法?
你想表明维护工作是否值得实现不可变数据结构的实现方式?
http://en.wikipedia.org/wiki/Const_correctness

与维护工作无关。最大的好处来自

关于

问题狩猎:如果一个函数需要一个const参数,那么就没有必要

来按照路径进行操作通过函数的参数,提供

const_cast没有使用。

问候,
Markus
A couple of software provides const-incorrect programming interfaces. I
guess that it is possible to develop const-correct APIs/SDKs from the
beginning if a few basic design rules and patterns would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?
Not difficult at all:
Just define that unless needed all member functions are const,
and, with very few exceptions, do not allow the use of const_cast
and/or mutable.

There are a few situations how the const-correctness can be violated,
all of them returning non-const references or pointers
from const functions. E.g.,

class Class {
// constructor, copy, assigment, destructor ommitted.
public:
int& getInt() const
{
return *i_;
}
private:
int* i_;
};

void foo(const Class& c)
{
c.getInt() = 5;
}

will compile happily on most compilers.
How much do you need to fiddle with "const_cast" because the key word
"const" was forgotten for type specifiers in important cases by an API
designer?
I don''t remember any case where I needed to const_cast because of an
API.

It seems that there exist strong different opinions when this technique
becomes a valuable tool for the detection of security flaws and the
improvement of the overall source code quality. Does the experience
change over time to achieve a common sense?
Do developers and programmers need key/success stories like compiler
optimisations and error avoidance to give "correctness" a try and to
let it become a common practice?
Do you want to show that the maintenance effort is worth for the
implementation way of immutable data structures?
http://en.wikipedia.org/wiki/Const_correctness
Not so much the maintenance effort. The big benefit comes when it is
about
problem hunting: If a function takes a const argument, there''s no need
to follow the "path" of the argument through the function, provided
const_cast is not used.

Regards,
Markus






Stephan Br?nnimann写道:
Stephan Br?nnimann wrote:
Ma ************@web.de 写道:
Ma************@web.de wrote:
一些软件提供了const错误的编程接口。我猜想如果考虑一些基本的设计规则和模式,可以从
开始开发const-correct API / SDK。
对于初学者来说,制作它有多困难正确并习惯了这种编程风格?
A couple of software provides const-incorrect programming interfaces. I
guess that it is possible to develop const-correct APIs/SDKs from the
beginning if a few basic design rules and patterns would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?



根本不难:
只要定义,除非需要所有成员函数都是const,否则并且,除了极少数例外,不允许使用const_cast
和/或可变。

有几种情况如何违反const正确性,所有这些都从const函数返回非const引用或指针
。例如,类类{
//构造函数,复制,分配,析构函数已被省略。
公共:
int& getInt()const
{
返回* i_;
}
私人:
int * i _;



Not difficult at all:
Just define that unless needed all member functions are const,
and, with very few exceptions, do not allow the use of const_cast
and/or mutable.

There are a few situations how the const-correctness can be violated,
all of them returning non-const references or pointers
from const functions. E.g.,

class Class {
// constructor, copy, assigment, destructor ommitted.
public:
int& getInt() const
{
return *i_;
}
private:
int* i_;




请不要将这个偏离主题的内容发布到clc


-

"如果您想通过groups.google.com发布后续内容,不要使用

破碎的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson



Please do not post this off-topic stuff to c.l.c.

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


这篇关于不正确的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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