Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于封闭类模板参数有条件地定义嵌套类_C++_C++11_Templates - Fatal编程技术网

C++ 基于封闭类模板参数有条件地定义嵌套类

C++ 基于封闭类模板参数有条件地定义嵌套类,c++,c++11,templates,C++,C++11,Templates,只有当矩阵是平方的(M==N)时,它才具有LU分解。有没有一种简单的方法可以禁用类lu和方法luFactorizationiffM=N下面是什么 template<int M, int N> class matrix { // lots and lots of operators and stuff // ... class lu { // ... } lu luFactorization() { // .

只有当矩阵是平方的(
M==N
)时,它才具有LU分解。有没有一种简单的方法可以禁用
类lu
和方法
luFactorization
iff
M=N下面是什么

template<int M, int N>
class matrix {

    // lots and lots of operators and stuff
    // ...

    class lu {
        // ...
    }

    lu luFactorization() {
        // ...
    }

}
模板
类矩阵{
//很多很多的操作员和东西
// ...
鲁类{
// ...
}
lu因子分解(){
// ...
}
}
定义“简单”。)


模板部分专业化确实有效:

template <int M, int N>
class matrix {
    // class lu, function luFactorization *not* defined
};

template <int N>
class matrix<N,N> {
    // class lu, function luFactorization defined
    class lu { };
    lu luFactorisation() { /* ... */ }
};
模板
类矩阵{
//类lu,函数luFactorization*未定义*
};
模板
类矩阵{
//类lu,定义了函数lu分解
类lu{};
lu因子分解(){/*…*/}
};

如果两种变体都有很多行李,您可以将部分或全部行李移动到一个公共超类。您还可以考虑编写<代码> LU>代码>和<代码> LoFultudiue<代码>非成员模板。

您可以使用函数>模板部分专门化<代码>矩阵< /代码>?@某个程序员,我不知道如何。试图理解您发布的链接。@davmac
matrix
带来了很多麻烦(操作员等)。有可能在不重复代码的情况下对其进行部分专门化吗?@Museful可能会将包袱移到共享基类。一个细节是,OP要求在M==N时定义这些类,而您的评论则表明相反,编译器是否看到两个定义,例如,
matrix
然后选择最新的一个?@Museful我相信它比这更复杂-这不是首先定义哪个,而是一个是另一个的(部分)专门版本。专业版本将始终是首选。