我怎样才能转换这个c++模板函数是 C 语言的替代品吗?
我正在将一个小型 C++ 库的部分内容转换为 C (gcc)。这样做时,我想将以下模板函数转换为宏(为了便于阅读,删除了注释)。 CpuReadWriteFence() 是我已成功转换为宏的另一个函数。
template<typename T>
static inline T AtomicLoadAcquire(T const* addr)
{
T v = *const_cast<T const volatile*>(addr);
CpuReadWriteFence();
return v;
}
由于 C 中没有模板,我要么使用函数,要么使用宏。海湾合作委员会提供了 方便的扩展类型。也许我可以用 void* 来做到这一点?如果是这样怎么办?
到目前为止我所拥有的是:
#define AtomicLoadAcquire(addr) \
({ typeof (addr) v = *(volatile typeof (addr) *)(addr); CpuReadWriteFence(); })
但是,这不允许我这样做:
int x = AtomicStoreRelease(&bla);
我将如何解决这个问题?
I'm converting parts of a small C++ library to C (gcc). In doing so I'm wanting to convert the following template function to a macro (comments removed for readibility). CpuReadWriteFence() is another function that I've converted to a macro successfully.
template<typename T>
static inline T AtomicLoadAcquire(T const* addr)
{
T v = *const_cast<T const volatile*>(addr);
CpuReadWriteFence();
return v;
}
Since there are no templates in C I'm either using functions or macros. GCC provides a
convenient typeof extension. Perhaps I could do it with void*? if so how?
What I have so far is this:
#define AtomicLoadAcquire(addr) \
({ typeof (addr) v = *(volatile typeof (addr) *)(addr); CpuReadWriteFence(); })
However, that won't allow me to do this:
int x = AtomicStoreRelease(&bla);
How would I get around this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能使用宏返回值。试试这个:
You can't return a value with a macro. Try this:
你几乎做对了。 GCC “表达式中的语句和声明” 扩展不必返回 void。
因此您可以将宏定义为:
注意末尾的
v;
宏。这就是魔法的来源。另请注意,第一个
typeof
将*addr
作为参数,并且volatile typeof(addr)
后面没有星号。这些是一些与您的主要问题无关的小错误。You almost got it right. The GCC "statements and declarations in expressions" extension does not have to return void.
So you can define your macro as:
Note the
v;
at the end of the macro. That's where the magic comes from.Also note that the first
typeof
takes*addr
as an argument and there is no star aftervolatile typeof(addr)
. Those were some minor bugs unrelated to your main problem.您可以将返回值添加为宏参数吗?类似这样的东西:
它很丑陋,但这将为您做到这一点:
翻译为:
这将完全如您所愿。
另一种选择(如miaout17所述)是在调用宏之前声明
x
,然后删除开头的“typeof(addr)
”,这样会更安全,恕我直言。Can you add the return value as a macro parameter? Something like that:
It's ugly but this will do this for you:
translated to:
Which would be exactly as you want it.
Other option (as mentioned by miaout17) is to declare
x
before calling the macro, and then remove the "typeof(addr)
" in the beginning, that would be safer, IMHO.在宏中传递类型可能是您能做的最好的事情:
Passing the type in the macro is probably the best you can do: