#define的替代品? [英] Alternatives to #define?

查看:75
本文介绍了#define的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用哪些其他c ++构造代替#define来执行

几个函数?

示例:


#define DO_STUFF doThis(); doThat();


我猜我可以使用模板函数,内联函数或

内联静态方法。


// 1

命名空间MyUtils

{

模板<>

void doStuff()

{

doThis();

doThat();

}

}


// 2

命名空间MyUtils

{

inline void doStuff()

{

doThis();

doThat();

}

}


// 3

class MyUtils

{

public:

static inline void doStuff()const

{

doThis();

doThat();

}

}


我*相信*模板版本总是内联,而另一个

2个版本可能是内联的。

这些方法是否正确?

哪个应该首选?

还有其他更好的方法吗?


/ Carl

What other c++ constructs can I use instead of #define for executing a
couple of functions?
Example:

#define DO_STUFF doThis(); doThat();

I''d guess that I can either use a template function, an inlined function or
an inlined static method.

//1
namespace MyUtils
{
template<>
void doStuff()
{
doThis();
doThat();
}
}

//2
namespace MyUtils
{
inline void doStuff()
{
doThis();
doThat();
}
}

//3
class MyUtils
{
public:
static inline void doStuff() const
{
doThis();
doThat();
}
}

I *believe* that the template version always is inlined, and that the other
2 versions is probably inlined.
Are theese approaches correct?
Which should be preferred?
Are there any other better way?

/Carl

推荐答案



" Carl Ribbegaardh" < CA ********************* @ hotmail.com>在消息中写道

news:2k ************ @ uni-berlin.de ...

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in message
news:2k************@uni-berlin.de...
我可以使用哪些其他c ++结构而不是#define来执行
几个函数?
示例:

#define DO_STUFF doThis(); doThat();

我猜我可以使用模板函数,内联函数
或内联静态方法。

// 1
命名空间MyUtils
{
模板<>
void doStuff()
{
doThis();
doThat();
}
}
// 2命名空间MyUtils
{
inline void doStuff()
{
doThis( );
doThat();
}
}

// 3
类MyUtils
{
公开:
static inline void doStuff()const
{
doThis();
doThat();
}
}

我*相信*模板版本总是内联的,并且
其他2个版本可能是内联的。


不,这不是真的。我想你正在考虑

模板的异常,它来自函数和

类的正常定义规则。但那是一个完全独立的问题。

这些方法是否正确?


他们都是正确的。

哪个应该首选?


第二个

还有其他更好的办法吗?
What other c++ constructs can I use instead of #define for executing a
couple of functions?
Example:

#define DO_STUFF doThis(); doThat();

I''d guess that I can either use a template function, an inlined function or an inlined static method.

//1
namespace MyUtils
{
template<>
void doStuff()
{
doThis();
doThat();
}
}

//2
namespace MyUtils
{
inline void doStuff()
{
doThis();
doThat();
}
}

//3
class MyUtils
{
public:
static inline void doStuff() const
{
doThis();
doThat();
}
}

I *believe* that the template version always is inlined, and that the other 2 versions is probably inlined.
No that is not true. I guess you are thinking of the exception that
templates have from the normal one definition rules for functions and
classes. But that''s an entirely seperate issue.
Are theese approaches correct?
They are all correct.
Which should be preferred?
The second
Are there any other better way?




有什么不对用第二种方法?为什么它甚至是一个问题?


john



What''s wrong with the second method? Why is it even an issue?

john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

新闻:2k ************ @ uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2k************@uni-berlin.de...

卡尔Ribbegaardh" < CA ********************* @ hotmail.com>在消息中写道
新闻:2k ************ @ uni-berlin.de ...

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in message
news:2k************@uni-berlin.de...
我可以使用哪些其他c ++结构代替#define执行
几个函数?
例如:
#define DO_STUFF doThis(); doThat();

我猜我可以使用模板函数,内联函数
What other c++ constructs can I use instead of #define for executing a
couple of functions?
Example:

#define DO_STUFF doThis(); doThat();

I''d guess that I can either use a template function, an inlined function


内联静态方法。

//命名空间MyUtils
{
模板<>
void doStuff()
{
doThis( );
doThat();
}
}
// 2
命名空间MyUtils
{
inline void doStuff( )
{
doThis();
doThat();
}
}

// 3
类MyUtils
{
public:
static inline void doStuff()const
{
doThis();
doThat();
}
}

我*相信*模板版本总是内联的,并且
an inlined static method.

//1
namespace MyUtils
{
template<>
void doStuff()
{
doThis();
doThat();
}
}

//2
namespace MyUtils
{
inline void doStuff()
{
doThis();
doThat();
}
}

//3
class MyUtils
{
public:
static inline void doStuff() const
{
doThis();
doThat();
}
}

I *believe* that the template version always is inlined, and that the


其他

2版本可能是内联的。
2 versions is probably inlined.



不,这不是真的。我想你正在考虑
模板与函数和
类的正常定义规则之间存在的异常。但这是一个完全独立的问题。



No that is not true. I guess you are thinking of the exception that
templates have from the normal one definition rules for functions and
classes. But that''s an entirely seperate issue.

这些方法是否正确?



它们都是正确的。



They are all correct.

哪个应该首选?



第二个



The second

还有其他更好的方法吗?
Are there any other better way?



第二种方法有什么问题?为什么它甚至是一个问题?



What''s wrong with the second method? Why is it even an issue?




问题是我不知道*。

我目前只是在猜测/相信。 :)


所以第一个版本没有内联?为什么我认为它是,我已经读取了模板根据定义内联的读取行。但是我可能已经解释/读取/记住了错误的
。任何详细说明为什么或为什么没有

将非常感激。


谢谢! :)



The issue is that I dont *know*.
I''m currently just guessing/believing. :)

So the 1st version isn''t inlined? Why I thought it would be, is that I''ve
read lines like "templates are inlined by definition" but I might have
interpreted/read/remembered it wrong. Any elaboration on why or why not
would be much appreciated.

Thanks! :)




" Carl Ribbegaardh" < ca ***************************************** @ hotma il.com>

在留言新闻中写道:2k ************ @ uni-berlin.de ...

"Carl Ribbegaardh" <ca*****************************************@hotma il.com>
wrote in message news:2k************@uni-berlin.de...

约翰哈里森" <乔************* @ hotmail.com>在消息中写道
新闻:2k ************ @ uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2k************@uni-berlin.de...

Carl Ribbegaardh < CA ********************* @ hotmail.com>写在
消息新闻:2k ************ @ uni-berlin.de ...

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in message news:2k************@uni-berlin.de...
我可以使用哪些其他c ++结构代替#define执行
几个函数?
例如:
#define DO_STUFF doThis(); doThat();

我猜我可以使用模板函数,内联
What other c++ constructs can I use instead of #define for executing a
couple of functions?
Example:

#define DO_STUFF doThis(); doThat();

I''d guess that I can either use a template function, an inlined


函数或

内联静态方法。

//命名空间MyUtils
{
模板<>
void doStuff()
{
doThis( );
doThat();
}
}
// 2
命名空间MyUtils
{
inline void doStuff( )
{
doThis();
doThat();
}
}

// 3
类MyUtils
{
public:
static inline void doStuff()const
{
doThis();
doThat();
}
}

我*相信*模板版本总是内联的,而另一个
an inlined static method.

//1
namespace MyUtils
{
template<>
void doStuff()
{
doThis();
doThat();
}
}

//2
namespace MyUtils
{
inline void doStuff()
{
doThis();
doThat();
}
}

//3
class MyUtils
{
public:
static inline void doStuff() const
{
doThis();
doThat();
}
}

I *believe* that the template version always is inlined, and that the other
2版本可能是内联的。
2 versions is probably inlined.


模板与函数和
类的正常定义规则之间存在的异常。但这是一个完全独立的问题。



No that is not true. I guess you are thinking of the exception that
templates have from the normal one definition rules for functions and
classes. But that''s an entirely seperate issue.

这些方法是否正确?



它们都是正确的。



They are all correct.

哪个应该首选?



第二个



The second

还有其他更好的方法吗?
Are there any other better way?



第二种方法有什么问题?为什么它甚至是一个问题?



What''s wrong with the second method? Why is it even an issue?



问题是我不知道*。
我目前只是在猜测/相信。 :)



The issue is that I dont *know*.
I''m currently just guessing/believing. :)




好​​的方法二是使用一个函数来调用另外两个函数。它是完美的

解决常见问题的通用编程。其他方法介绍

额外语言功能,为简单的

问题创建一个更复杂的解决方案。

所以第一个版本不是内联?为什么我认为它是,我已经读过像模板按照定义内联这样的行。但我可能已经解释/阅读/记住错了。任何详细说明为什么或为什么没有
将非常感激。



Well method two is using one function to call two others. It''s perfectly
common programming to solve a common problem. The other methods introduce
extra language features to create a more complex solution to a simple
problem.
So the 1st version isn''t inlined? Why I thought it would be, is that I''ve
read lines like "templates are inlined by definition" but I might have
interpreted/read/remembered it wrong. Any elaboration on why or why not
would be much appreciated.




如果你读到它是错误的。正如我所说,我认为你可能已经读过

模板函数通常会进入头文件,这使得它们有点像

内联函数(通常也会出现在头文件中) ,但这是与模板编译方式有关的技术问题。它与模板函数调用本身是否内联无关。




john



If you read that it was wrong. As I said I think you might have read that
template functions usually go in header files which makes them a little like
inline functions (which also usually go in header files), but this is for
technical issues to do with how templates are compiled. It has nothing to do
with whether the template function call itself is inlined.

john


这篇关于#define的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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