C++模板编程——延迟函数调用

发布于 2024-11-30 06:01:21 字数 813 浏览 0 评论 0原文

我正在寻找解决以下问题的优雅方法。我有一个用于延迟函数调用的任务结构。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何处潇湘 2024-12-07 06:01:21

您可以在 boost 或即将推出的标准中查看 function<> 的实现。事实上,您可以使用function<>。我认为解决方案(在 C++0x 之前)总是存储参数的副本,如果用户想要引用语义,他们可以使用引用包装器

至于如何获取一个值,你可以看一下一些简单的元函数来删除 const&

// Remove reference:
template <typename T>
struct remove_reference {
   typedef T type;
};
template <typename T>
struct remove_reference<T&> {
   typedef T type;
};

对于 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 use function<>. 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 &:

// Remove reference:
template <typename T>
struct remove_reference {
   typedef T type;
};
template <typename T>
struct remove_reference<T&> {
   typedef T type;
};

Similarly for const.

写给空气的情书 2024-12-07 06:01:21

您可以使用 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.

柠檬色的秋千 2024-12-07 06:01:21

除了 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 在这种情况下应该对您有帮助:

template <typename T> struct Task1
{
    typename boost::remove_const<T>::type Arg1;
    Delegate<T> TaskDelegate;
};

或者,如果您对 const 类型使用模板专业化,则可以避免使用 boost:(

template <typename T> struct Task1
{
    T Arg1;
    Delegate<T> TaskDelegate;
};

template <typename T> struct Task1<const T>
{
    T Arg1;
    Delegate<const T> TaskDelegate;
};

警告:未经测试)

boost::remove_const should help you in this case:

template <typename T> struct Task1
{
    typename boost::remove_const<T>::type Arg1;
    Delegate<T> TaskDelegate;
};

Alternatively, you can avoid using boost if you use template specialization for const types:

template <typename T> struct Task1
{
    T Arg1;
    Delegate<T> TaskDelegate;
};

template <typename T> struct Task1<const T>
{
    T Arg1;
    Delegate<const T> TaskDelegate;
};

(Warning: untested)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文