我这里有这个类:
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;
}
};
我收到这个编译器错误:
1>------ Build started: Project: FileTester, Configuration: Debug Win32 ------
1> Matrix.cpp
1>c:usersskilz80documentsvisual studio 2015projectsfiletesterfiletestermatrix.h(155): error C3543: 'std::size_t': does not contain a parameter pack
1> c:usersskilz80documentsvisual studio 2015projectsfiletesterfiletestermatrix.h(171): note: see reference to class template instantiation 'MatrixCell<ClassType,Dims...>' being compiled
1>c:usersskilz80documentsvisual studio 2015projectsfiletesterfiletestermatrix.h(168): error C3543: 'std::size_t': does not contain a parameter pack
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
};
我理解错误代码没有问题;我在理解导致错误的确切原因时遇到了问题,并且不确定在我尝试了很多事情的地方纠正它的适当语法是什么。
我有一个类似的类,编译得很好,如下所示:
// DimensionPack & Helper Struct
struct MatrixDimensionOddOrEven {
const unsigned int even_or_odd;
explicit MatrixDimensionOddOrEven( unsigned int odd_or_even ) : even_or_odd( test( odd_or_even ) ) {}
private:
const unsigned int test( unsigned int value ) const {
if ( value == 0 ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << "invalid number: " << value << " must be >= 1.";
Logger::log( strStream, Logger::TYPE_ERROR );
throw ExceptionHandler( strStream );
}
return ( ((value % 2) == 0) ? EVEN : ODD );
}
}; typedef MatrixDimensionOddOrEven MatDimOddEven;
template <std::size_t... Dims>
class DimensionPack {
public:
std::vector<std::size_t> dimensions;
std::vector<unsigned int> even_or_odd;
const std::size_t total_dimensions = sizeof...(Dims);
const std::size_t total_elements = countElements();
public:
DimensionPack() : dimensions{Dims...},
even_or_odd{ MatrixDimensionOddOrEven{Dims}.even_or_odd...} {
}
private:
std::size_t countElements() {
std::size_t val = 1; // Don't Init to 0 otherwise multiplication won't work here!
for ( std::size_t n = 0; n < dimensions.size(); n++ ) {
val *= dimensions.at( n );
}
return val;
}
}; // DimensionPack
因此,正确方向的指针将非常有用,并将不胜感激。
你可能想要这个:
void addItem( ClassType item, decltype(Dims)... dims ) {
if ( !checkIndex( dims... ) ) {
和
bool checkIndex( decltype(Dims)... dims ) {
不幸的是,VC++无法编译它。这可能是编译器的缺陷(不太确定,因为我没有尝试根据标准验证这一点,但 g++ 和 clang 都编译了它(。幸运的是,我们可以解决它。我们真的不需要decltype
,我们需要一些需要std::size_t
常量表达式的东西,忽略它并返回std::size_t
。类似于常量类型级函数的东西。例如:
template <std::size_t>
struct const_size_t
{
using type = std::size_t;
};
并像这样使用它:
void addItem( ClassType item, typename const_size_t<Dims>::type... dims)
现场演示