Exx*_*xul 29 c++ templates template-meta-programming
有人可以向我解释一下,为什么第一个模板元编程方式将进入无限循环,而第二个模板正确运行。
#include <iostream>
using namespace std;
template<int N, int M>
struct commondivs {
static const int val = (N<M) ? commondivs<N,(M-N)>::val : commondivs<(N-M),M>::val;
};
template<int N>
struct commondivs<N,N> {
static const int val = N;
};
int commondiv(int N, int M){
if(N==M){
return N;
}
return (N<M)?commondiv(N,(M-N)):commondiv((N-M),M);
}
int main() {
cout << commondivs<9,6>::val << endl;
cout << commondiv(9,6) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Hol*_*Cat 39
(N<M) ? commondivs<N,(M-N)>::val : commondivs<(N-M),M>::val
Run Code Online (Sandbox Code Playgroud)
This line causes instantiation of both commondivs<N,(M-N)>::val
and commondivs<(N-M),M>::val
, even if the condition is known at compile time and one of the branches will never be taken.
Replace ? :
with std::conditional_t
, which doesn't have this limitation:
static const int val = std::conditional_t<N < M, commondivs<N,(M-N)>, commondivs<(N-M),M>>::val;
Run Code Online (Sandbox Code Playgroud)
son*_*yao 15
问题在于条件运算符的所有操作数都将被求值,因此,commondivs<N,(M-N)>
和都将被commondivs<(N-M),M>
实例化,并对其val
求值,然后导致递归模板实例化。
您可以应用constexpr if并将其放在constexpr
static
成员函数中。
如果值为
true
,则丢弃statement-false(如果存在),否则,丢弃statement-true。
template<int N, int M>
struct commondivs {
constexpr static int get_val() {
if constexpr (N<M) return commondivs<N,(M-N)>::val; // if true, the else part won't be evaluated
else return commondivs<(N-M),M>::val; // vice versa
}
static const int val = get_val();
};
Run Code Online (Sandbox Code Playgroud)
三元运算符不像if constexpr
:当编译器看到它时,它必须为两个分支生成代码。换句话说,实例化一个模板commondivs<M, N>
,编译器实例都模板commondivs<N, M - N>
和commondivs<N - M, M>
。
与此相反,commondiv(N, M - N)
和commondiv(N - M, M)
被转换为两个函数调用。当实际调用该函数时,将决定采用哪一个。
加成。
HolyBlackCat通过给出了一个解决方案std::conditional_t
。这是另一个:
template<int N, int M>
struct commondivs {
static constexpr int min = (N < M) ? N : M;
static constexpr int max = (N < M) ? M : N;
static constexpr int val = commondivs<min, max - min>::val;
};
template<int N>
struct commondivs<N, N> {
static constexpr int val = N;
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2401 次 |
最近记录: |