- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图了解如何正确实现一个函数来计算数学样本均值,具有两个初始要求的特征:
1) 使用可变参数。
2) 不使用两个函数来完成这项工作,即不使用调用函数,然后使用第二个函数实际进行计算。
3) 函数应该尽可能通用
我很清楚已经有人问过一个非常相似的问题:Calculate the average of several values using a variadic-template function然而,虽然该问题的公认答案似乎教会了 OP 如何完成他不知道的小部分,但它提供的代码实际上是错误的并且无法编译。
所以,我自己的第一次尝试是沿着这些思路进行的:
template <class... Args>
double mean(const Args & ... args)
{
auto total = 0;
for (auto value : { ...args })
{
total += value;
}
return (double)total / sizeof...(args);
}
这里的问题是在 auto total = 0;
行中,编译自然无法自动识别对象 total
应该具有的类型。
然后,我的第二次尝试:
template <class T, class... Args>
T mean(const T &t, const Args & ... args)
{
T total = 0;
for (auto value : { args... })
{
total += value;
}
return (T)(total / sizeof...(args));
}
那个版本有以下问题。如果调用者使用混合类型的参数调用函数,它将不起作用,例如 mean(1, 2.5)
,其中第一个参数被自动检测为 int
并且第二个被检测为 double
。
我能够通过执行以下操作解决该问题:
template <class T, class... Args>
T mean(const T &t, const Args & ... args)
{
size_t argsSize = sizeof...(args);
T total = t;
T arg_array[] = { args... };
for (size_t i = 0; i< argsSize; i++)
{
total += (T)arg_array[i];
}
return (T)(total / argsSize) ;
}
即使传递的参数是不同的类型(当然,前提是这些类型可以转换为 T
),这个方法仍然有效。然而,现在的问题是该函数仅适用于至少两个参数。如果像 mean(3.14)
那样调用它,虽然它应该返回 3.14,但它实际上会引发错误,因为 T arg_array[] = { args... }
can'无法编译,因为不可能创建大小为 0
的静态数组。当然,我可以将其替换为动态数组,但这将使我每次调用该函数时都必须进行一次内存分配和一次内存释放 - 这是一种 Not Acceptable 浪费。
那么,实现这样一个功能的正确方法是什么,它可以避免上述问题并满足我的两个初始条件?
最佳答案
template<class... Args> constexpr auto mean(Args... args) {
std::common_type_t<Args...> total(0);
for(auto value : {std::common_type_t<Args...>(args)...}) total += value;
return total/sizeof...(args);
}
关于c++ - 类型检测 : using variadic arguments to properly implement a function that calculates the mean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404672/
我试图创建一个模板,给定一组元函数和一个元函数,将这个元函数添加到提供的集合中。 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,
我是一名优秀的程序员,十分优秀!