C++ C+中的错误+;计算阶乘的17倍表达式
下面的程序给出了g++和clang中的错误C++ C+中的错误+;计算阶乘的17倍表达式,c++,c++17,C++,C++17,下面的程序给出了g++和clang中的错误 #include <iostream> #include <utility> using namespace std; //Since C++17 one can use fold expression to calculate factorial: template <class T, T N, class I = std::make_integer_sequence<T, N>> struct f
#include <iostream>
#include <utility>
using namespace std;
//Since C++17 one can use fold expression to calculate factorial:
template <class T, T N, class I = std::make_integer_sequence<T, N>>
struct factorial;
template <class T, T N, T... Is>
struct factorial<T,N,std::index_sequence<T, Is...>> {
static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};
int main(int argc, char** argv) {
std::cout << factorial<int, 5>::value << std::endl;
return 0;
}
#包括
#包括
使用名称空间std;
//由于C++17,可以使用折叠表达式计算阶乘:
模板
结构因子;
模板
结构阶乘{
静态constexpr T值=(静态_cast(1)*…*(Is+1));
};
int main(int argc,字符**argv){
std::cout您对结果对象使用了错误的类型
是只接受一组std::size\t
的模板。它不接受类型参数,因为类型是固定的。它是std::integer\u序列的别名模板
因此,试图将类型指定为std::index_序列中的第一个参数是错误的
您想要的是更通用的类型traitstd::integer\u sequence
,使您专门化到这个
template <class T, T N, T... Is>
struct factorial<T,N,std::integer_sequence<T, Is...>> {
static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};
模板
结构阶乘{
静态constexpr T值=(静态_cast(1)*…*(Is+1));
};
error: template argument for non-type template parameter must be an expression
struct factorial<T,N,std::index_sequence<T, Is...>> {
^
D:\Programs\msys64\mingw64\include\c++\10.1.0\utility:344:22: note: template parameter is declared here
template<size_t... _Idx>
^
main.cpp:32:18: error: implicit instantiation of undefined template 'factorial<int, 5, std::integer_sequence<int, 0, 1, 2, 3, 4> >'
std::cout << factorial<int, 5>::value << std::endl;
^
main.cpp:22:8: note: template is declared here
struct factorial;
^
template <class T, T N, T... Is>
struct factorial<T,N,std::integer_sequence<T, Is...>> {
static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};