- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了下面的代码:
#include<array>
#include<type_traits>
namespace math{
namespace detail{
template<std::size_t... Is> struct seq{};
template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...>{};
template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...>{};
template<class T,std::size_t N>
struct sin_coeffs{
using array_type = std::array<T,N>;
constexpr static inline T coeff(std::size_t n){
return power(-1, n-1) * inverse((T)factorial((2 * n)-1));
}
template<std::size_t...NS>
constexpr static array_type _coeffs(seq<NS...>){
return {{coeff(NS)...}};
}
constexpr static array_type coeffs=_coeffs(gen_seq<N>{});
};
}
template<class T,std::size_t N = max_factorial, class dcy = std::decay_t<T>>
constexpr std::enable_if_t<std::is_floating_point<dcy>::value,dcy> sin(T x) noexcept{
constexpr std::array<dcy,N>& coeffs = detail::sin_coeffs<dcy,N>::coeffs;
const dcy x_2 = x*x;
dcy pow = x;
dcy result = 0;
for(std::size_t i=0;i<N;++i){
result += coeffs[i] * pow;
pow*=x_2;
}
return result;
}
}
主要示例:
int main()
{
constexpr double d = math::sin(0.0);
}
代码被设计成一个 constexpr sin 函数,它使用一个包含系数的 constexpr 数组来进行所需的计算。
所有未列出的函数都存在于单独的头文件中,编译没有问题。
我正在尝试使用“indicies”技巧使用基于 this answer to another question 的 constexpr 函数填充数组.
我正在使用带有标志的 GCC 5.3.1 进行编译
--std=c++1z -pthread -g -O3 -MMD -MP -Wall -pedantic
编译器没有向我的代码发出错误,但在编译时停止了。
我已经让编译运行了几分钟,但它什么也没做。
我已经测试了代码中使用的所有函数,它们都可以独立于本节进行编译。
int main()
{
math::detail::sin_coeffs<double,20>::coeffs[0];
}
此代码片段还重现了使我相信它与 sin 函数本身无关但与 sin_coeffs 结构无关的问题。
编辑:以下是要求的其他功能:
#include <type_traits>
namespace math{
template<class T,class dcy = std::decay_t<T>>
constexpr inline std::enable_if_t<std::is_floating_point<T>::value,dcy> inverse(T value){
return (value == 0) ? 0.0 : 1.0 / value;
}
template <class T>
constexpr inline std::decay_t<T> sign(T value) {
return value < 0 ? -1 : 1;
}
template <typename T>
constexpr inline std::decay_t<T> abs(T value) {
return value * sign(value);
}
template<class T>
constexpr inline std::decay_t<T> power(T const& base, std::size_t const& pow){
if(pow==0){return 1;}
else if(pow == 1){return base;}
else{
T result = base;
for(std::size_t i=1;i<pow;++i){
result*=base;
}
return result;
}
}
constexpr std::intmax_t factorial(std::intmax_t const& n){
if(n==0){return 1;}
std::intmax_t result = n;
for(std::intmax_t i=n-1;i>0;--i){
result *=i;
}
return result;
}
constexpr static std::size_t max_factorial = 20;//replace with calculated version later
}
最佳答案
const
限定符在这里:constexpr const std::array<dcy,N>& coeffs = /* ... */ ;
^^^^^
gen_seq
从 0
生成值至 N - 1
, 但关于你的 coeff
函数,您需要来自 1
的值至 N
.您可以通过以下任一方式解决此问题:1
生成值至 N
(详细解释见本答案底部):template<std::size_t N, std::size_t... Is>
struct gen_seq: gen_seq<N-1, N, Is...> {};
// ^--- N instead of N - 1
template<std::size_t...NS>
constexpr static array_type _coeffs(seq<NS...>){
return {{coeff(NS + 1)...}};
// ^^^^
}
power
计算 -1 ** n
,使用三元条件:constexpr static inline T coeff(std::size_t n){
return (n%2 ? 1 : -1) * inverse((T)factorial((2 * n)-1));
}
有了这个,我可以计算系数:
auto arr = math::detail::sin_coeffs<double, 10>::coeffs;
for (auto x: arr) {
std::cout << x << " ";
}
输出:
1 -0.166667 0.00833333 -0.000198413 2.75573e-06 -2.50521e-08 1.6059e-10 -7.64716e-13 ...
据我所知,这些是正确的系数( 1
、 -1/3!
、 1/5!
……)。请注意,我必须使用 N = 10
,否则我会溢出 std::intmax_t
(在我的架构上)- 如果溢出 std::intmax_t
,Clang 会在编译时警告您与 factorial
(多么好的编译器!)。
通过以上两个修改,您的代码可以正常工作(max_factorial
值除外,但您可以根据需要调整它)。
参见 rextester
上的代码: http://rextester.com/CRR35028
你的 gen_seq<N>
从 0
生成一个序列至 N - 1
, 所以你调用 coeff(0)
,正在调用 power(-1, static_cast<size_t>(0) - 1)
,实际上是 power(-1, 18446744073709551615)
(在我的架构上),无法编译。在 power
中添加一个简单的案例“修复”编译(表明这是一个问题,但没有解决真正的问题):
template<class T>
constexpr inline std::decay_t<T> power(T const& base, std::size_t const& pow) {
if (pow == static_cast<size_t>(0) - 1) { // Stupid test
return 1;
}
/* ... */
}
此外,您的 max_factorial
值(value)也可能太大了。更正后power
, 我无法为 max_factorial > 11
编译(我可能有 32 位 std::intmax_t
,所以你可以超过这个,但我认为 20 在所有情况下都太大了)。
此外,对于 future 的问题,clang
似乎提供了关于为什么它不编译的更好信息:
power
正在做一个(几乎)无限循环。factorial
的返回值给出警告溢出 intmax_t
.gen_seq
把戏?你的 seq
struct 基本上是 std::size_t...
的结构持有者(因为您不能将其直接存储在变量中)。
gen_seq
是构建gen_seq<N, ...>
的“递归”结构使用 gen_seq<N - 1, ...>
.它是如何工作的:
gen_seq<3>: gen_seq<2, 2>
gen_seq<2, 2>: gen_seq<1, 1, 2>
gen_seq<1, 1, 2>: gen_seq<0, 0, 1, 2>
gen_seq<0, 0, 1, 2>: seq<0, 1, 2> // Specialized case
如你所见,既然你继承了gen_seq<N - 1, N - 1, ...>
, 最后继承自 seq
有 0
值(value),你不想要的。因为什么是“发送”到seq
是可变参数 std::size_t...
, 你想避免 0
, 所以你改成 gen_seq<N - 1, N, ...>
:
gen_seq<3>: gen_seq<2, 3>
gen_seq<2, 3>: gen_seq<1, 2, 3>
gen_seq<1, 2, 3>: gen_seq<0, 1, 2, 3>
gen_seq<0, 1, 2, 3>: seq<1, 2, 3> // Specialized case
现在,当您调用 _coeffs(gen_seq<N>{})
,您允许编译器推断模板参数 NS
的 _coeffs
.由此,您可以使用 NS
在 _coeffs
做pack expansion :
{{coeff(NS)...}};
关于c++ - GCC 5.3.1 C++ 在编译 Variadic 模板时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069092/
我试图创建一个模板,给定一组元函数和一个元函数,将这个元函数添加到提供的集合中。 template class...> class Dest, template c
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
考虑以下代码 #define COMB(F, ...) F(__VA_ARGS__) #define ADD(X, Y) (X + Y) int foo() { return COMB(ADD
以下代码可以编译,因为我假设 parent的 Object本身没有 parent 。 template struct Object { T item; // T is item's typ
我目前正在研究一些新的 C++17 特性,特别是 std::optional,我决定尝试找到一个最小值使用可变参数模板的函数。这是我目前所拥有的: template typename Opt,
我想知道是我做错了什么还是编译器错误。我正在使用 Intel C++ Composer XE 2011 for Windows SP1(或更新 6,目前是最新的)。请参阅代码中的注释行。 #inclu
阅读可变参数函数时,我发现了一个 sum 函数,它接受任意数量的任意数字类型并计算它们的总和。 具有此函数的模板化特性,我希望它接受 string 对象,因为运算符 + 是为字符串定义的。 #incl
在 2012 ACCU C++ Pub quiz 的第 15 个问题中,我被结果难住了。 #include template void P(T x) { std::cout // foo 2 v
我在这里遗漏了一些关于类型推导的非常基本的东西: 我正在尝试编写一个包装函数,它调用一个带有 nullptr 的编写器函数以获得所需的长度,然后调整缓冲区大小,然后再次调用该函数,现在使用调整大小的缓
如何创建两个 VARIADIC 参数。查看我的代码并纠正我。 CREATE OR REPLACE FUNCTION ip_source_test(text,text,date,date,VARIADI
我希望创建一个类来检查两个参数类型列表,以查看是否可以将第一个列表中的所有参数转换为第二个列表。到目前为止,我有这样的事情: template class conv{ public: t
我想了解可变参数模板的工作原理。在下面的示例中,我想将变量参数传递给类的构造函数并将其存储到一个元组中,以供稍后使用。 template class CompoundOrs { public:
我正在尝试创建一个可变参数模板类,其方法需要迭代模板参数并为每种类型调用不相关的模板函数。 这个想法是用特定的模板参数扩展这个模板类,以封装基本行为。 一般想法的一个简单示例是这个(糟糕的)Check
考虑这个最小的例子: template struct foo {}; template class Bar> struct converter { template conv
我有以下测试程序: #include template struct Dispatcher { template static inline void call1(bool a, bool
我有一个模板化函数包装器,我正在尝试将其更新为 C++11 语法(可变参数)。 我的问题是我陷入了“第 22 条军规”,其中 'Args...' 必须是最后一个模板参数,但同时不能在函数指针之后定义模
我正在尝试组合一个通用方法调用程序(用于 C++ OO/v8 桥),使用可变参数模板元编程构建参数列表,转换为 native 类型,最后执行附加方法,一旦传入参数列表为空(因此构建了传出): temp
我已经使用可变参数函数来包装 printf(或 vprintf)。 除了在 warning_printf 的第一个可变参数上出错外,以下代码有效。此外,直接放置字符串会更改 ASCII 字符,但不会修
在 stackoverflow 问题之后:“具有零参数和逗号的可变参数宏”,我尝试了: #define MAKE_TEMPLATE(...) template MAKE_TEMPLATE() str
为了我的理解,我正在尝试使用可变参数模板编写一个简单的记录器。它有效,但有一个问题。 void log() { std::cout void log(T t1,
我是一名优秀的程序员,十分优秀!