编译时内省 [英] Compile-time introspection

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

问题描述

有没有办法在编译时基于

a结构的数据成员生成一系列语句?


我有一个函数可以反转任何数据结构的字节顺序:


///反转数据结构的字节顺序就地。

模板< typename T>

void reverseEndian(T&);


使用boost,可以为所有POD

类型提供默认实现(即简单地反转数据项表示中的字节。


直到今天,我故意没有提供结构的实现,

强迫函数的用户为每个

结构提供实现,例如:


struct S

{

int a;

浮动b;

双c;

};


模板< void reverseEndian(S& s)

{

//功能体 - 可以这个b自动化?

reverseEndian<>(sa);

reverseEndian<>(sb);

reverseEndian<>(sc) ;

}


是否可以自动生成专业化的内容?

这需要内省(语言特征) ,而不是禅宗哲学),

到目前为止我还没有找到任何办法,便携式或其他方式。


-dr

Is there a way to generate a series of statements based on the data members of
a structure at compile time?

I have a function that reverses the endianness of any data structure:

/// Reverse the endianness of a data structure "in place".
template <typename T>
void reverseEndian(T&);

Using boost, it is possible to provide the default implementation for all POD
types (i.e. simply reverse the bytes in the data item''s representation).

Until today, I have purposely not provided the implementation for structures,
forcing the user of the function to provide an implementation for each
structure, for example:

struct S
{
int a;
float b;
double c;
};

template <void reverseEndian(S& s)
{
// Function body -- can this be automated?
reverseEndian<>(s.a);
reverseEndian<>(s.b);
reverseEndian<>(s.c);
}

Is it possible to automatically generate the contents of the specialization?
This calls for introspection (the language feature, not the Zen philosophy),
and so far I have not found any way to do it, portable or otherwise.

-dr

推荐答案

Dave Rahardja写道:
Dave Rahardja wrote:

有没有办法基于此生成一系列语句编译时

a结构的数据成员?


我有一个函数可以反转任何数据结构的字节顺序:


///在适当的位置反转数据结构的字节顺序。

模板< typename T>

void reverseEndian(T&);


使用boost,可以提供默认的实现适用于所有POD

类型(即简单地反转数据项表示中的字节。


直到今天,我故意没有提供结构的实现,

强迫用户为每个

结构提供实现的功能,例如:


struct S

{

int a;

浮动b;

双c;

};


模板< void reverseEndian(S& s)

{

//功能体 - 可以自动化吗?

reverseEndian<> (sa);

reverseEndian<>(sb);

reverseEndian<>(sc);

}


是否可以自动生成专业化的内容?

这需要内省(语言功能,而不是禅宗哲学),

到目前为止,我还没有找到任何方法,便携式或其他方式。
Is there a way to generate a series of statements based on the data members of
a structure at compile time?

I have a function that reverses the endianness of any data structure:

/// Reverse the endianness of a data structure "in place".
template <typename T>
void reverseEndian(T&);

Using boost, it is possible to provide the default implementation for all POD
types (i.e. simply reverse the bytes in the data item''s representation).

Until today, I have purposely not provided the implementation for structures,
forcing the user of the function to provide an implementation for each
structure, for example:

struct S
{
int a;
float b;
double c;
};

template <void reverseEndian(S& s)
{
// Function body -- can this be automated?
reverseEndian<>(s.a);
reverseEndian<>(s.b);
reverseEndian<>(s.c);
}

Is it possible to automatically generate the contents of the specialization?
This calls for introspection (the language feature, not the Zen philosophy),
and so far I have not found any way to do it, portable or otherwise.



我不知道d的手段在这个问题上,当遇到这种问题时,我倾向于使用代码生成。


-

Ian Collins。

I don''t know of a means of doing this, when faced with this kind of a
problem, I tend to resort to code generation.

--
Ian Collins.


Dave Rahardja写道:
Dave Rahardja wrote:

有没有办法基于生成一系列语句编译时

a结构的数据成员?


我有一个函数可以反转任何数据结构的字节顺序:


///反转数据结构的字节顺序就地。

模板< typename T>

void reverseEndian(T&);


使用boost,可以为所有POD

类型提供默认实现(即简单地反转数据项表示中的字节。


直到今天,我故意没有提供结构的实现,

强迫用户为每个

结构提供实现的功能,例如:


struct S

{

int a;

浮动b;

双c;

};


模板< void reverseEndian(S& s)

{

//功能体 - 可以自动化吗?

reverseEndian<> (sa);

reverseEndian<>(sb);

reverseEndian<>(sc);

}


是否可以自动生成专业化的内容?

这需要内省(语言功能,而不是禅宗哲学),

到目前为止,我还没有找到任何办法,便携式或其他方式。


-dr
Is there a way to generate a series of statements based on the data members of
a structure at compile time?

I have a function that reverses the endianness of any data structure:

/// Reverse the endianness of a data structure "in place".
template <typename T>
void reverseEndian(T&);

Using boost, it is possible to provide the default implementation for all POD
types (i.e. simply reverse the bytes in the data item''s representation).

Until today, I have purposely not provided the implementation for structures,
forcing the user of the function to provide an implementation for each
structure, for example:

struct S
{
int a;
float b;
double c;
};

template <void reverseEndian(S& s)
{
// Function body -- can this be automated?
reverseEndian<>(s.a);
reverseEndian<>(s.b);
reverseEndian<>(s.c);
}

Is it possible to automatically generate the contents of the specialization?
This calls for introspection (the language feature, not the Zen philosophy),
and so far I have not found any way to do it, portable or otherwise.

-dr



有al在C ++中进行内省的其他方法。到目前为止

最便携的是使用宏。这个想法是,当你定义类时,你也会将普通的宏扩展为代码

,这将有助于你进行内省。这是一个简单的例子:


class A

{

public:

// your your regualr C ++到这里

void foo();

int bar;

public:

// introspection via macros

REFLECT_MEM_FUNC(foo,......);

REFLECT_MEM_VAR(bar,...);

};


但我真的很讨厌这种方法,因为当我读一堂课时,

它只是因为那些而导致课程长度增加一倍

手动内省线。此外,它容易出现用户错误。喜欢

,如果你拼错了东西或省略了什么。


如果你写了普通的C ++并且某种程度上,不管怎么说,这不是很酷吗

C ++语言本身创建了必要的数据?这是目前正在由C ++ LWG讨论的
,并且建议已经是b
。不幸的是,在我们得到关于

这个问题的答案之前还需要一段时间,因为它仍然标有打开字样。 LWG的状态。


然后有一些才华横溢的人来救援:

http://seal-reflex.web.cern.ch/seal-reflex/
< br $> b $ b祝你好运!

There are a lot of ways to do introspection in C++. So far
the most portable one is to use macros. The idea is, as you
define classes you also put plain Macros that expand to code
that will help you do introspection. Here is a quick example:

class A
{
public:
// your regualr C++ goes here
void foo();
int bar;
public:
// introspection via macros
REFLECT_MEM_FUNC( foo, ...... );
REFLECT_MEM_VAR( bar, ... );
};

But I honestly hate this approach since when I go read a class,
it doubles up the length of the class just because of those
manual introspection lines. Plus it is prone to user error. Like
if you misspelled something or omitted something.

Wouldn''t it be cool if you wrote regular C++ and somehow, someway
the C++ language created the necessary data by itself? This is
currently being discussed by the C++ LWG and proposals have been
made. Unfortunately, it will be a while until we get an answer about
this issue since it is still labeled "Open" status by the LWG.

And then there are some talented people who come to the rescue:

http://seal-reflex.web.cern.ch/seal-reflex/

Good Luck!




BTW,二读时,你想做编译时内省:


我会考虑大量使用模板元编程来实现这一点。

如果这对你来说太异国情调了,那么运行时内省可能会很好。 >
替代方案。 (我最初发布的东西。)


祝你好运!

Piyo写道:

BTW, on second reading, you wanted to do Compile-time introspection:

I would look into heavy use of template metaprogramming to do this.
If that is too exotic for you, run-time introspection could be a good
alternative for you. (The stuff I originally posted.)

Good Luck!
Piyo wrote:

Dave Rahardja写道:
Dave Rahardja wrote:

>有没有办法根据数据生成一系列语句
成员编译时的结构时间?

我有一个函数可以反转任何数据结构的字节顺序:
///反转数据结构的字节顺序就地。
template< typename T>
void reverseEndian(T&);

使用boost,可以提供
所有POD
类型的默认实现(即简单地反转数据项表示中的字节。

直到今天,我故意没有提供
结构的实现,强迫功能的用户为每个
结构提供实现,例如:

struct S
{
int a;
浮动b;
d ouble c;
};

模板< void reverseEndian(S& S& s)
//功能体 - 这可以自动化吗?
reverseEndian<>(sa);
reverseEndian<>(sb);
reverseEndian<>(sc);
}

是否可以自动生成
专业化的内容?
这需要内省(语言功能,不是禅宗哲学),
到目前为止我还没有找到任何办法,便携式或其他方式。

-dr
>Is there a way to generate a series of statements based on the data
members of
a structure at compile time?

I have a function that reverses the endianness of any data structure:

/// Reverse the endianness of a data structure "in place".
template <typename T>
void reverseEndian(T&);

Using boost, it is possible to provide the default implementation for
all POD
types (i.e. simply reverse the bytes in the data item''s representation).

Until today, I have purposely not provided the implementation for
structures,
forcing the user of the function to provide an implementation for each
structure, for example:

struct S
{
int a;
float b;
double c;
};

template <void reverseEndian(S& s)
{
// Function body -- can this be automated?
reverseEndian<>(s.a);
reverseEndian<>(s.b);
reverseEndian<>(s.c);
}

Is it possible to automatically generate the contents of the
specialization?
This calls for introspection (the language feature, not the Zen
philosophy),
and so far I have not found any way to do it, portable or otherwise.

-dr



在C ++中有很多方法可以进行内省。到目前为止

最便携的是使用宏。这个想法是,当你定义类时,你也会将普通的宏扩展为代码

,这将有助于你进行内省。这是一个简单的例子:


class A

{

public:

// your your regualr C ++到这里

void foo();

int bar;

public:

// introspection via macros

REFLECT_MEM_FUNC(foo,......);

REFLECT_MEM_VAR(bar,...);

};


但我真的很讨厌这种方法,因为当我读一堂课时,

它只是因为那些而导致课程长度增加一倍

手动内省线。此外,它容易出现用户错误。喜欢

,如果你拼错了东西或省略了什么。


如果你写了普通的C ++并且某种程度上,不管怎么说,这不是很酷吗

C ++语言本身创建了必要的数据?这是目前正在由C ++ LWG讨论的
,并且建议已经是b
。不幸的是,在我们得到关于

这个问题的答案之前还需要一段时间,因为它仍然标有打开字样。 LWG的状态。


然后有一些才华横溢的人来救援:

http://seal-reflex.web.cern.ch/seal-reflex/
< br $> b $ b祝你好运!


There are a lot of ways to do introspection in C++. So far
the most portable one is to use macros. The idea is, as you
define classes you also put plain Macros that expand to code
that will help you do introspection. Here is a quick example:

class A
{
public:
// your regualr C++ goes here
void foo();
int bar;
public:
// introspection via macros
REFLECT_MEM_FUNC( foo, ...... );
REFLECT_MEM_VAR( bar, ... );
};

But I honestly hate this approach since when I go read a class,
it doubles up the length of the class just because of those
manual introspection lines. Plus it is prone to user error. Like
if you misspelled something or omitted something.

Wouldn''t it be cool if you wrote regular C++ and somehow, someway
the C++ language created the necessary data by itself? This is
currently being discussed by the C++ LWG and proposals have been
made. Unfortunately, it will be a while until we get an answer about
this issue since it is still labeled "Open" status by the LWG.

And then there are some talented people who come to the rescue:

http://seal-reflex.web.cern.ch/seal-reflex/

Good Luck!


这篇关于编译时内省的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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