C++ 不知道为什么我';我收到关于std::size\t不包含参数包的编译错误

C++ 不知道为什么我';我收到关于std::size\t不包含参数包的编译错误,c++,variadic-templates,variadic-functions,C++,Variadic Templates,Variadic Functions,我在这里上课: template<typename ClassType, std::size_t... Dims> class MatrixCell { private: std::vector<std::size_t> coordinateIndices_; ClassType data_; public: MatrixCell() : coordinateIndices_{Dims...} {} //template<type

我在这里上课:

template<typename ClassType, std::size_t... Dims>
class MatrixCell {
private:
    std::vector<std::size_t> coordinateIndices_;
    ClassType data_;

public:
    MatrixCell() : coordinateIndices_{Dims...} {}

    //template<typename ClassType, std::size_t... Dims>
    void addItem( ClassType item, std::size_t... Dims) {
        if ( !checkIndex( Dims... ) ) {
            std::ostringstream strStream;
            strStream << __FUNCTION__ << " current index doesn't exist.";
            Logger::log( strStream, Logger::TYPE_ERROR );
            throw ExceptionHandler( strStream );
        } else {
            data_ = item;
        }
    }

private:
    //template<typename ClassType, std::size_t... Dims>
    bool checkIndex( std::size_t... Dims ) {
        return true;
    }
};
模板
类矩阵单元{
私人:
std::向量坐标;
类类型数据;
公众:
MatrixCell():坐标系{Dims…}{}
//模板
无效附加项(类类型项,标准::大小\u t…Dims){
如果(!检查索引(Dims…){
std::ostringstream strStream;

strStream您可能想要:

void addItem( ClassType item, decltype(Dims)... dims ) {
    if ( !checkIndex( dims... ) ) {

不幸的是,VC++无法编译此代码。这可能是编译器的缺陷(不太确定,因为我没有尝试根据标准验证此代码,但g++和clang都编译了此代码)。幸运的是,我们可以解决它。我们并不真正需要
decltype
,我们需要一个接受
std::size\t
常量表达式的东西,忽略它并返回
std::size\t
。类似于常量类型级别的函数。例如:

template <std::size_t>
struct const_size_t
{
    using type = std::size_t;
};
模板
结构常数大小
{
使用类型=标准::大小\u t;
};
然后像这样使用它:

void addItem( ClassType item, typename const_size_t<Dims>::type... dims) 
void addItem(类类型项,类型名称常量大小::类型…dims)

您可能想要:

void addItem( ClassType item, decltype(Dims)... dims ) {
    if ( !checkIndex( dims... ) ) {

不幸的是,VC++无法编译此代码。这可能是编译器的缺陷(不太确定,因为我没有尝试根据标准验证此代码,但g++和clang都编译了此代码)。幸运的是,我们可以解决它。我们并不真正需要
decltype
,我们需要一个接受
std::size\t
常量表达式的东西,忽略它并返回
std::size\t
。类似于常量类型级别的函数。例如:

template <std::size_t>
struct const_size_t
{
    using type = std::size_t;
};
模板
结构常数大小
{
使用类型=标准::大小\u t;
};
然后像这样使用它:

void addItem( ClassType item, typename const_size_t<Dims>::type... dims) 
void addItem(类类型项,类型名称常量大小::类型…dims)

<…> P> >……是关于C++ 11或C++ 14的问题吗?如果你包含了一个标签,请使用一个。@ TAMBRE……或者我使用VS2015,所以它不应该是关于可变模板和它们的参数包…@ TAMBRE固定它只剩下C++,如果问题被解决了,请更新你的问题或发布你的自己的答案可以帮助其他人解决类似的问题。尚不清楚你所说的

std::size\u t…Dims
函数参数是什么意思。
Dims
是一组常量。
MatrixCell
的一个实例是
MatrixCell
Dims…
将扩展到
2,3,4
。什么签名应该
MatrixCell::addItem
have?您的意思是“想要与
Dims
pack中的参数数量相同的
std::size\t
参数”……是关于C++ 11还是C++ 14的问题?如果你为这个规范包含了一个标签,那么只使用一个。@ TAMBRE要么…要么我使用VS2015,所以它不应该是关于可变模板和它们的参数包…@ Tabre修复它只剩下C++,如果问题被解决了,请更新你的问题或者发布你自己的AN。我们不清楚你所说的
std::size\u t…Dims
函数参数是什么意思。
Dims
是一组常量。
MatrixCell
的一个实例是
MatrixCell
Dims…
将扩展到
2,3,4
。什么样的签名应该
矩阵Cell::addItem
have?您的意思是“想要与
Dims
pack中的参数数量相同的
std::size\t
参数”?我喜欢您的答案,但是当我尝试时,我得到了以下结果:
matrix.h(154):错误C3543:“未知类型”:不包含参数pack matrix.h(170):注意:请参阅对正在编译的类模板实例化“MatrixCell”的引用matrix.h(167):错误C3543:“未知类型”:不包含参数包
(…续)我甚至试图将成员函数声明为模板函数,但仍然生成相同的编译器错误。@FrancisCugler它使用gcc和clang编译,一定是vc++的缺陷。你可以解决它,我喜欢你的答案,但是当我尝试时,我得到了这个:
matrix.h(154):错误C3543:“未知类型”:不包含参数包矩阵。h(170):注意:请参阅对正在编译矩阵的类模板实例化“MatrixCell”的引用。h(167):错误C3543:“未知类型”:不包含参数包
(…续)我甚至试图将成员函数声明为模板函数,但仍然生成相同的编译器错误。@FrancisCugler它使用gcc和clang编译,一定是vc++的缺陷。您可以解决它,请参阅