繁体   English   中英

如何为使用std :: enable_if专门化的类定义类外函数

[英]How to define out of class functions for a class specialized using std::enable_if

我有一个叫做graph的类的专业化,只有在输入是特定类型时才启用。 我无法为该类内部的函数定义类外定义。 这个问题与堆栈溢出的其他一些问题不同,在堆栈溢出中,sfinae发生在成员函数上。 在这里,我要在类上启用if,并在类之外为该类定义一个普通的成员函数。

注意-有多个具有不同容器类型的图类。 这只是一个例子。

我希望能够在此类之外定义graph_func

template<typename ContainerType,
    std::enable_if<std::is_same<ContainerType, Eigen::MatrixXd>::value, int>::type = 0>
class graph
{
    .
    .
    .
    void graph_func() const;
}

我尝试了这个,但是我得到一个错误,它没有引用任何类

template <typename ContainerType>
void graph<ContainerType,  std::enable_if<std::is_same<Graph, Eigen::MatrixXd>::value, int>::type>::graph_func() const
{
  // definition
}

请注意,参数列表中的std::enable_if<..., int>::type 是非类型模板参数

template<typename ContainerType,
    typename std::enable_if<std::is_same<ContainerType, Eigen::MatrixXd>::value, int>::type = 0>
class graph
{
    void graph_func() const;
};

您需要将该类型的 (在这里我们仅将其命名为_ )传递给参数列表:

template <typename ContainerType,
    typename std::enable_if<std::is_same<ContainerType, Eigen::MatrixXd>::value, int>::type _>
void graph<ContainerType, _>::graph_func() const
{
  // definition
}

观看现场演示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM