您认为此代码会产生任何未定义的行为吗? [英] Do you think this code will produce any undefined behavior?

查看:89
本文介绍了您认为此代码会产生任何未定义的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++进行高速期货实现,并且正在修改函数指针逻辑的方法。我是一个没有大量使用C ++的/ b $ b C /汇编程序员。所以我的技能

肯定会得到改善。我想知道我是否正在对我的第一个

尝试处理任何未定义行为的函数指针。这是

代码:
http://appcore.home.comcast.net/vzoo...cope-guard.cpp
我基本上使用的是Andrei和Petru范围保护技术,用一个小的

调整:我正在使用一个指向模板化调用细节的函数指针,而不是一个布尔值的
。这样我可以像对范围保护一样使用它来测试函数指针对NULL的值

,我可以把它称为虚拟的

函数。因此,我可以虚拟地或通过

模板化的SafeWxecute函数在本地调用它...这听起来像是C ++大师吗?'


; ^)


谢谢大家的时间,


Chris Thomasson

解决方案

* Chris Thomasson:


我正在研究C ++中的高速期货实现,并且是

修补周围有包装函数指针逻辑的方法。我是一个没有大量使用C ++的/ b $ b C /汇编程序员。所以我的技能

肯定会得到改善。我想知道我是否正在对我的第一个

尝试处理任何未定义行为的函数指针。这是

代码:
http://appcore.home.comcast.net/vzoo...cope-guard.cpp


我基本上都在使用Andrei和Petru scopeguard技术,有一个小的

调整:我使用一个指向模板化调用细节的函数指针,而不是一个布尔值的
。这样我可以像对范围保护一样使用它来测试函数指针对NULL的值

,我可以把它称为虚拟的

函数。因此,我可以虚拟地或通过

模板化的SafeWxecute函数在本地调用它...这听起来像是C ++大师吗?'


; ^)



否。范围保护员的工作是清理。执行清理行动

不止一次必然会导致问题。如果你想要一个委托,

a可调用实体包装一个对象指针加上成员函数

指针,请考虑标准库和Boost / TR1类。

Boost课程是优越的,因为它们建立在标准库的经验之上,加上1998年以后开发的其他技术。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


" Alf P 。斯坦巴赫 < al *** @ start.nowrote in message

news:4s ************ @ mid.individual.net ...


> * Chris Thomasson:


>我正在研究C ++中的高速期货实现,



[...]


>我是想知道我是否第一次尝试使用函数指向任何未定义的行为。这是代码:

http://appcore.home.comcast.net/vzoo...cope-guard.cpp

我基本上使用的是Andrei和Petru范围保护技术,其中一个小
tweak:



[...]


>所以,我可以虚拟地或通过模板化的SafeWxecute
函数在本地调用它...这听起来像是C ++大师吗?'

; ^)



否。范围保护员的工作是清理。



好​​吧......我认为我用于该对象的名称并不能完全匹配它的

功能。因此,我将整个对象重命名为:

''funcall_base'',它不会在析构函数中调用任何东西;它在其功能的任何方面都不会使用它的范围...


执行清理行动不止一次必然会导致问题。



然后我可以添加一个名为''funcall_scoped''的变体,它确实会在析构函数中调用

,只要它是以前没有被解雇。那个

应该比以前更清楚地定义我的缩进用法...


如果你想要一个委托 ,一个包装对象指针的可调用实体

加上成员函数指针,



这就是我所追求的,除了我有点想在''funcall_base'中远离

虚拟析构函数/函数,因为它最终必须是一个与我的低级兼容的POD
C /汇编库,其中
包含我的新期货算法的实际实现细节。它应该有一个成员,它是一个类型的函数指针,它是一个类型的函数指针。

" standard"到我的低级库:


class funcall_base {

typedef void(* fpfuturesapi_t)(void const *);

fpfuturesapi_t m_fpcall_virtual;

};

因此库可以直接调用它来获取实际模板化的实现细节以调用自身。另外,我想我可以跳过调用

到''funcall_base''中包含的函数指针:


class funcall_base {

public:

void execute_virtual(){

m_fpcall_virtual(this);

}

};

当调用者知道其模板化呼叫详细信息的类型时,

提前:


class funcall_base {

public:

template< typename T_calldetail>

void execute_proxy(){

static_cast< T_calldetail *>(this) - > ; execute_direct();

}

};


//假设mycall是calldetails_t的基类

funcall_base mycall;

//我认为这个电话:


mycall.execute_virtual();

//应该有比这次电话更多的开销:


mycall.execute_proxy< calldetails_t>();

//我离开这里的路吗?


:^)


我很感激如果你能澄清一下......


考虑标准库和Boost / TR1类。 Boost

课程是优越的,因为他们建立在

标准库的经验基础上,加上1998年以后开发的其他技术。



不幸的是,在我的C ++期货的实现细节中,我无法使用Boost的任何部分或任何其他的b
第三方库

图书馆。


; ^(...


感谢您的耐心等待!



Chris Thomasson写道:


不幸的是,我不能使用Boost的任何部分或其他任何部分

第三方库,在我的C ++期货的实现细节中

库。


; ^(...



为什么不呢?你知道提升是开源的,但不是GPL,对吗?这更像是BSD许可证的b $ b在开源或商业中使用它,无论你想要什么




Tony


I am working on a high-speed futures implementation in C++, and was
tinkering around with ways to wrap the function pointer logic. I am a
C/Assembly programmer that doesn''t make a lot of use of C++. So my skills
can be improved upon for sure. I was wondering if I was subjecting my first
attempt at a function pointer to any undefined behavior(s). Here is the
code:
http://appcore.home.comcast.net/vzoo...cope-guard.cpp
I''m basically using Andrei and Petru scopeguard technique, with one small
tweak: I am using a function pointer to the templated call details, instead
of a boolean. That way I can use it like a scopeguard by testing the value
of the function pointer against NULL, and I can call it like a virtual
function of sorts. So, I can call it virtually, or locally through the
templated SafeWxecute function... Does this sound Kosher to you C++ gurus''?

;^)

Thank you all for your time,

Chris Thomasson

解决方案

* Chris Thomasson:

I am working on a high-speed futures implementation in C++, and was
tinkering around with ways to wrap the function pointer logic. I am a
C/Assembly programmer that doesn''t make a lot of use of C++. So my skills
can be improved upon for sure. I was wondering if I was subjecting my first
attempt at a function pointer to any undefined behavior(s). Here is the
code:
http://appcore.home.comcast.net/vzoo...cope-guard.cpp
I''m basically using Andrei and Petru scopeguard technique, with one small
tweak: I am using a function pointer to the templated call details, instead
of a boolean. That way I can use it like a scopeguard by testing the value
of the function pointer against NULL, and I can call it like a virtual
function of sorts. So, I can call it virtually, or locally through the
templated SafeWxecute function... Does this sound Kosher to you C++ gurus''?

;^)

No. A scopeguard''s job is to clean up. Executing the clean-up action
more than once is bound to lead to problems. If you want a "delegate",
a callable entity that wraps an object pointer plus member function
pointer, consider the standard library and Boost/TR1 classes for that.
The Boost classes are superior because they build on the experience with
the standard library, plus additional techniques developed after 1998.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


"Alf P. Steinbach" <al***@start.nowrote in message
news:4s************@mid.individual.net...

>* Chris Thomasson:

>I am working on a high-speed futures implementation in C++,

[...]

>I was wondering if I was subjecting my first attempt at a function
pointer to any undefined behavior(s). Here is the code:

http://appcore.home.comcast.net/vzoo...cope-guard.cpp

I''m basically using Andrei and Petru scopeguard technique, with one small
tweak:

[...]

>So, I can call it virtually, or locally through the templated SafeWxecute
function... Does this sound Kosher to you C++ gurus''?

;^)


No. A scopeguard''s job is to clean up.

Okay... I think the name I used for the object didn''t safely match its
functionality. Therefore, I will rename the entire object to something like:
''funcall_base'' which will not call anything in the destructor; it won''t use
its scope in any aspect of its functionality...

Executing the clean-up action more than once is bound to lead to problems.

Then I can add a variant called ''funcall_scoped'' which will indeed invoke
the call in its destructor, only if it was not previously "Dismissed". That
should define my indented usage a bit clearer than before...

If you want a "delegate", a callable entity that wraps an object pointer
plus member function pointer,

That''s kind of what I am after, except I kind of want to stay away from
virtual destructors/functions in ''funcall_base'' because it ultimately has to
be a POD that is compatible with my low-level C/Assembly library, which
contains the actual implementation details of my new futures algorithm. It
should have a single member which is a function pointer of a type that is
"standard" to my low-level library:

class funcall_base {
typedef void (*fpfuturesapi_t) (void const*);
fpfuturesapi_t m_fpcall_virtual;
};
So that the library can call it directly to get the actual templated
implementation details to invoke itself. Also, I think I can skip calls
into the function pointer contained in ''funcall_base'':

class funcall_base {
public:
void execute_virtual() {
m_fpcall_virtual(this);
}
};
when the type of its templated call details are known by the caller in
advance:

class funcall_base {
public:
template<typename T_calldetail>
void execute_proxy() {
static_cast<T_calldetail*>(this)->execute_direct();
}
};

// assume mycall is base class of calldetails_t
funcall_base mycall;
// I think that this call:

mycall.execute_virtual();
// should have more overhead than this call:

mycall.execute_proxy<calldetails_t>();
// Am I way off base here?

:^)

I would appreciate it if you could clarify a bit...

consider the standard library and Boost/TR1 classes for that. The Boost
classes are superior because they build on the experience with the
standard library, plus additional techniques developed after 1998.

Unfortunately, I can''t make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.

;^(...

Thanks for your patience!



Chris Thomasson wrote:

Unfortunately, I can''t make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.

;^(...

Why not? You know that boost is open source but NOT GPL, right? It is
more like BSD license - use it in open source or commercial, whatever
you want.

Tony


这篇关于您认为此代码会产生任何未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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