测试温度B.h
#ifndef TESTTEMPB_H_
#define TESTTEMPB_H_
#include <string>
using namespace std;
namespace ddl_lib {
class TestTempB {
public:
TestTempB();
virtual ~TestTempB();
template<class ... Args>
void callCommandB(const string&, const uint32_t*, Args ...);
template<class ... Args>
void callCommandA(const string&, Args ...);
};
} /* namespace ddl_lib */
#endif /* TESTTEMPB_H_ */
Run Code Online (Sandbox Code Playgroud)
TestTempB.cpp
#include "TestTempB.h"
#include "TestTempA.h"
#include "stdint.h"
#include <iostream>
namespace ddl_lib {
TestTempB::TestTempB() {
// TODO Auto-generated constructor stub
}
TestTempB::~TestTempB() {
// TODO Auto-generated destructor stub
}
template<class ... Args>
void …Run Code Online (Sandbox Code Playgroud) 我希望得到一组值int buf[]={1...100}。我希望可以使用可变参数模板在编译时构造此数组。这是像Python /哈斯克尔等的列表理解
但是c ++ 11/14模板可以做到吗,怎么办?谢谢
我编写了以下代码来创建一个带有构造函数的类,该构造函数将变量数(N)的整数加上两个double作为参数,如下所示:
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <array>
template <std::size_t N>
class point_t {
public:
std::vector<int> values;
template<typename ... Args>
point_t(Args ... args, double distance, double value) {
std::array<int , N> list = {(args)...};
for(std::size_t i=0; i<N; ++i) values[i] = list[i];
}
};
int main(int argc, char *argv[]) {
point_t<4> test(1, 2, 3, 4, 3.0, 6.7);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器返回以下错误:
没有用于初始化'point_t <4>'的匹配构造函数
候选构造函数不可行:需要2个参数,但是提供了6个参数
我错过了什么?
我正在尝试编写一个类模板,该模板使用参数包并为参数包中包含的每种类型实现一个成员函数。
这是我到目前为止:
template <typename...T>
class Myclass {
public:
void doSomething((Some_Operator_to_divorce?) T) {
/*
* Do Something
*/
std::cout << "I did something" << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
我的目标是拥有一个可以按以下方式使用的类模板:
Myclass<std::string, int, double> M;
M.doSomething("I am a String");
M.doSomething(1234);
M.doSomething(0.1234);
Run Code Online (Sandbox Code Playgroud)
凡类模板机制将创建一个实现doSomething(std::string x),一个doSomething(int x)和doSomething(double x)成员函数而不是一个doSomething(std::string x, int i, double f)成员函数。
我在网上找到了很多关于参数包可用性的例子,但我不知道它是否可以用于我的目的,或者我是否完全误解了参数包的用途。
我以为我需要解包参数包,但在阅读了很多关于解包参数包的示例后,我认为这不是正确的选择,它具有完全不同的含义。
因此,因此,我正在寻找一种“离婚”参数包的操作。
我正在尝试Swift Protocol用Variadic财产创造。根据文档,可以在函数中执行此操作:
func arithmeticMean(_ numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
Run Code Online (Sandbox Code Playgroud)
但是尝试Variadic在协议中创建参数,如下所示:
struct ProductModel {
}
protocol SubscriptionModel {
var products: ProductModel... { get set }
}
Run Code Online (Sandbox Code Playgroud)
是不是不可能Variadic在 a 中创建一个属性Protocol?
您好,我想将一个结构传递给可变参数函数,并在 C 中使用所述结构内的值。我不知道该怎么做是如何访问传递的每个结构的内容。
这是一个示例情况
typedef struct {
int num;
bool dontIncludeFlag;
} number;
int average(unsigned count, ...){
va_list ap;
int j;
int sum = 0;
va_start(ap, count);
for (j = 0; j < count; j++) {
if(va_arg(ap,number.dontIncludeFlag)) //This line does not work
sum += va_arg(ap.num, number); //so does this line
}
va_end(ap);
return sum / count;
}
int main(){
number a,b,c;
a.num= 5;
a.dontIncludeFlag = 0;
b.num= 2;
b.dontIncludeFlag= 1;
c.num= 1;
c.dontIncludeFlag= 0;
average(3,a,b,c);
}
Run Code Online (Sandbox Code Playgroud)
如何访问我传递的结构参数的内容
假设我有 N 个不同类型的随机访问容器(std::vector例如std::array),并且所有容器都具有相同的长度。我想编写一个以列顺序方式打印它们的函数,即:
#include <vector>
#include <iostream>
#include <array>
#include <complex>
constexpr int nr=100;
void print(const std::vector<double>& d1, const std::array<std::complex<double>,nr>& b1, const std::vector<int>& b2, const std::array<double,nr>& d2)
{
for(int i=0; i<nr; ++i)
std::cout<<b1[i]<<" "<<d1[i]<<" "<<b2[i]<<" "<<d2[i]<<"\n";
}
Run Code Online (Sandbox Code Playgroud)
现在假设所有容器都包含标准数字类型,我可以编写一个可变参数模板函数,如下所示:
template<typename... T>
void vprint(T... cs)
{
constexpr int nc=sizeof...(T);
std::vector<std::vector<std::complex<long double>>> v(nr, std::vector<std::complex<long double>>(nc));
//then fill v recursively with all the cs and print it
}
Run Code Online (Sandbox Code Playgroud)
我使用的地方std::complex<long double>,因为它会包含任何可能的数字。然而,这个解决方案并不令人满意,因为我分配了额外的内存,我正在转换一些整数,从而破坏输出(可能还有精度),最后,如果任何容器包含不简单地转换为的可打印类型,则该解决方案不起作用数字类型。关于如何编写更通用的解决方案有什么想法吗?
void fun2(char *format, ...){
va_list arg_list;
va_start(arg_list, format);
vprintf(format, arg_list);
va_end(arg_list);
}
void fun1(char *format, ...){
fun2(format);
}
int main(){
fun1("test: %d", 100);
}
Run Code Online (Sandbox Code Playgroud)
输出:
测试:100
https://onlinegdb.com/OfdDeSJg_
上面的例子有什么错误或者不推荐的地方吗?
我猜想当调用时,只传递了fun2(format);指向第一个参数()的指针,这是正确的吗?format
当vprintfinfun2访问整数100时,这个整数在哪里?在为 保留的堆栈中fun1,在为 保留的堆栈中fun2,在为 保留的堆栈中vprintf,还是在其他地方?
如果正如我想象的那样,仅将指向第一个参数的指针传递给fun2,这是否意味着当通过fun2访问整数 100 调用的函数和宏时,它们正在访问为 保留的堆栈fun1?
从介绍http://en.cppreference.com/w/cpp/language/parameter_pack看,似乎"参数包"的概念与"可变参数模板"相同.那么它们之间的概念差异是什么?
谢谢!
我的应用程序具有这样的结构片段
type ItemOrder struct {
ItemId
...
}
var items = []*ItemOrder
Run Code Online (Sandbox Code Playgroud)
可变函数接受 ...int
func ItemIds(lang string, ids ...int){
...
Run Code Online (Sandbox Code Playgroud)
如何从items []*ItemOrder切片中获取所有itemIds 并将其提供给可变参数函数?就像是
itemsPB, err := ItemIds("", items[:].itemId)
Run Code Online (Sandbox Code Playgroud)
Abov无法正常工作,因为我没有给切片提供从中提取itemId的位置。