C 参数包作为变异模板
C++ parameter pack as variadic template
我尝试构建一个具有不同类型的参数(字符串,double,int,bool(的函数。为此,我使用了一个变异模板。函数参数传递给std :: tuple,其值将在稍后提取。但是我的应用程序会陷入编译错误。
错误:错误C2440"初始化":" char *"不能以" int"转换(转换为en(。这是为" int"论点,显然将被编译器认可为" char *"。评论/删除错误的线,该类型在运行时重新处理的类型正确地" int"。
IDE:Visual Studio 2017 Community Edition,V 15.5.6
示例:一个简单的代码示例,具有适当项目的所有基本功能,请下面。NB:此代码示例的汇编工作正常,没有错误,可变类型/值以及预期的输出:
#include<iostream>
#include<tuple>
using namespace std;
template<typename ... T>
int func1(string s, T... args) {
//creating the tuple ...
auto argsT = make_tuple(args...);
//run through the variables
double p0{ get<0>(argsT) };
string p1{ get<1>(argsT) };
int p2{ get<2>(argsT) };
bool p3{ get<3>(argsT) };
char p4{ get<4>(argsT) };
int p6{ get<6>(argsT) };
string w{ s };
std::cout << w.c_str() << std::endl;
double z = p0 * p6;
return 1;
}
int main()
{
func1("Kuckuck",1.0, "Hello", 4, false, 'n', "bye", -5, true, 't');
return 0;
}
看起来您尝试做类似:
的事情int p5{ get<5>(argsT) };
// "const char *" cannot be converted to "int"
这应该有效:
const char* p5{ get<5>(argsT) };
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 具有变异模板模板参数的方法的部分专业化
- 我如何实现变异模式以将可变数量的参数转发到C 11中的函数
- 组合类模板默认参数和变异参数
- 继承具有变异参数的构造函数
- C 参数包作为变异模板
- 在没有参数的变异模板中迭代
- 结构化参数为变异模板
- C 是否允许在变异模板参数之后进行普通参数
- 计数宏函数中变异参数的数量
- 如何使模板通过变异参数推断返回类型的函数
- 为什么C 不允许在非模板函数中进行变异参数
- C :处理Visual Studio中的每个变异参数
- 为什么这个嵌套的变异模板是无效的参数
- 如何绑定变异模板参数与功能