如何为专用于 std::enable_if 的类定义类外函数
How to define out of class functions for a class specialized using std::enable_if
我有一个叫做图的类的专门化,只有当输入是特定类型时才会启用。我无法为该类中的函数定义类外定义。这个问题不同于堆栈溢出的其他一些问题,其中 sfinae 发生在成员函数上。在这里,我希望在类上启用,并且只在类外为该类定义一个普通成员函数。
注意 - 有多个具有不同容器类型的图形类。这只是一个例子。
我希望能够在此类之外定义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
}
观看现场演示。
相关文章:
- c++:定义if语句中的模板
- 获取 if 语句以检查定义的宏
- 如何在C++中为 if 和 else 语句定义新行为
- 如何定义此"if block"中其他无效输入的值,以便在c ++中将字符串转换为对象?
- 是否应该定义 #if 中使用的宏?
- c if语句:第一个条件取代第二个ub或定义良好
- 在"if"条件中定义 fstream
- C 使用功能定义中的if/其他语句以返回最小的数字
- 为什么我不能在 If 语句中定义多个变量以相互相等?
- C多行宏问题:为什么不使用if(1){..}而不是do{..}while(0)在多行宏定义中
- 奇怪的错误:标签'loopend'使用但未定义," if "之前应为非限定 id("goto"和" '}' token "错误相同
- 如何在 if 条件中区分零和未定义
- 如何在if/else序列中定义未知类型的全局变量
- C++对方法和What-if所有东西都转到.h的未定义引用
- 为什么if语句中未定义的参数不会导致segfault或其他错误
- 自定义C++谓词并查找if
- 获取 while、if 和 for 语句的用户定义类的对象值
- 0 && 定义(_LP64)#if 如何为真?
- 对函数的未定义引用,当在Windows上使用if定义时(在Linux上可以)
- VS2010上不区分大小写的字符串比较-为每个版本的VS播放#if定义的语句