如果在纯C ++中是静态的? [英] static if in plain c++?
问题描述
简短的问题:
如何在纯c ++中实现c ++ 11中提出的static if
功能?
Problem in short:
How could one implement static if
functionality, proposed in c++11, in plain c++ ?
历史和原始问题:
最近我想到了这样的问题.我需要具有类似接口的类Sender
History and original problem:
Recently I came up with a problem like this. I need a class Sender
with an interface like
class Sender
{
void sendMessage( ... );
void sendRequest( ... );
void sendFile( ... );
// lots of different send methods, not important actually
}
在某些情况下,我将需要创建一个 DoubleSender ,即该类的一个实例,该实例将两次调用其方法,即,当调用一个sendMessage(...)
方法时,消息必须发送两次.
In some cases I will need to create a DoubleSender, i.e. an instance of this class, which would call its methods twice, i.e. when calling, let's say, a sendMessage(...)
method, the same message has to be sent twice.
我的解决方案:
第一种方法:
拥有一个isDouble
成员,并在每个方法调用的最后进行检查
My solutions:
First approach:
Have an isDouble
member, and in the end of each method call make a check
sendMessage(...) { ... if( isDouble ) { sendMessage( ... ); }
好吧,我不希望这样,因为实际上我最近需要双重发布,并且对时间要求严格的部分中的这一部分代码将是98%被动的.
Well, I don't want this, because actually I will need double posting very recently, and this part of code in time-critical section will be 98% passive.
第二种方法:
从Sender
继承类DoubleSender
,并实现其方法,如:
Second approach:
Inherit a class DoubleSender
from Sender
, and implement its methods like:
void DoubleSender::sendMessage( ... )
{
Sender::sendMessage(...);
Sender::sendMessage(...);
}
这是可以接受的,但是会占用大量不愉快的代码(确实很多,因为有很多不同的send..
方法.
Well, this is acceptable, but takes much space of unpleasant code (really much, because there are lots of different send..
methods.
第三种方法:
想象一下我正在使用c ++ 11 :).然后,我可以使该类通用,并使用static if
根据tempalte参数生成代码的必要部分:
Third approach:
Imagine that I am using c++11 :). Then I can make this class generic and produce the necessary part of code according to tempalte argument using static if
:
enum SenderType { Single, Double };
template<SenderType T>
class Sender
{
void sendMessage(...)
{
// do stuff
static if ( T == Single )
{
sendMessage(...);
}
}
};
与以前的解决方案相比,它更短,更易于阅读,并且不会生成其他代码,并且...它是c ++ 11,很遗憾,我无法在我的工作中使用它.
This is shorter, easier to read than previous solutions, does not generate additional code and... it's c++11, which I unfortunately cannot use in my work.
所以,这是我的问题所在-如何在c ++中实现static if
模拟?
另外,对于任何其他有关如何解决原始问题的建议,我也将不胜感激.
预先感谢.
So, here is where I came to my question - how can I implement static if
analog in c++ ?
Also, I would appreciate any other suggestions about how to solve my original problem.
Thanks in advance.
推荐答案
引用@JohannesSchaubLitb
Quoting @JohannesSchaubLitb
使用可在gcc上运行的static_if可以做到:)
以某种有限的方式
with my static_if that works on gcc one can do it :)
in some limited fashion
(另请参见此处)
此技巧涉及C ++ 11中Lambda规范的特定GCC解释.这样,它很可能会成为违反标准的缺陷报告.这将导致该技巧在GCC的最新版本中不再起作用(在4.7中已经不起作用).
有关约翰内斯的更多详细信息,请参见下面的评论主题
See the comment thread below for some more details from Johanness
#include <iostream>
namespace detail {
template<bool C>
struct call_if { template<typename F> void operator<<(F) { } };
template<>
struct call_if<true> {
template<typename F>
void operator<<(F f) { f(); }
};
}
#define static_if(cond) detail::call_if<cond>() << [&]
template<bool C, typename T>
void f(T t) {
static_if(C) {
t.foo();
};
}
int main() {
f<false>(42);
}
这篇关于如果在纯C ++中是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!