使用enable_if作为模板参数的模板类方法定义 [英] Template class methods definition with enable_if as template parameter

查看:49
本文介绍了使用enable_if作为模板参数的模板类方法定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候问过这个问题,其中

I asked this question earlier where a solution was presented. The solution is great as far as the question is concerned, but now I am confused on how I would define the methods outside of the class i.e. I would like to define the methods in an .inl file. What would be the syntax in this case?

需要明确的是,对于模板类,方法定义为:

Just to be clear, for a template class, the method definition will be:

template <typename T>
struct Foo
{
  Foo();
};

// C-tor definition
template <typename T>
Foo<T>::Foo()
{
}

如何使用 enable_if 作为参数之一为模板类定义方法?

How would I define methods for the template class with enable_if as one of the parameters?

template <typename Policy, enable_if< is_base<BasePolicy, Policy>::value >::type >
struct Foo
{
  Foo();
};

// C-tor definition -- ???

推荐答案

从外观上,您想按照以下方式做点事情:

From the looks of it, you want to do something along the lines of this:

template <typename Policy,
          typename = typename std::enable_if<std::is_base_of<BasePolicy, Policy>::value>::type >
struct Foo;

template <typename Policy>
struct Foo<Policy> {
    Foo();
};

template <typename Policy>
Foo<Policy>::Foo() {
}

这在某些地方偷偷地利用了默认参数:不要感到困惑,在多个位置都有一个隐式的 void .

This sneakily takes advantage of the default argument in a few places: don't get confused, there is an implicit void sitting in several locations.

这篇关于使用enable_if作为模板参数的模板类方法定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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