C++ 编译时的基本因子列表:专门化错误
考虑以下代码(链接到):C++ 编译时的基本因子列表:专门化错误,c++,c++11,metaprogramming,variadic-templates,specialization,C++,C++11,Metaprogramming,Variadic Templates,Specialization,考虑以下代码(链接到): #包括 #包括 //因素清单 模板 结构因子{}; //声明 模板 结构因子分解; //初始专业化 模板 结构因式分解 { typedef typename std::conditional::type type; }; //值不能被2整除时的初始专门化 模板 结构因式分解 { typedef typename std::conditional::type type; }; //第一步复发后的专业化 模板 结构因式分解 { typedef typename std::c
#包括
#包括
//因素清单
模板
结构因子{};
//声明
模板
结构因子分解;
//初始专业化
模板
结构因式分解
{
typedef typename std::conditional::type type;
};
//值不能被2整除时的初始专门化
模板
结构因式分解
{
typedef typename std::conditional::type type;
};
//第一步复发后的专业化
模板
结构因式分解
{
typedef typename std::conditional::type type;
};
//递归专门化
模板
结构因式分解
{
typedef typename std::conditional::type>::type类型;
};
//最后递归步骤
模板
结构因式分解
{
typedef typename分解::type type;
};
//定稿
模板
结构因式分解
{
类型定义因子类型;
};
//主要
int main(){
typename分解::type x;
返回0;
}
该代码应列出一个数字的素数因子,不得重复:
例如,factorization::type
应该等于factors
但它失败了,出现以下错误:
prog.cpp: In instantiation of ‘struct factorization<4ll, 2ll, 2ll>’:
prog.cpp:36:79: required from ‘struct factorization<9ll, 2ll, 2ll>’
prog.cpp:18:67: required from ‘struct factorization<18ll>’
prog.cpp:67:28: required from here
prog.cpp:36:79: error: ambiguous class template instantiation for ‘struct factorization<2ll, 2ll, 2ll>’
typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
^
prog.cpp:32:8: error: candidates are: struct factorization<Value, Divisor, Prime>
struct factorization<Value, Divisor, Prime>
^
prog.cpp:41:8: error: struct factorization<Value, Divisor, Prime, Primes ...>
struct factorization<Value, Divisor, Prime, Primes...>
^
prog.cpp:53:8: error: struct factorization<Value, Value, Primes ...>
struct factorization<Value, Value, Primes...>
^
prog.cpp:36:79: error: invalid use of incomplete type ‘struct factorization<2ll, 2ll, 2ll>’
typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
^
prog.cpp:10:8: error: declaration of ‘struct factorization<2ll, 2ll, 2ll>’
struct factorization;
^
prog.cpp: In function ‘int main()’:
prog.cpp:67:30: error: invalid combination of multiple type-specifiers
typename factorization<18>::type x;
^
prog.cpp:67:36: error: invalid type in declaration before ‘;’ token
typename factorization<18>::type x;
^
prog.cpp:67:35: warning: unused variable ‘x’ [-Wunused-variable]
typename factorization<18>::type x;
^
Compilation error time: 0 memory: 0 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:67:30: error: ‘type’ in ‘struct factorization<18ll>’ does not name a type
typename factorization<18>::type x;
^
prog.cpp:67:36: error: invalid type in declaration before ‘;’ token
typename factorization<18>::type x;
^
prog.cpp:67:35: warning: unused variable ‘x’ [-Wunused-variable]
typename factorization<18>::type x;
^
prog.cpp:在“结构因式分解”的实例化中:
程序cpp:36:79:从“结构因式分解”中需要
程序cpp:18:67:从“结构因式分解”中需要
进度cpp:67:28:从这里开始需要
prog.cpp:36:79:错误:“结构因式分解”的类模板实例化不明确
类型名称分解::类型>::类型类型;
^
prog.cpp:32:8:错误:候选项为:结构因式分解
结构因式分解
^
程序cpp:41:8:错误:结构因式分解
结构因式分解
^
程序cpp:53:8:错误:结构因式分解
结构因式分解
^
程序cpp:36:79:错误:对不完整类型“结构因式分解”的使用无效
类型名称分解::类型>::类型类型;
^
程序cpp:10:8:错误:“结构因式分解”的声明
结构因子分解;
^
prog.cpp:在函数“int main()”中:
程序cpp:67:30:错误:多个类型说明符的组合无效
typename分解::type x;
^
prog.cpp:67:36:错误:在“;”之前的声明中的类型无效代币
typename分解::type x;
^
程序cpp:67:35:警告:未使用的变量“x”[-Wunused variable]
typename分解::type x;
^
编译错误时间:0内存:0信号:0
prog.cpp:在函数“int main()”中:
prog.cpp:67:30:错误:“结构因式分解”中的“类型”未命名类型
typename分解::type x;
^
prog.cpp:67:36:错误:在“;”之前的声明中的类型无效代币
typename分解::type x;
^
程序cpp:67:35:警告:未使用的变量“x”[-Wunused variable]
typename分解::type x;
^
如何解决这个问题?除非您省略了一些代码,否则就我所知,没有与分解相匹配的专门化。您的每个专门化似乎都假设至少有两个参数被传递到因子分解中
从您添加的注释来看,似乎您假设标记为“初始专门化”的专门化将捕获您只传入一个数字的情况。这不是真的。尝试运行
factorization
以查看其实际效果。如果这确实是您想要的,那么要么在您的专门化中省略2并保持主体不变,要么添加一个与单个数字简单匹配的最终专门化,并将type
设置为typename factorization::type
没有与factorization
匹配的专门化(据我所知),除非你省略了一些代码。您的每个专门化似乎都假设至少有两个参数被传递到因子分解中
从您添加的注释来看,似乎您假设标记为“初始专门化”的专门化将捕获您只传入一个数字的情况。这不是真的。尝试运行
factorization
以查看其实际效果。如果这确实是您想要的,那么要么在您的专门化中省略2并保持主体不变,要么添加一个与单个数字简单匹配的最终专门化,并将type
设置为typename factorization::type
没有与factorization
匹配的专门化(据我所知),除非你省略了一些代码。您的每个专门化似乎都假设至少有两个参数被传递到因子分解中
从您添加的注释来看,似乎您假设标记为“初始专门化”的专门化将捕获您只传入一个数字的情况。这不是真的。尝试运行
factorization
以查看其实际效果。如果这确实是您想要的,那么要么在您的专门化中省略2并保持主体不变,要么添加一个与单个数字简单匹配的最终专门化,并将type
设置为typename factorization::type
没有与factorization
匹配的专门化(据我所知),除非你省略了一些代码。您的每个专门化似乎都假设至少有两个参数被传递到因子分解中
从您添加的注释来看,似乎您假设标记为“初始专门化”的专门化将捕获您只传入一个数字的情况。这不是真的。尝试运行fact
prog.cpp: In instantiation of ‘struct factorization<4ll, 2ll, 2ll>’:
prog.cpp:36:79: required from ‘struct factorization<9ll, 2ll, 2ll>’
prog.cpp:18:67: required from ‘struct factorization<18ll>’
prog.cpp:67:28: required from here
prog.cpp:36:79: error: ambiguous class template instantiation for ‘struct factorization<2ll, 2ll, 2ll>’
typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
^
prog.cpp:32:8: error: candidates are: struct factorization<Value, Divisor, Prime>
struct factorization<Value, Divisor, Prime>
^
prog.cpp:41:8: error: struct factorization<Value, Divisor, Prime, Primes ...>
struct factorization<Value, Divisor, Prime, Primes...>
^
prog.cpp:53:8: error: struct factorization<Value, Value, Primes ...>
struct factorization<Value, Value, Primes...>
^
prog.cpp:36:79: error: invalid use of incomplete type ‘struct factorization<2ll, 2ll, 2ll>’
typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
^
prog.cpp:10:8: error: declaration of ‘struct factorization<2ll, 2ll, 2ll>’
struct factorization;
^
prog.cpp: In function ‘int main()’:
prog.cpp:67:30: error: invalid combination of multiple type-specifiers
typename factorization<18>::type x;
^
prog.cpp:67:36: error: invalid type in declaration before ‘;’ token
typename factorization<18>::type x;
^
prog.cpp:67:35: warning: unused variable ‘x’ [-Wunused-variable]
typename factorization<18>::type x;
^
Compilation error time: 0 memory: 0 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:67:30: error: ‘type’ in ‘struct factorization<18ll>’ does not name a type
typename factorization<18>::type x;
^
prog.cpp:67:36: error: invalid type in declaration before ‘;’ token
typename factorization<18>::type x;
^
prog.cpp:67:35: warning: unused variable ‘x’ [-Wunused-variable]
typename factorization<18>::type x;
^
// List of factors
template<std::intmax_t...>
struct factors { };
// Declaration
template<bool no_remainder, std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_remove_repeated;
template<bool no_remainder, std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_check;
// wraps the remainder check to reduce code duplication
template<template<bool, std::intmax_t ...> class T, std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ...Primes>
struct factorization_advance
{
typedef typename T<(Value % Divisor) == 0, Value, Divisor, Primes...>::type type;
};
// end case
template<template<bool, std::intmax_t ...> class T, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_advance<T, 1, Divisor, Primes...>
{
typedef factors<Primes...> type;
};
// No more repeats of Divisor, move to Divisor+1
template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_remove_repeated<false, Value, Divisor, Primes...>
{
typedef typename factorization_advance<factorization_check, Value, Divisor + 1, Primes...>::type type;
};
// removed a repeat of Divisor, continue doing so, without adding to primes list
template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_remove_repeated<true, Value, Divisor, Primes...>
{
typedef typename factorization_advance<::factorization_remove_repeated, Value / Divisor, Divisor, Primes...>::type type;
};
// found that Divisor isn't a factor, move to Divisor+1
template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_check<false, Value, Divisor, Primes...>
{
typedef typename factorization_advance<::factorization_check, Value, Divisor + 1, Primes...>::type type;
};
// Found first occurrence of a factor, add to primes list, remove repeats
template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
struct factorization_check<true, Value, Divisor, Primes...>
{
typedef typename factorization_advance<factorization_remove_repeated, Value / Divisor, Divisor, Primes..., Divisor>::type type;
};
// Convenience wrapper
template<std::intmax_t Value>
struct factorization
{
typedef typename factorization_advance<factorization_check, Value, 2>::type type;
};