C++模板编程——延迟函数调用
我正在寻找解决以下问题的优雅方法。我有一个用于延迟函数调用的任务结构。
template <typename T> struct Task1
{
T Arg1;
Delegate<T> TaskDelegate;
};
我遇到的问题是:
Task1<const Foo&> MyTask;
这将导致参数被保存为常量引用。有谁知道解决这个问题的好解决方案?我可以强制执行诸如委托签名始终采用 const& 等规则。 params 但这似乎是有限制的。我总是可以有两个任务结构(一个用于引用,一个用于值),但这看起来很糟糕。
另一个解决方案是创建以下内容:
template <typename T1, typename T2> struct Task1
{
T2 Arg1;
Delegate<T1> TaskDelegate;
};
是否可以将 T2 默认为与 T1 相同的类型?这样,每当我有方法值签名时,我就不需要额外的模板参数。
编辑: 该模板用于多线程任务调度程序。这是一个例子:
void MyClass::Populate(const std::string& instrText);
CTaskScheduler::Schedule(Task1<const std::string&>(this, &MyClass::Popluate, "MyString"));
I am looking for an elegant solution to the following problem. I have a task struct that I use for deferred function calls.
template <typename T> struct Task1
{
T Arg1;
Delegate<T> TaskDelegate;
};
The problem I'm having is this:
Task1<const Foo&> MyTask;
This will result in the parameter being held as a const reference. Does anyone know a nice solution to get round this? I could enforce rules such as the delegate signature always taking const& params but this seems restrictive. I could always have two task structs (one for ref and one for value) but this seems nasty.
The other solution would be to create the following:
template <typename T1, typename T2> struct Task1
{
T2 Arg1;
Delegate<T1> TaskDelegate;
};
Is there anyway to default T2 to be the same type as T1? That way whenever I have a method value signature I don't need to have the additional template params.
EDIT:
The template is used for a multithreaded task scheduler. Here is an example:
void MyClass::Populate(const std::string& instrText);
CTaskScheduler::Schedule(Task1<const std::string&>(this, &MyClass::Popluate, "MyString"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 boost 或即将推出的标准中查看
function<>
的实现。事实上,您可以使用function<>
。我认为解决方案(在 C++0x 之前)总是存储参数的副本,如果用户想要引用语义,他们可以使用引用包装器。至于如何获取一个值,你可以看一下一些简单的元函数来删除
const
或&
:对于
const
也是如此。You could take a look at the implementation of
function<>
either in boost or the upcoming standard. As a matter of fact, you can just usefunction<>
. I think that the solution there was (before C++0x) to always store a copy of the arguments, if the user wants reference semantics they can use a reference wrapper.As to how to get to a value, you can take a look at some simple metafunction to remove
const
or&
:Similarly for
const
.您可以使用 boost.type_traits库使用
boost::remove_const
删除参数的常量性。You can use the boost.type_traits library to remove the const-ness of the parameter using
boost::remove_const
.除了 boost::type_traits 之外,还有一个 boos::call_traits专门为处理此类问题而构建的库。它还提供了避免引用问题的引用的机制。
In addition to boost::type_traits, there is a boos::call_traits library specifically built to handle problems like this. It also provides mechanisms to avoid the references of references problem.
boost::remove_const 在这种情况下应该对您有帮助:
或者,如果您对 const 类型使用模板专业化,则可以避免使用 boost:(
警告:未经测试)
boost::remove_const should help you in this case:
Alternatively, you can avoid using boost if you use template specialization for const types:
(Warning: untested)