C++ 在va_arg上访问冲突
我试图创建一个函数,该函数在参数中采用可变数量的矩阵,并将这些矩阵乘以第一个矩阵。 我可以使用C++ 在va_arg上访问冲突,c++,templates,variadic-functions,variadic,C++,Templates,Variadic Functions,Variadic,我试图创建一个函数,该函数在参数中采用可变数量的矩阵,并将这些矩阵乘以第一个矩阵。 我可以使用va_arg读取第一个,但下一次调用va_arg将导致访问冲突 以下是我如何声明我的方法: template<class MType> static CMatrice<MType> COperationsComplexesMatricesPassages::OCPChangementDeBase (CMatrice<MType> & MATVecteur
va_arg
读取第一个,但下一次调用va_arg
将导致访问冲突
以下是我如何声明我的方法:
template<class MType>
static CMatrice<MType> COperationsComplexesMatricesPassages::OCPChangementDeBase
(CMatrice<MType> & MATVecteur, unsigned int uiNbMatricesPassages,
CMatricePassage<MType> MAPMatrices...)
异常出现在我的方法主体中(…)的的第一个va_arg
上。
这是我的方法的代码:
unsigned int uiIndice;
unsigned int uiBaseArriveePrecedente;
va_list args;
va_start(args, MAPMatrices);
CMatrice<MType> MATResult(MATVecteur);
CMatricePassage<MType> MAPMatricePass = va_arg(args, CMatricePassage<MType>);
MATResult = MATResult * MAPMatricePass;
uiBaseArriveePrecedente = MAPMatricePass.MAPGetBaseArrivee();
for (uiIndice = 1; uiIndice < uiNbMatricesPassages; uiIndice++) {
CMatricePassage<MType> MAPMatricePass2 = va_arg(args, CMatricePassage<MType>);
if (uiBaseArriveePrecedente != MAPMatricePass2.MAPGetBaseDepart()) {
CException EXCError(EXC_ChangementImpossible);
throw EXCError;
}
uiBaseArriveePrecedente = MAPMatricePass2.MAPGetBaseArrivee();
MATResult = MATResult * MAPMatricePass2;
}
return MATResult;
无符号整数索引;
未签名的int-UIbaseArriveReceident;
va_列表参数;
va_开始(参数、映射矩阵);
CMatrice MATResult(MATVecteur);
CMatricePassage MAPMatricePass=va_arg(args,CMatricePassage);
MATResult=MATResult*MAPMatricePass;
UIBaseArrievePresente=MapMatriceCPass.MapGetBaseArrieve();
对于(uiIndice=1;uiIndice
我不明白您到底想从OCPChangementDeBase()
方法中获得什么。。。也许我错了。。。但在我看来,关于变量函数,有几个重要的点你不知道
(1) 旧的C变量语法
void foo (int a, int b...)
这并不意味着b
是整数的可变列表
该声明等同于(最后一个逗号是可选的)
因此,对于这两个声明,您有一个b
整数(一个b
整数)和一个未命名的可变参数列表
所以给你一个方法
template<class MType>
static CMatrice<MType>
COperationsComplexesMatricesPassages::OCPChangementDeBase
(CMatrice<MType> & MATVecteur, unsigned int uiNbMatricesPassages,
CMatricePassage<MType> MAPMatrices...)
你有吗
MATVecteur
变成mat1
uinbmatricespassions
变为2
映射矩阵
变成matP1
- 未命名的变量列表
…
变成matP2
因此,如果您希望在未命名变量列表中有两个参数,那么您只有一个参数,而且“下一次调用va_arg将导致访问冲突”也就不足为奇了
(2)旧C变量语法(<代码> VAYLISTA/<代码>, VAYARG, VAXSTART 基础)在C++中仍然可用,但据我所知,仅适用于POD(普通旧数据)类型。
所以,据我所知,您的代码是UB(undefined bahaviour),因为matP2
(我想)不是POD
幸运的是C++(从C++ 11开始)引入了可变的模板,这些模板也可以兼容而不是POD类型。
所以,我想,你可以写下你的方法如下或类似的东西
template <typename MType, typename ... MTs>
static auto COperationsComplexesMatricesPassages::OCPChangementDeBase
(CMatrice<MType> & MATVecteur, MTs ... MAPMatrices)
{
auto MatResult { MatVectour };
( MatResult *= MapMatrices, ... ); // template folding; only from C++17
return MatResult;
}
模板
静态自动处理复杂的矩阵过程::OCPChangementDeBase
(CMatrice&MATVecteur,MTs…地图矩阵)
{
自动MatResult{MatVectour};
(MatResult*=MapMatrix,…);//模板折叠;仅来自C++17
返回结果;
}
您还可以添加一些约束(查找SFINAE)来强制执行MTs.
类型正好是(或者更好地说,可以转换为)CMatricePassage
(或者其他类型,如果需要)
template<class MType>
static CMatrice<MType>
COperationsComplexesMatricesPassages::OCPChangementDeBase
(CMatrice<MType> & MATVecteur, unsigned int uiNbMatricesPassages,
CMatricePassage<MType> MAPMatrices...)
COperationsComplexesMatricesPassages::OCPChangementDeBase(mat1, 2, matP1, matP2)
template <typename MType, typename ... MTs>
static auto COperationsComplexesMatricesPassages::OCPChangementDeBase
(CMatrice<MType> & MATVecteur, MTs ... MAPMatrices)
{
auto MatResult { MatVectour };
( MatResult *= MapMatrices, ... ); // template folding; only from C++17
return MatResult;
}